incipit 0.2.3

Workspace identity vocabulary for Mere: the stable ids of a graph and a session, and view-local curation (subgraph specifications and folds), and, behind the `arrangement` feature, forme: the workbench arrangement authority. No dependency on the graph itself.
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright 2026 Mark Alan Boykin
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// SPDX-License-Identifier: MPL-2.0

//! View-local graph folds (mer3ly site plan, Rulings 153, 163 and 164).
//!
//! A fold is curation, not a source-graph group: it records which source
//! members one view draws as a single stand-in. Members are opaque strings,
//! so this crate stays serde-only (SE73); a host converts its own ids at its
//! edge. A host serving a scene lowers this record into `sceno::Fold`, which
//! has the same stand-in, rule and label.
//!
//! The wire is versioned. [`FOLD_RECORD_VERSION`] 1 was forme's UUID record
//! with no stand-in, rule or label; it still loads, migrated by
//! [`FoldRecordV1::migrate`] to a summary stand-in with neither. Any other
//! unknown version is refused rather than read with new semantics.

use std::fmt;

use serde::{Deserialize, Serialize};

/// The record's current wire version.
pub const FOLD_RECORD_VERSION: u16 = 2;

/// Stable identity of one fold, distinct from every source member. Opaque:
/// the host mints it (pictograph uses a UUID string).
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct FoldId(String);

impl FoldId {
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for FoldId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

/// How relations crossing a fold boundary read in the current projection.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum FoldBoundaryPolicy {
    /// Internal cells disappear. Boundary cells attach to the stand-in and
    /// bundle only with cells of the same relation family.
    #[default]
    BundleByRelationFamily,
}

/// A direction through relations, from the rule's root.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FoldDirection {
    Outgoing,
    Incoming,
}

/// What a view draws in the fold's place.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum FoldStandIn {
    /// One member stays drawn, carrying the "+N" of the others.
    Member(String),
    /// A synthetic body stands for every member.
    Summary {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        label: Option<String>,
    },
}

/// How the host chose the members. Recorded, never re-run here.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum FoldRule {
    Selection,
    /// `root` and everything reachable from it through relations of `family`,
    /// followed in `direction`.
    Descendants {
        root: String,
        family: String,
        direction: FoldDirection,
    },
}

/// One durable, view-local fold.
///
/// `members` are kept sorted and distinct so serialization, undo comparisons
/// and boundary accounting agree. `source_scope` is opaque: it lets a host
/// refuse a fold made against another graph, cursor or lens.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "FoldRecordWire")]
pub struct FoldRecord {
    pub version: u16,
    pub id: FoldId,
    pub source_scope: String,
    pub members: Vec<String>,
    pub stand_in: FoldStandIn,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rule: Option<FoldRule>,
    /// The host's own words for the fold. Never blank when present.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    pub boundary_policy: FoldBoundaryPolicy,
}

/// The version 1 record, as forme wrote it (UUIDs read as strings).
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct FoldRecordV1 {
    pub version: u16,
    pub id: FoldId,
    pub source_scope: String,
    pub members: Vec<String>,
    pub boundary_policy: FoldBoundaryPolicy,
}

impl FoldRecordV1 {
    /// The version 2 record a version 1 one means (Ruling 164): the same id,
    /// scope and members, a summary stand-in, no rule and no label.
    pub fn migrate(self) -> FoldRecord {
        FoldRecord {
            version: FOLD_RECORD_VERSION,
            id: self.id,
            source_scope: self.source_scope,
            members: self.members,
            stand_in: FoldStandIn::Summary { label: None },
            rule: None,
            label: None,
            boundary_policy: self.boundary_policy,
        }
    }
}

/// Why a fold record was refused.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FoldRecordError {
    UnsupportedVersion {
        found: u16,
    },
    /// A version 2 record names no stand-in.
    MissingStandIn,
    /// A version 1 record carries a field version 1 never had.
    NotVersionOne,
    TooFewMembers {
        count: usize,
    },
    /// Members are not sorted and distinct.
    Unordered,
    StandInNotMember(String),
    RootNotMember(String),
    BlankLabel,
}

impl fmt::Display for FoldRecordError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnsupportedVersion { found } => {
                write!(f, "fold record version {found} is not supported")
            },
            Self::MissingStandIn => write!(f, "a version 2 fold record names no stand-in"),
            Self::NotVersionOne => write!(
                f,
                "a version 1 fold record carries a stand-in, rule or label"
            ),
            Self::TooFewMembers { count } => {
                write!(f, "a fold needs at least two members, found {count}")
            },
            Self::Unordered => write!(f, "fold members are not sorted and distinct"),
            Self::StandInNotMember(id) => write!(f, "fold stand-in {id} is not a member"),
            Self::RootNotMember(id) => write!(f, "fold rule root {id} is not a member"),
            Self::BlankLabel => write!(f, "a fold's label is blank"),
        }
    }
}

impl std::error::Error for FoldRecordError {}

impl FoldRecord {
    /// A fold over `members`, sorted and deduplicated. Refused when fewer
    /// than two distinct members remain, or when the stand-in, the rule's
    /// root or the label does not hold.
    pub fn new(
        id: FoldId,
        source_scope: impl Into<String>,
        members: impl IntoIterator<Item = impl Into<String>>,
        stand_in: FoldStandIn,
        rule: Option<FoldRule>,
        label: Option<String>,
    ) -> Result<Self, FoldRecordError> {
        let mut members: Vec<String> = members.into_iter().map(Into::into).collect();
        members.sort();
        members.dedup();
        let record = Self {
            version: FOLD_RECORD_VERSION,
            id,
            source_scope: source_scope.into(),
            members,
            stand_in,
            rule,
            label,
            boundary_policy: FoldBoundaryPolicy::BundleByRelationFamily,
        };
        record.validate()?;
        Ok(record)
    }

    /// A selection fold drawn as an unlabelled summary.
    pub fn selection(
        id: FoldId,
        source_scope: impl Into<String>,
        members: impl IntoIterator<Item = impl Into<String>>,
    ) -> Result<Self, FoldRecordError> {
        Self::new(
            id,
            source_scope,
            members,
            FoldStandIn::Summary { label: None },
            Some(FoldRule::Selection),
            None,
        )
    }

    /// Check the record alone; whether its members exist is the host's check.
    pub fn validate(&self) -> Result<(), FoldRecordError> {
        if self.version != FOLD_RECORD_VERSION {
            return Err(FoldRecordError::UnsupportedVersion {
                found: self.version,
            });
        }
        if self.members.len() < 2 {
            return Err(FoldRecordError::TooFewMembers {
                count: self.members.len(),
            });
        }
        if self.members.windows(2).any(|pair| pair[0] >= pair[1]) {
            return Err(FoldRecordError::Unordered);
        }
        if let FoldStandIn::Member(member) = &self.stand_in
            && !self.contains(member)
        {
            return Err(FoldRecordError::StandInNotMember(member.clone()));
        }
        if let Some(FoldRule::Descendants { root, .. }) = &self.rule
            && !self.contains(root)
        {
            return Err(FoldRecordError::RootNotMember(root.clone()));
        }
        if self
            .label
            .as_ref()
            .is_some_and(|label| label.trim().is_empty())
        {
            return Err(FoldRecordError::BlankLabel);
        }
        Ok(())
    }

    /// Whether the fold has `member` among its members.
    pub fn contains(&self, member: &str) -> bool {
        self.members
            .binary_search_by(|probe| probe.as_str().cmp(member))
            .is_ok()
    }

    /// The member that stays drawn, when the stand-in is a member.
    pub fn stand_in_member(&self) -> Option<&str> {
        match &self.stand_in {
            FoldStandIn::Member(member) => Some(member),
            FoldStandIn::Summary { .. } => None,
        }
    }

    /// The members the fold hides: all but a member stand-in.
    pub fn hidden(&self) -> impl Iterator<Item = &str> {
        let keep = self.stand_in_member();
        self.members
            .iter()
            .map(String::as_str)
            .filter(move |member| Some(*member) != keep)
    }

    /// The "+N" the stand-in carries.
    pub fn hidden_count(&self) -> usize {
        self.hidden().count()
    }

    /// Whether two folds share a member. Active folds never do.
    pub fn overlaps(&self, other: &FoldRecord) -> bool {
        self.members.iter().any(|member| other.contains(member))
    }
}

/// Every version's fields, read before the version picks a meaning.
#[derive(Deserialize)]
struct FoldRecordWire {
    version: u16,
    id: FoldId,
    source_scope: String,
    members: Vec<String>,
    #[serde(default)]
    stand_in: Option<FoldStandIn>,
    #[serde(default)]
    rule: Option<FoldRule>,
    #[serde(default)]
    label: Option<String>,
    boundary_policy: FoldBoundaryPolicy,
}

impl TryFrom<FoldRecordWire> for FoldRecord {
    type Error = FoldRecordError;

    fn try_from(wire: FoldRecordWire) -> Result<Self, Self::Error> {
        let record = match wire.version {
            1 => {
                if wire.stand_in.is_some() || wire.rule.is_some() || wire.label.is_some() {
                    return Err(FoldRecordError::NotVersionOne);
                }
                FoldRecordV1 {
                    version: 1,
                    id: wire.id,
                    source_scope: wire.source_scope,
                    members: wire.members,
                    boundary_policy: wire.boundary_policy,
                }
                .migrate()
            },
            FOLD_RECORD_VERSION => Self {
                version: FOLD_RECORD_VERSION,
                id: wire.id,
                source_scope: wire.source_scope,
                members: wire.members,
                stand_in: wire.stand_in.ok_or(FoldRecordError::MissingStandIn)?,
                rule: wire.rule,
                label: wire.label,
                boundary_policy: wire.boundary_policy,
            },
            found => return Err(FoldRecordError::UnsupportedVersion { found }),
        };
        record.validate()?;
        Ok(record)
    }
}

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

    const A: &str = "00000000-0000-0000-0000-000000000001";
    const B: &str = "00000000-0000-0000-0000-000000000002";
    const C: &str = "00000000-0000-0000-0000-000000000003";

    fn id() -> FoldId {
        FoldId::new("00000000-0000-0000-0000-0000000000f0")
    }

    #[test]
    fn a_selection_canonicalizes_its_members() {
        let fold = FoldRecord::selection(id(), "graph:local", [C, A, C]).expect("two members");
        assert_eq!(fold.version, FOLD_RECORD_VERSION);
        assert_eq!(fold.members, vec![A, C]);
        assert!(fold.contains(A) && !fold.contains(B));
        assert_eq!(fold.rule, Some(FoldRule::Selection));
        assert_eq!(fold.hidden_count(), 2, "a summary hides every member");
        assert_eq!(
            FoldRecord::selection(id(), "graph:local", [A, A]),
            Err(FoldRecordError::TooFewMembers { count: 1 })
        );
    }

    #[test]
    fn a_member_stand_in_counts_the_others() {
        let fold = FoldRecord::new(
            id(),
            "graph:local",
            [A, B, C],
            FoldStandIn::Member(A.into()),
            Some(FoldRule::Descendants {
                root: A.into(),
                family: "containment".into(),
                direction: FoldDirection::Outgoing,
            }),
            Some("A's contents".into()),
        )
        .expect("valid");
        assert_eq!(fold.hidden().collect::<Vec<_>>(), vec![B, C]);
        assert_eq!(fold.hidden_count(), 2);
        let other = FoldRecord::selection(id(), "graph:local", [C, "z"]).unwrap();
        assert!(fold.overlaps(&other));
    }

    #[test]
    fn invalid_records_are_refused() {
        let stray = FoldRecord::new(id(), "s", [A, B], FoldStandIn::Member(C.into()), None, None);
        assert_eq!(stray, Err(FoldRecordError::StandInNotMember(C.into())));
        let rootless = FoldRecord::new(
            id(),
            "s",
            [A, B],
            FoldStandIn::Summary { label: None },
            Some(FoldRule::Descendants {
                root: C.into(),
                family: "containment".into(),
                direction: FoldDirection::Incoming,
            }),
            None,
        );
        assert_eq!(rootless, Err(FoldRecordError::RootNotMember(C.into())));
        let blank = FoldRecord::new(
            id(),
            "s",
            [A, B],
            FoldStandIn::Summary { label: None },
            None,
            Some(" ".into()),
        );
        assert_eq!(blank, Err(FoldRecordError::BlankLabel));
        let mut unordered = FoldRecord::selection(id(), "s", [A, B]).unwrap();
        unordered.members.reverse();
        assert_eq!(unordered.validate(), Err(FoldRecordError::Unordered));
    }

    #[test]
    fn version_two_round_trips_with_its_wire_pinned() {
        let fold = FoldRecord::selection(id(), "graph:local", [A, B]).unwrap();
        let wire = serde_json::to_string(&fold).unwrap();
        assert_eq!(
            wire,
            format!(
                r#"{{"version":2,"id":"{}","source_scope":"graph:local","members":["{A}","{B}"],"stand_in":{{"Summary":{{}}}},"rule":"Selection","boundary_policy":"BundleByRelationFamily"}}"#,
                id()
            ),
            "absent label stays off the wire"
        );
        assert_eq!(serde_json::from_str::<FoldRecord>(&wire).unwrap(), fold);
    }

    /// Ruling 164: a saved version 1 record loads as a summary with no rule
    /// and no label, keeping its id and members, and writes back as version 2.
    #[test]
    fn a_saved_version_one_record_migrates_on_load() {
        let v1 = format!(
            r#"{{"version":1,"id":"{}","source_scope":"session:alpha/graph:current","members":["{A}","{C}"],"boundary_policy":"BundleByRelationFamily"}}"#,
            id()
        );
        let fold: FoldRecord = serde_json::from_str(&v1).expect("v1 loads");
        assert_eq!(
            fold,
            FoldRecord {
                version: 2,
                id: id(),
                source_scope: "session:alpha/graph:current".into(),
                members: vec![A.into(), C.into()],
                stand_in: FoldStandIn::Summary { label: None },
                rule: None,
                label: None,
                boundary_policy: FoldBoundaryPolicy::BundleByRelationFamily,
            }
        );
        assert!(fold.validate().is_ok());
        let written = serde_json::to_string(&fold).unwrap();
        assert!(written.starts_with(r#"{"version":2,"#), "{written}");
        let explicit: FoldRecordV1 = serde_json::from_str(&v1).unwrap();
        assert_eq!(explicit.migrate(), fold);
    }

    #[test]
    fn unknown_or_mixed_versions_are_refused() {
        let base = format!(
            r#""id":"{}","source_scope":"s","members":["{A}","{B}"],"boundary_policy":"BundleByRelationFamily""#,
            id()
        );
        let read = |json: String| serde_json::from_str::<FoldRecord>(&json).map(|_| ());
        let error = read(format!(r#"{{"version":3,{base}}}"#)).unwrap_err();
        assert!(error.to_string().contains("version 3"), "{error}");
        let error = read(format!(r#"{{"version":2,{base}}}"#)).unwrap_err();
        assert!(error.to_string().contains("no stand-in"), "{error}");
        let error = read(format!(
            r#"{{"version":1,{base},"stand_in":{{"Summary":{{}}}}}}"#
        ))
        .unwrap_err();
        assert!(error.to_string().contains("version 1"), "{error}");
    }

    #[test]
    fn invalid_saved_members_are_refused_before_and_after_migration() {
        for version in [1, 2] {
            for members in [vec![A], vec![B, A], vec![A, A]] {
                let mut wire = serde_json::json!({
                    "version": version, "id": "saved-fold", "source_scope": "s",
                    "members": members, "boundary_policy": "BundleByRelationFamily"
                });
                if version == 2 {
                    wire["stand_in"] = serde_json::json!({"Summary": {}});
                }
                assert!(serde_json::from_value::<FoldRecord>(wire).is_err());
            }
        }
    }
}