dto.rs (3487B)
1 #![allow(dead_code)] 2 3 use dto_bindgen_core::RootDescriptor; 4 5 pub fn dto_roots() -> Vec<RootDescriptor> { 6 vec![ 7 RootDescriptor::new::<DiscountDescriptor>(), 8 RootDescriptor::new::<DiscountScopeDescriptor>(), 9 RootDescriptor::new::<DiscountThresholdDescriptor>(), 10 RootDescriptor::new::<DiscountValueDescriptor>(), 11 RootDescriptor::new::<MoneyDescriptor>(), 12 RootDescriptor::new::<PercentDescriptor>(), 13 RootDescriptor::new::<QuantityDescriptor>(), 14 RootDescriptor::new::<QuantityPriceDescriptor>(), 15 RootDescriptor::new::<UnitDescriptor>(), 16 RootDescriptor::new::<UnitDimensionDescriptor>(), 17 ] 18 } 19 20 #[derive(dto_bindgen::Dto)] 21 #[dto(as = "string")] 22 #[dto(ts(name = "Currency"))] 23 struct CurrencyDescriptor(String); 24 25 #[derive(dto_bindgen::Dto)] 26 #[dto(as = "string")] 27 #[dto(ts(name = "Decimal"))] 28 struct DecimalDescriptor(String); 29 30 #[derive(dto_bindgen::Dto)] 31 #[dto(ts(name = "Money"))] 32 struct MoneyDescriptor { 33 amount: DecimalDescriptor, 34 currency: CurrencyDescriptor, 35 } 36 37 #[derive(dto_bindgen::Dto)] 38 #[dto(ts(name = "Percent"))] 39 struct PercentDescriptor { 40 value: DecimalDescriptor, 41 } 42 43 #[derive(dto_bindgen::Dto)] 44 #[dto(as = "string_enum")] 45 #[dto(ts(name = "Unit"))] 46 enum UnitDescriptor { 47 #[dto(rename = "each")] 48 Each, 49 #[dto(rename = "kg")] 50 MassKg, 51 #[dto(rename = "g")] 52 MassG, 53 #[dto(rename = "oz")] 54 MassOz, 55 #[dto(rename = "lb")] 56 MassLb, 57 #[dto(rename = "l")] 58 VolumeL, 59 #[dto(rename = "ml")] 60 VolumeMl, 61 } 62 63 #[derive(dto_bindgen::Dto)] 64 #[dto(as = "string_enum")] 65 #[dto(ts(name = "UnitDimension"))] 66 enum UnitDimensionDescriptor { 67 #[dto(rename = "count")] 68 Count, 69 #[dto(rename = "mass")] 70 Mass, 71 #[dto(rename = "volume")] 72 Volume, 73 } 74 75 #[derive(dto_bindgen::Dto)] 76 #[dto(ts(name = "Quantity"))] 77 struct QuantityDescriptor { 78 amount: DecimalDescriptor, 79 unit: UnitDescriptor, 80 #[serde(skip_serializing_if = "Option::is_none")] 81 label: Option<String>, 82 } 83 84 #[derive(dto_bindgen::Dto)] 85 #[dto(ts(name = "QuantityPrice"))] 86 struct QuantityPriceDescriptor { 87 amount: MoneyDescriptor, 88 quantity: QuantityDescriptor, 89 } 90 91 #[derive(dto_bindgen::Dto)] 92 #[dto(as = "string_enum")] 93 #[dto(ts(name = "DiscountScope"))] 94 enum DiscountScopeDescriptor { 95 #[dto(rename = "bin")] 96 Bin, 97 #[dto(rename = "order_total")] 98 OrderTotal, 99 } 100 101 #[derive(dto_bindgen::Dto)] 102 #[dto(ts(name = "DiscountThreshold"))] 103 #[serde(rename_all = "snake_case", tag = "kind", content = "amount")] 104 enum DiscountThresholdDescriptor { 105 BinCount { bin_id: String, min: u32 }, 106 OrderQuantity { min: QuantityDescriptor }, 107 } 108 109 #[derive(dto_bindgen::Dto)] 110 #[dto(ts(name = "DiscountValue"))] 111 #[serde(rename_all = "snake_case", tag = "kind", content = "amount")] 112 enum DiscountValueDescriptor { 113 MoneyPerBin(MoneyDescriptor), 114 Percent(PercentDescriptor), 115 } 116 117 #[derive(dto_bindgen::Dto)] 118 #[dto(ts(name = "Discount"))] 119 struct DiscountDescriptor { 120 scope: DiscountScopeDescriptor, 121 threshold: DiscountThresholdDescriptor, 122 value: DiscountValueDescriptor, 123 } 124 125 #[cfg(test)] 126 mod tests { 127 use dto_bindgen_core::build_registry; 128 129 use super::dto_roots; 130 131 #[test] 132 fn canonical_core_descriptor_roots_build_without_diagnostics() { 133 let roots = dto_roots(); 134 let registry = build_registry(roots.clone()); 135 136 assert!(!registry.has_errors(), "{:?}", registry.diagnostics); 137 assert_eq!(registry.roots.len(), roots.len()); 138 } 139 }