open_menu_standard 0.1.0

Rust implementation of the OpenMenuStandard (OMS) specification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// src/document.rs
//
// Implementation of OmsDocument methods

use std::collections::HashMap;

use crate::{OMS_VERSION, OmsError, OmsResult};
use crate::types::*;
use crate::validation::validate_document;
use chrono::Utc;
use serde_json::{to_string_pretty, from_str};
use validator::Validate;

impl OmsDocument {
    /// Create a new OMS document with the minimum required fields
    pub fn new(metadata: Metadata, vendor: Vendor, items: Vec<Item>) -> Self {
        Self {
            oms_version: OMS_VERSION.to_string(),
            metadata,
            vendor,
            items,
            order: None,
            extensions: None,
        }
    }
    
    /// Create a new OMS document with an order
    pub fn with_order(metadata: Metadata, vendor: Vendor, items: Vec<Item>, order: Order) -> Self {
        Self {
            oms_version: OMS_VERSION.to_string(),
            metadata,
            vendor,
            items,
            order: Some(order),
            extensions: None,
        }
    }
    
    /// Validate the OMS document according to the specification
    pub fn validate(&self) -> OmsResult<()> {
        // Perform additional validations
        validate_document(self)?;
        
        Ok(())
    }
    
    /// Serialize the OMS document to a JSON string
    pub fn to_json(&self) -> OmsResult<String> {
        let json = to_string_pretty(self)?;
        Ok(json)
    }
    
    /// Serialize the OMS document to a compact JSON string (for NFC tags)
    pub fn to_compact_json(&self) -> OmsResult<String> {
        let json = serde_json::to_string(self)?;
        Ok(json)
    }
    
    /// Deserialize an OMS document from a JSON string
    pub fn from_json(json: &str) -> OmsResult<Self> {
        let document: Self = from_str(json)?;
        document.validate()?;
        Ok(document)
    }
    
    /// Calculate total price for all items in the order
    pub fn calculate_total_price(&self) -> Option<f64> {
        // Sum up the prices of all items
        let items_total = self.items.iter().fold(0.0, |acc, item| {
            // Get the base price or fallback to 0.0
            let base_price = item.base_price.unwrap_or(0.0);
            
            // Get the quantity or fallback to 1
            let quantity = item.quantity.unwrap_or(1) as f64;
            
            // Get the calculated price if available
            let item_price = match &item.calculated {
                Some(calc) => calc.item_price,
                None => base_price,
            };
            
            acc + (item_price * quantity)
        });
        
        // Return the total if it's greater than zero
        if items_total > 0.0 {
            Some(items_total)
        } else {
            None
        }
    }
    
    /// Create an OMS URL for this document
    pub fn create_url(&self) -> Option<String> {
        // We need vendor ID to create a URL
        let vendor_id = &self.vendor.id;
        
        // Get the location ID if available
        let location_param = match &self.vendor.location_id {
            Some(location_id) => format!("&l={}", location_id),
            None => String::new(),
        };
        
        // Use the first item ID if available
        if let Some(first_item) = self.items.first() {
            let item_id = &first_item.id;
            Some(format!("omenu://order?v={}{}&i={}", vendor_id, location_param, item_id))
        } else {
            // If no items, just return the vendor URL
            Some(format!("omenu://view?v={}{}", vendor_id, location_param))
        }
    }
    
    /// Add an item to the document
    pub fn add_item(&mut self, item: Item) {
        self.items.push(item);
    }
    
    /// Remove an item by ID
    pub fn remove_item(&mut self, item_id: &str) -> bool {
        let initial_len = self.items.len();
        self.items.retain(|item| item.id != item_id);
        self.items.len() < initial_len
    }
    
    /// Find an item by ID
    pub fn find_item(&self, item_id: &str) -> Option<&Item> {
        self.items.iter().find(|item| item.id == item_id)
    }
    
    /// Find an item by ID and return a mutable reference
    pub fn find_item_mut(&mut self, item_id: &str) -> Option<&mut Item> {
        self.items.iter_mut().find(|item| item.id == item_id)
    }
    
    /// Add order information to the document
    pub fn set_order(&mut self, order: Order) {
        self.order = Some(order);
    }
    
    /// Update the order status
    pub fn update_order_status(&mut self, status: OrderStatus) -> OmsResult<()> {
        match &mut self.order {
            Some(order) => {
                order.status = Some(status);
                Ok(())
            },
            None => Err(OmsError::MissingRequiredField("order".to_string())),
        }
    }
    
    /// Extract selected customizations as a compact representation
    pub fn extract_customization_selections(&self) -> HashMap<String, Vec<SelectedCustomization>> {
        let mut result = HashMap::new();
        
        for item in &self.items {
            if let Some(selections) = &item.selected_customizations {
                result.insert(item.id.clone(), selections.clone());
            }
        }
        
        result
    }
    
    /// Add an extension to the document
    pub fn add_extension(&mut self, namespace: &str, data: serde_json::Value) {
        let extensions = self.extensions.get_or_insert_with(HashMap::new);
        extensions.insert(namespace.to_string(), data);
    }
    
    /// Get an extension from the document
    pub fn get_extension(&self, namespace: &str) -> Option<&serde_json::Value> {
        self.extensions.as_ref().and_then(|e| e.get(namespace))
    }
    
    /// Create a new OmsDocument with the current timestamp
    pub fn now(vendor_id: &str, vendor_name: &str, vendor_type: &str) -> Self {
        let metadata = Metadata {
            created: Utc::now(),
            source: "open_menu_standard".to_string(),
            locale: "en-US".to_string(),
        };
        
        let vendor = Vendor {
            id: vendor_id.to_string(),
            name: vendor_name.to_string(),
            r#type: vendor_type.to_string(),
            location_id: None,
            location_name: None,
            address: None,
            contact: None,
            hours: None,
            cuisine: None,
            services: None,
        };
        
        Self::new(metadata, vendor, Vec::new())
    }
}

/// Parse an OMS document from a JSON string
pub fn parse_oms_document(json: &str) -> OmsResult<OmsDocument> {
    OmsDocument::from_json(json)
}

/// Create a compact JSON representation suitable for NFC tags
pub fn create_compact_oms_json(document: &OmsDocument) -> OmsResult<String> {
    document.to_compact_json()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::*;
    
    fn create_test_document() -> OmsDocument {
        let metadata = Metadata {
            created: Utc::now(),
            source: "test".to_string(),
            locale: "en-US".to_string(),
        };
        
        let vendor = Vendor {
            id: "test-vendor".to_string(),
            name: "Test Restaurant".to_string(),
            r#type: "restaurant".to_string(),
            location_id: None,
            location_name: None,
            address: None,
            contact: None,
            hours: None,
            cuisine: None,
            services: None,
        };
        
        let item = Item {
            id: "test-item".to_string(),
            name: "Test Item".to_string(),
            category: "test".to_string(),
            vendor_id: None,
            description: None,
            subcategory: None,
            image_url: None,
            base_price: Some(10.0),
            currency: Some("USD".to_string()),
            nutrition: None,
            customizations: None,
            selected_customizations: None,
            quantity: Some(1),
            item_note: None,
            calculated: None,
            components: None,
            availability: None,
            popularity: None,
        };
        
        OmsDocument::new(metadata, vendor, vec![item])
    }
    
    #[test]
    fn test_serialization() {
        let doc = create_test_document();
        let json = doc.to_json().unwrap();
        let parsed_doc = OmsDocument::from_json(&json).unwrap();
        
        assert_eq!(doc.vendor.id, parsed_doc.vendor.id);
        assert_eq!(doc.items[0].name, parsed_doc.items[0].name);
    }
    
    #[test]
    fn test_calculate_total_price() {
        let mut doc = create_test_document();
        
        // Test with one item
        let total = doc.calculate_total_price().unwrap();
        assert_eq!(total, 10.0);
        
        // Add another item
        let item2 = Item {
            id: "test-item-2".to_string(),
            name: "Test Item 2".to_string(),
            category: "test".to_string(),
            vendor_id: None,
            description: None,
            subcategory: None,
            image_url: None,
            base_price: Some(5.0),
            currency: Some("USD".to_string()),
            nutrition: None,
            customizations: None,
            selected_customizations: None,
            quantity: Some(2),
            item_note: None,
            calculated: None,
            components: None,
            availability: None,
            popularity: None,
        };
        
        doc.add_item(item2);
        
        // Test with two items
        let total = doc.calculate_total_price().unwrap();
        assert_eq!(total, 10.0 + (5.0 * 2.0));
    }
    
    #[test]
    fn test_create_url() {
        let doc = create_test_document();
        let url = doc.create_url().unwrap();
        assert_eq!(url, "omenu://order?v=test-vendor&i=test-item");
    }
    
    #[test]
    fn test_find_item() {
        let doc = create_test_document();
        
        // Test finding existing item
        let item = doc.find_item("test-item").unwrap();
        assert_eq!(item.name, "Test Item");
        
        // Test finding non-existent item
        let item = doc.find_item("nonexistent");
        assert!(item.is_none());
    }
    
    #[test]
    fn test_remove_item() {
        let mut doc = create_test_document();
        
        // Add another item
        let item2 = Item {
            id: "test-item-2".to_string(),
            name: "Test Item 2".to_string(),
            category: "test".to_string(),
            vendor_id: None,
            description: None,
            subcategory: None,
            image_url: None,
            base_price: None,
            currency: None,
            nutrition: None,
            customizations: None,
            selected_customizations: None,
            quantity: None,
            item_note: None,
            calculated: None,
            components: None,
            availability: None,
            popularity: None,
        };
        
        doc.add_item(item2);
        assert_eq!(doc.items.len(), 2);
        
        // Remove an item
        let result = doc.remove_item("test-item");
        assert!(result);
        assert_eq!(doc.items.len(), 1);
        assert_eq!(doc.items[0].id, "test-item-2");
        
        // Try to remove a non-existent item
        let result = doc.remove_item("nonexistent");
        assert!(!result);
        assert_eq!(doc.items.len(), 1);
    }
    
    #[test]
    fn test_set_order() {
        let mut doc = create_test_document();
        
        let order = Order {
            id: Some("test-order".to_string()),
            status: Some(OrderStatus::Draft),
            created: Some(Utc::now()),
            pickup_time: None,
            delivery_time: None,
            r#type: Some(OrderType::Pickup),
            customer_notes: None,
            payment: None,
            customer: None,
            delivery: None,
        };
        
        doc.set_order(order);
        
        assert!(doc.order.is_some());
        assert_eq!(doc.order.as_ref().unwrap().id, Some("test-order".to_string()));
    }
    
    #[test]
    fn test_update_order_status() {
        let mut doc = create_test_document();
        
        // Test updating when no order exists
        let result = doc.update_order_status(OrderStatus::Confirmed);
        assert!(result.is_err());
        
        // Add an order and test updating
        let order = Order {
            id: Some("test-order".to_string()),
            status: Some(OrderStatus::Draft),
            created: Some(Utc::now()),
            pickup_time: None,
            delivery_time: None,
            r#type: Some(OrderType::Pickup),
            customer_notes: None,
            payment: None,
            customer: None,
            delivery: None,
        };
        
        doc.set_order(order);
        
        let result = doc.update_order_status(OrderStatus::Confirmed);
        assert!(result.is_ok());
        assert_eq!(
            doc.order.as_ref().unwrap().status,
            Some(OrderStatus::Confirmed)
        );
    }
    
    #[test]
    fn test_extensions() {
        let mut doc = create_test_document();
        
        // Test adding an extension
        let data = serde_json::json!({
            "key": "value",
            "number": 42
        });
        
        doc.add_extension("com.example.test", data.clone());
        
        // Test getting the extension
        let ext = doc.get_extension("com.example.test").unwrap();
        assert_eq!(ext, &data);
        
        // Test getting a non-existent extension
        let ext = doc.get_extension("nonexistent");
        assert!(ext.is_none());
    }
}