hyf

Context-aware query service for Radroots
git clone https://radroots.dev/git/hyf.git
Log | Files | Refs | README | LICENSE

snapshot.mojo (1704B)


      1 from hyf_core.domain.product import ProductRef
      2 from hyf_core.domain.units import unit_dimension
      3 
      4 
      5 @fieldwise_init
      6 struct SupplySnapshot(Copyable, Movable):
      7     var lot_id: String
      8     var revision: String
      9     var supplier_id: String
     10     var product: ProductRef
     11     var unreserved_state: String
     12     var unreserved_value: Int
     13     var unreserved_scale: Int
     14     var unit: String
     15     var dimension: String
     16 
     17 
     18 def supply_snapshot(
     19     lot_id: String,
     20     revision: String,
     21     supplier_id: String,
     22     product: ProductRef,
     23     unreserved_state: String,
     24     unreserved_value: Int,
     25     unreserved_scale: Int,
     26     unit: String,
     27 ) raises -> SupplySnapshot:
     28     if (
     29         lot_id.strip() == ""
     30         or revision.strip() == ""
     31         or supplier_id.strip() == ""
     32     ):
     33         raise Error("snapshot requires lot id, revision and supplier id")
     34     if unreserved_state != "known" and unreserved_state != "unknown":
     35         raise Error("unreserved state must be 'known' or 'unknown'")
     36     if unreserved_scale < 0 or unreserved_scale > 9:
     37         raise Error("unreserved scale must be between 0 and 9")
     38     return SupplySnapshot(
     39         lot_id=String(lot_id),
     40         revision=String(revision),
     41         supplier_id=String(supplier_id),
     42         product=product.copy(),
     43         unreserved_state=String(unreserved_state),
     44         unreserved_value=unreserved_value,
     45         unreserved_scale=unreserved_scale,
     46         unit=String(unit),
     47         dimension=String(unit_dimension(unit)),
     48     )
     49 
     50 
     51 def snapshot_unreserved_known(snapshot: SupplySnapshot) -> Bool:
     52     return snapshot.unreserved_state == "known"
     53 
     54 
     55 def snapshot_identity(snapshot: SupplySnapshot) -> String:
     56     return snapshot.lot_id + "@" + snapshot.revision