structfs-core-store 0.2.0

Core StructFS store traits - Record, Value, Path, Format
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
//! The Reference pattern - a placeholder for a value at another path.
//!
//! References enable lazy loading, pagination, and shallow responses while
//! maintaining consistency with the underlying data model. They are the key
//! to HATEOAS in StructFS: clients discover and navigate the API by following
//! embedded references, not by constructing paths from out-of-band knowledge.

use std::collections::BTreeMap;

use crate::Value;

/// Type information for a referenced value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeInfo {
    /// Type name (e.g., "integer", "string", "handle", "action", "collection").
    pub name: String,

    /// Optional schema describing the structure of the type.
    pub schema: Option<BTreeMap<String, TypeDescriptor>>,
}

impl TypeInfo {
    /// Create a TypeInfo with just a name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            schema: None,
        }
    }

    /// Convert to a Value::Map representation.
    pub fn to_value(&self) -> Value {
        let mut map = BTreeMap::new();
        map.insert("name".to_string(), Value::String(self.name.clone()));

        if let Some(ref schema) = self.schema {
            let schema_map: BTreeMap<String, Value> = schema
                .iter()
                .map(|(k, v)| (k.clone(), v.to_value()))
                .collect();
            map.insert("schema".to_string(), Value::Map(schema_map));
        }

        Value::Map(map)
    }

    /// Try to parse a TypeInfo from a Value.
    pub fn from_value(value: &Value) -> Option<Self> {
        let map = match value {
            Value::Map(m) => m,
            _ => return None,
        };

        let name = match map.get("name") {
            Some(Value::String(s)) => s.clone(),
            _ => return None,
        };

        let schema = map.get("schema").and_then(|v| {
            if let Value::Map(schema_map) = v {
                let mut result = BTreeMap::new();
                for (k, v) in schema_map {
                    if let Some(td) = TypeDescriptor::from_value(v) {
                        result.insert(k.clone(), td);
                    }
                }
                Some(result)
            } else {
                None
            }
        });

        Some(Self { name, schema })
    }
}

/// Describes a field's type within a schema.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeDescriptor {
    /// The type of this field.
    pub type_info: TypeInfo,
}

impl TypeDescriptor {
    /// Create a TypeDescriptor from a type name.
    pub fn new(type_name: impl Into<String>) -> Self {
        Self {
            type_info: TypeInfo::new(type_name),
        }
    }

    /// Convert to a Value::Map representation.
    pub fn to_value(&self) -> Value {
        let mut map = BTreeMap::new();
        map.insert("type".to_string(), self.type_info.to_value());
        Value::Map(map)
    }

    /// Try to parse a TypeDescriptor from a Value.
    pub fn from_value(value: &Value) -> Option<Self> {
        let map = match value {
            Value::Map(m) => m,
            _ => return None,
        };

        let type_info = map.get("type").and_then(TypeInfo::from_value)?;

        Some(Self { type_info })
    }
}

/// A reference to a value at another path.
///
/// References are the foundation of HATEOAS in StructFS. Instead of embedding
/// full values or documenting path construction rules, stores return references
/// that clients can follow.
///
/// # Examples
///
/// ```
/// use structfs_core_store::Reference;
///
/// // Minimal reference
/// let r = Reference::new("handles/0");
///
/// // Reference with type hint
/// let r = Reference::with_type("handles/0", "handle");
///
/// // Reference to an action
/// let r = Reference::with_type("meta/open", "action");
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Reference {
    /// The path to the referenced value.
    pub path: String,

    /// Optional type information.
    pub type_info: Option<TypeInfo>,
}

impl Reference {
    /// Create a minimal reference with just a path.
    pub fn new(path: impl Into<String>) -> Self {
        Self {
            path: path.into(),
            type_info: None,
        }
    }

    /// Create a reference with a type name.
    pub fn with_type(path: impl Into<String>, type_name: impl Into<String>) -> Self {
        Self {
            path: path.into(),
            type_info: Some(TypeInfo::new(type_name)),
        }
    }

    /// Convert to a Value::Map representation.
    ///
    /// This produces the canonical reference format:
    /// ```json
    /// {"path": "handles/0", "type": {"name": "handle"}}
    /// ```
    pub fn to_value(&self) -> Value {
        let mut map = BTreeMap::new();
        map.insert("path".to_string(), Value::String(self.path.clone()));

        if let Some(ref ti) = self.type_info {
            map.insert("type".to_string(), ti.to_value());
        }

        Value::Map(map)
    }

    /// Try to parse a Reference from a Value.
    ///
    /// A value is a reference if it's a map containing a `path` key whose
    /// value is a string.
    pub fn from_value(value: &Value) -> Option<Self> {
        let map = match value {
            Value::Map(m) => m,
            _ => return None,
        };

        let path = match map.get("path") {
            Some(Value::String(s)) => s.clone(),
            _ => return None,
        };

        let type_info = map.get("type").and_then(TypeInfo::from_value);

        Some(Self { path, type_info })
    }

    /// Check if a Value is a reference.
    ///
    /// A value is a reference if it's a map containing a `path` key whose
    /// value is a string.
    pub fn is_reference(value: &Value) -> bool {
        match value {
            Value::Map(m) => matches!(m.get("path"), Some(Value::String(_))),
            _ => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn reference_new() {
        let r = Reference::new("handles/0");
        assert_eq!(r.path, "handles/0");
        assert!(r.type_info.is_none());
    }

    #[test]
    fn reference_with_type() {
        let r = Reference::with_type("handles/0", "handle");
        assert_eq!(r.path, "handles/0");
        assert_eq!(r.type_info.as_ref().unwrap().name, "handle");
    }

    #[test]
    fn reference_to_value_minimal() {
        let r = Reference::new("handles/0");
        let v = r.to_value();

        if let Value::Map(map) = v {
            assert_eq!(map.get("path"), Some(&Value::String("handles/0".into())));
            assert!(!map.contains_key("type"));
        } else {
            panic!("Expected map");
        }
    }

    #[test]
    fn reference_to_value_with_type() {
        let r = Reference::with_type("handles/0", "handle");
        let v = r.to_value();

        if let Value::Map(map) = v {
            assert_eq!(map.get("path"), Some(&Value::String("handles/0".into())));
            if let Some(Value::Map(type_map)) = map.get("type") {
                assert_eq!(type_map.get("name"), Some(&Value::String("handle".into())));
            } else {
                panic!("Expected type map");
            }
        } else {
            panic!("Expected map");
        }
    }

    #[test]
    fn reference_from_value_minimal() {
        let mut map = BTreeMap::new();
        map.insert("path".to_string(), Value::String("handles/0".into()));
        let v = Value::Map(map);

        let r = Reference::from_value(&v).unwrap();
        assert_eq!(r.path, "handles/0");
        assert!(r.type_info.is_none());
    }

    #[test]
    fn reference_from_value_with_type() {
        let mut type_map = BTreeMap::new();
        type_map.insert("name".to_string(), Value::String("handle".into()));

        let mut map = BTreeMap::new();
        map.insert("path".to_string(), Value::String("handles/0".into()));
        map.insert("type".to_string(), Value::Map(type_map));
        let v = Value::Map(map);

        let r = Reference::from_value(&v).unwrap();
        assert_eq!(r.path, "handles/0");
        assert_eq!(r.type_info.as_ref().unwrap().name, "handle");
    }

    #[test]
    fn reference_from_value_not_map() {
        assert!(Reference::from_value(&Value::String("x".into())).is_none());
    }

    #[test]
    fn reference_from_value_no_path() {
        let mut map = BTreeMap::new();
        map.insert("other".to_string(), Value::String("x".into()));
        assert!(Reference::from_value(&Value::Map(map)).is_none());
    }

    #[test]
    fn reference_from_value_path_not_string() {
        let mut map = BTreeMap::new();
        map.insert("path".to_string(), Value::Integer(42));
        assert!(Reference::from_value(&Value::Map(map)).is_none());
    }

    #[test]
    fn is_reference_true() {
        let mut map = BTreeMap::new();
        map.insert("path".to_string(), Value::String("x".into()));
        assert!(Reference::is_reference(&Value::Map(map)));
    }

    #[test]
    fn is_reference_false_not_map() {
        assert!(!Reference::is_reference(&Value::String("x".into())));
    }

    #[test]
    fn is_reference_false_no_path() {
        let mut map = BTreeMap::new();
        map.insert("other".to_string(), Value::String("x".into()));
        assert!(!Reference::is_reference(&Value::Map(map)));
    }

    #[test]
    fn is_reference_false_path_not_string() {
        let mut map = BTreeMap::new();
        map.insert("path".to_string(), Value::Integer(42));
        assert!(!Reference::is_reference(&Value::Map(map)));
    }

    #[test]
    fn reference_roundtrip() {
        let original = Reference::with_type("meta/handles/0", "handle");
        let value = original.to_value();
        let parsed = Reference::from_value(&value).unwrap();
        assert_eq!(original, parsed);
    }

    #[test]
    fn type_info_new() {
        let ti = TypeInfo::new("handle");
        assert_eq!(ti.name, "handle");
        assert!(ti.schema.is_none());
    }

    #[test]
    fn type_info_to_value() {
        let ti = TypeInfo::new("integer");
        let v = ti.to_value();

        if let Value::Map(map) = v {
            assert_eq!(map.get("name"), Some(&Value::String("integer".into())));
        } else {
            panic!("Expected map");
        }
    }

    #[test]
    fn type_info_with_schema() {
        let mut schema = BTreeMap::new();
        schema.insert("position".to_string(), TypeDescriptor::new("integer"));

        let ti = TypeInfo {
            name: "handle".to_string(),
            schema: Some(schema),
        };

        let v = ti.to_value();
        if let Value::Map(map) = v {
            assert!(map.contains_key("schema"));
        } else {
            panic!("Expected map");
        }
    }

    #[test]
    fn type_info_from_value() {
        let mut map = BTreeMap::new();
        map.insert("name".to_string(), Value::String("handle".into()));
        let v = Value::Map(map);

        let ti = TypeInfo::from_value(&v).unwrap();
        assert_eq!(ti.name, "handle");
    }

    #[test]
    fn type_info_from_value_not_map() {
        assert!(TypeInfo::from_value(&Value::String("x".into())).is_none());
    }

    #[test]
    fn type_info_from_value_no_name() {
        let map = BTreeMap::new();
        assert!(TypeInfo::from_value(&Value::Map(map)).is_none());
    }

    #[test]
    fn type_descriptor_new() {
        let td = TypeDescriptor::new("string");
        assert_eq!(td.type_info.name, "string");
    }

    #[test]
    fn type_descriptor_to_value() {
        let td = TypeDescriptor::new("integer");
        let v = td.to_value();

        if let Value::Map(map) = v {
            assert!(map.contains_key("type"));
        } else {
            panic!("Expected map");
        }
    }

    #[test]
    fn type_descriptor_from_value() {
        let mut type_map = BTreeMap::new();
        type_map.insert("name".to_string(), Value::String("integer".into()));

        let mut map = BTreeMap::new();
        map.insert("type".to_string(), Value::Map(type_map));

        let td = TypeDescriptor::from_value(&Value::Map(map)).unwrap();
        assert_eq!(td.type_info.name, "integer");
    }

    #[test]
    fn type_descriptor_from_value_not_map() {
        assert!(TypeDescriptor::from_value(&Value::String("x".into())).is_none());
    }

    #[test]
    fn type_descriptor_from_value_no_type() {
        let map = BTreeMap::new();
        assert!(TypeDescriptor::from_value(&Value::Map(map)).is_none());
    }
}