grc-20 0.4.0

GRC-20 v2 binary property graph format for decentralized knowledge networks
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
444
445
446
447
448
449
450
451
452
453
454
//! Operation types for GRC-20 state changes.
//!
//! All state changes in GRC-20 are expressed as operations (ops).

use std::borrow::Cow;

use crate::model::{Context, Id, PropertyValue};

/// An atomic operation that modifies graph state (spec Section 3.1).
#[derive(Debug, Clone, PartialEq)]
pub enum Op<'a> {
    CreateEntity(CreateEntity<'a>),
    UpdateEntity(UpdateEntity<'a>),
    DeleteEntity(DeleteEntity),
    RestoreEntity(RestoreEntity),
    CreateRelation(CreateRelation<'a>),
    UpdateRelation(UpdateRelation<'a>),
    DeleteRelation(DeleteRelation),
    RestoreRelation(RestoreRelation),
    CreateValueRef(CreateValueRef),
}

impl Op<'_> {
    /// Returns the op type code for wire encoding.
    pub fn op_type(&self) -> u8 {
        match self {
            Op::CreateEntity(_) => 1,
            Op::UpdateEntity(_) => 2,
            Op::DeleteEntity(_) => 3,
            Op::RestoreEntity(_) => 4,
            Op::CreateRelation(_) => 5,
            Op::UpdateRelation(_) => 6,
            Op::DeleteRelation(_) => 7,
            Op::RestoreRelation(_) => 8,
            Op::CreateValueRef(_) => 9,
        }
    }
}

/// Creates a new entity (spec Section 3.2).
///
/// If the entity does not exist, creates it. If it already exists,
/// this acts as an update: values are applied as set_properties (LWW).
#[derive(Debug, Clone, PartialEq)]
pub struct CreateEntity<'a> {
    /// The entity's unique identifier.
    pub id: Id,
    /// Initial values for the entity.
    pub values: Vec<PropertyValue<'a>>,
    /// Optional context for grouping changes (spec Section 4.5).
    pub context: Option<Context>,
}

/// Updates an existing entity (spec Section 3.2).
///
/// Application order within op:
/// 1. unset_values
/// 2. set_properties
#[derive(Debug, Clone, PartialEq, Default)]
pub struct UpdateEntity<'a> {
    /// The entity to update.
    pub id: Id,
    /// Replace value for these properties (LWW).
    pub set_properties: Vec<PropertyValue<'a>>,
    /// Clear values for these properties (optionally specific language for TEXT).
    pub unset_values: Vec<UnsetValue>,
    /// Optional context for grouping changes (spec Section 4.5).
    pub context: Option<Context>,
}

/// Specifies which language slot to clear for an UnsetValue.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnsetLanguage {
    /// Clear all language slots (wire format: 0xFFFFFFFF).
    All,
    /// Clear only the English slot (wire format: 0).
    English,
    /// Clear a specific language slot (wire format: 1+).
    Specific(Id),
}

impl Default for UnsetLanguage {
    fn default() -> Self {
        Self::All
    }
}

/// Specifies a value to unset, with optional language targeting (TEXT only).
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct UnsetValue {
    /// The property whose value to clear.
    pub property: Id,
    /// Which language slot(s) to clear.
    /// For TEXT properties: All clears all slots, English clears English slot,
    ///   Specific clears a specific language slot.
    /// For non-TEXT properties: must be All.
    pub language: UnsetLanguage,
}

impl UnsetValue {
    /// Creates an UnsetValue that clears all values for a property.
    pub fn all(property: Id) -> Self {
        Self { property, language: UnsetLanguage::All }
    }

    /// Creates an UnsetValue that clears the English slot for a TEXT property.
    pub fn english(property: Id) -> Self {
        Self { property, language: UnsetLanguage::English }
    }

    /// Creates an UnsetValue that clears a specific language for a TEXT property.
    pub fn language(property: Id, language: Id) -> Self {
        Self { property, language: UnsetLanguage::Specific(language) }
    }
}

impl<'a> UpdateEntity<'a> {
    /// Creates a new UpdateEntity for the given entity ID.
    pub fn new(id: Id) -> Self {
        Self {
            id,
            set_properties: Vec::new(),
            unset_values: Vec::new(),
            context: None,
        }
    }

    /// Returns true if this update has no actual changes.
    pub fn is_empty(&self) -> bool {
        self.set_properties.is_empty() && self.unset_values.is_empty()
    }
}


/// Deletes an entity (spec Section 3.2).
///
/// Transitions the entity to DELETED state. Subsequent updates are ignored
/// until restored via RestoreEntity.
#[derive(Debug, Clone, PartialEq)]
pub struct DeleteEntity {
    /// The entity to delete.
    pub id: Id,
    /// Optional context for grouping changes (spec Section 4.5).
    pub context: Option<Context>,
}

/// Restores a deleted entity (spec Section 3.2).
///
/// Transitions a DELETED entity back to ACTIVE state.
/// If the entity is ACTIVE or does not exist, this is a no-op.
#[derive(Debug, Clone, PartialEq)]
pub struct RestoreEntity {
    /// The entity to restore.
    pub id: Id,
    /// Optional context for grouping changes (spec Section 4.5).
    pub context: Option<Context>,
}

/// Creates a new relation (spec Section 3.3).
///
/// Also implicitly creates the reified entity if it doesn't exist.
#[derive(Debug, Clone, PartialEq)]
pub struct CreateRelation<'a> {
    /// The relation's unique identifier.
    pub id: Id,
    /// The relation type entity ID.
    pub relation_type: Id,
    /// Source entity or value ref ID.
    pub from: Id,
    /// If true, `from` is a value ref ID (inline encoding).
    /// If false, `from` is an entity ID (ObjectRef encoding).
    pub from_is_value_ref: bool,
    /// Optional space pin for source entity.
    pub from_space: Option<Id>,
    /// Optional version (edit ID) to pin source entity.
    pub from_version: Option<Id>,
    /// Target entity or value ref ID.
    pub to: Id,
    /// If true, `to` is a value ref ID (inline encoding).
    /// If false, `to` is an entity ID (ObjectRef encoding).
    pub to_is_value_ref: bool,
    /// Optional space pin for target entity.
    pub to_space: Option<Id>,
    /// Optional version (edit ID) to pin target entity.
    pub to_version: Option<Id>,
    /// Explicit reified entity ID.
    /// If None, entity ID is auto-derived from the relation ID.
    pub entity: Option<Id>,
    /// Optional ordering position (fractional indexing).
    pub position: Option<Cow<'a, str>>,
    /// Optional context for grouping changes (spec Section 4.5).
    pub context: Option<Context>,
}

impl CreateRelation<'_> {
    /// Computes the reified entity ID.
    ///
    /// If explicit entity is provided, returns it.
    /// Otherwise, derives it from the relation ID.
    pub fn entity_id(&self) -> Id {
        use crate::model::id::relation_entity_id;
        match self.entity {
            Some(id) => id,
            None => relation_entity_id(&self.id),
        }
    }

    /// Returns true if this relation has an explicit entity ID.
    pub fn has_explicit_entity(&self) -> bool {
        self.entity.is_some()
    }
}

/// Fields that can be unset on a relation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UnsetRelationField {
    FromSpace,
    FromVersion,
    ToSpace,
    ToVersion,
    Position,
}

/// Updates a relation's mutable fields (spec Section 3.3).
///
/// The structural fields (entity, type, from, to) are immutable.
/// The space pins, version pins, and position can be updated or unset.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct UpdateRelation<'a> {
    /// The relation to update.
    pub id: Id,
    /// Set space pin for source entity.
    pub from_space: Option<Id>,
    /// Set version pin for source entity.
    pub from_version: Option<Id>,
    /// Set space pin for target entity.
    pub to_space: Option<Id>,
    /// Set version pin for target entity.
    pub to_version: Option<Id>,
    /// Set position for ordering.
    pub position: Option<Cow<'a, str>>,
    /// Fields to clear/unset.
    pub unset: Vec<UnsetRelationField>,
    /// Optional context for grouping changes (spec Section 4.5).
    pub context: Option<Context>,
}

impl UpdateRelation<'_> {
    /// Creates a new UpdateRelation for the given relation ID.
    pub fn new(id: Id) -> Self {
        Self {
            id,
            from_space: None,
            from_version: None,
            to_space: None,
            to_version: None,
            position: None,
            unset: Vec::new(),
            context: None,
        }
    }

    /// Returns true if this update has no actual changes.
    pub fn is_empty(&self) -> bool {
        self.from_space.is_none()
            && self.from_version.is_none()
            && self.to_space.is_none()
            && self.to_version.is_none()
            && self.position.is_none()
            && self.unset.is_empty()
    }
}

/// Deletes a relation (spec Section 3.3).
///
/// Transitions the relation to DELETED state. Does NOT delete the reified entity.
/// Subsequent updates are ignored until restored via RestoreRelation.
#[derive(Debug, Clone, PartialEq)]
pub struct DeleteRelation {
    /// The relation to delete.
    pub id: Id,
    /// Optional context for grouping changes (spec Section 4.5).
    pub context: Option<Context>,
}

/// Restores a deleted relation (spec Section 3.3).
///
/// Transitions a DELETED relation back to ACTIVE state.
/// If the relation is ACTIVE or does not exist, this is a no-op.
#[derive(Debug, Clone, PartialEq)]
pub struct RestoreRelation {
    /// The relation to restore.
    pub id: Id,
    /// Optional context for grouping changes (spec Section 4.5).
    pub context: Option<Context>,
}

/// Creates a referenceable ID for a value slot (spec Section 3.4).
///
/// This enables relations to target specific values for provenance,
/// confidence, attribution, or other qualifiers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateValueRef {
    /// The value ref's unique identifier.
    pub id: Id,
    /// The entity holding the value.
    pub entity: Id,
    /// The property of the value.
    pub property: Id,
    /// The language (TEXT values only).
    pub language: Option<Id>,
    /// The space containing the value (default: current space).
    pub space: Option<Id>,
}

/// Validates a position string according to spec rules.
///
/// Position strings must:
/// - Not be empty
/// - Only contain characters 0-9, A-Z, a-z (62 chars, ASCII order)
/// - Not exceed 64 characters
pub fn validate_position(pos: &str) -> Result<(), &'static str> {
    if pos.is_empty() {
        return Err("position cannot be empty");
    }
    if pos.len() > 64 {
        return Err("position exceeds 64 characters");
    }
    for c in pos.chars() {
        if !c.is_ascii_alphanumeric() {
            return Err("position contains invalid character");
        }
    }
    Ok(())
}

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

    #[test]
    fn test_op_type_codes() {
        assert_eq!(
            Op::CreateEntity(CreateEntity {
                id: [0; 16],
                values: vec![],
                context: None,
            })
            .op_type(),
            1
        );
        assert_eq!(Op::UpdateEntity(UpdateEntity::new([0; 16])).op_type(), 2);
        assert_eq!(Op::DeleteEntity(DeleteEntity { id: [0; 16], context: None }).op_type(), 3);
    }

    #[test]
    fn test_validate_position() {
        assert!(validate_position("abc123").is_ok());
        assert!(validate_position("aV").is_ok());
        assert!(validate_position("a").is_ok());

        // Empty is not allowed
        assert!(validate_position("").is_err());

        // Invalid characters
        assert!(validate_position("abc-123").is_err());
        assert!(validate_position("abc_123").is_err());
        assert!(validate_position("abc 123").is_err());

        // Too long (65 chars)
        let long = "a".repeat(65);
        assert!(validate_position(&long).is_err());

        // Exactly 64 chars is ok
        let exact = "a".repeat(64);
        assert!(validate_position(&exact).is_ok());
    }

    #[test]
    fn test_update_entity_is_empty() {
        let update = UpdateEntity::new([0; 16]);
        assert!(update.is_empty());

        let mut update2 = UpdateEntity::new([0; 16]);
        update2.set_properties.push(PropertyValue {
            property: [1; 16],
            value: crate::model::Value::Boolean(true),
        });
        assert!(!update2.is_empty());
    }

    #[test]
    fn test_entity_id_derivation() {
        use crate::model::id::relation_entity_id;

        let rel_id = [5u8; 16];
        let from = [1u8; 16];
        let to = [2u8; 16];
        let rel_type = [3u8; 16];

        // Auto-derived entity (entity = None)
        let rel_auto = CreateRelation {
            id: rel_id,
            relation_type: rel_type,
            from,
            from_is_value_ref: false,
            to,
            to_is_value_ref: false,
            entity: None,
            position: None,
            from_space: None,
            from_version: None,
            to_space: None,
            to_version: None,
            context: None,
        };
        assert_eq!(rel_auto.entity_id(), relation_entity_id(&rel_id));
        assert!(!rel_auto.has_explicit_entity());

        // Explicit entity
        let explicit_entity = [6u8; 16];
        let rel_explicit = CreateRelation {
            id: rel_id,
            relation_type: rel_type,
            from,
            from_is_value_ref: false,
            to,
            to_is_value_ref: false,
            entity: Some(explicit_entity),
            position: None,
            from_space: None,
            from_version: None,
            to_space: None,
            to_version: None,
            context: None,
        };
        assert_eq!(rel_explicit.entity_id(), explicit_entity);
        assert!(rel_explicit.has_explicit_entity());
    }

    #[test]
    fn test_update_relation_is_empty() {
        let update = UpdateRelation::new([0; 16]);
        assert!(update.is_empty());

        let mut update2 = UpdateRelation::new([0; 16]);
        update2.from_space = Some([1; 16]);
        assert!(!update2.is_empty());

        let mut update3 = UpdateRelation::new([0; 16]);
        update3.unset.push(UnsetRelationField::Position);
        assert!(!update3.is_empty());
    }
}