product.mojo (1217B)
1 from std.collections import Optional 2 3 4 @fieldwise_init 5 struct ProductRef(Copyable, Movable): 6 var phrase: String 7 var catalogue_id: Optional[String] 8 var resolution: String 9 10 11 def resolved_product(phrase: String, catalogue_id: String) raises -> ProductRef: 12 if phrase.strip() == "": 13 raise Error("product phrase must not be empty") 14 if catalogue_id.strip() == "": 15 raise Error("resolved product requires a catalogue id") 16 return ProductRef( 17 phrase=String(phrase), 18 catalogue_id=Optional[String](String(catalogue_id)), 19 resolution="resolved", 20 ) 21 22 23 def unresolved_product(phrase: String) raises -> ProductRef: 24 if phrase.strip() == "": 25 raise Error("product phrase must not be empty") 26 return ProductRef( 27 phrase=String(phrase), catalogue_id=None, resolution="unresolved" 28 ) 29 30 31 def product_is_resolved(product: ProductRef) -> Bool: 32 return product.resolution == "resolved" and product.catalogue_id is not None 33 34 35 def product_matches(left: ProductRef, right: ProductRef) -> Bool: 36 if not product_is_resolved(left) or not product_is_resolved(right): 37 return False 38 return left.catalogue_id.value() == right.catalogue_id.value()