gantz_core 0.4.1

The core types and traits for gantz, an environment for creative systems.
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
//! A node that looks up its own implementation via content address.

use crate::datum::{self, Datum, DatumError};
use crate::{
    node::{self, Node},
    visit,
};
use serde::de::{self, Deserialize, DeserializeOwned, Deserializer, Visitor};
use serde::ser::{Serialize, SerializeMap, Serializer};
use std::collections::BTreeMap;
use std::fmt;

/// A node-set hook exposing the underlying [`Ref`] when a node is
/// transparently a reference to another graph (a bare [`Ref`], or a wrapper
/// such as gantz_egui's `NamedRef`).
///
/// Deliberately *not* implemented by function-value wrappers
/// ([`Fn`](crate::node::Fn)), which reference a graph without standing in for
/// it within their parent.
pub trait AsRefNode {
    /// The underlying [`Ref`], if this node is transparently a reference.
    fn as_ref_node(&self) -> Option<&Ref>;
}

/// A node that refers to another node in the environment by content address.
///
/// A reference optionally carries domain-extension data in `ext`: canonical
/// [`Datum`]s keyed by a domain-prefixed string (e.g. `"plyphon.dsp-ref"`).
/// Ext data serializes and content-addresses with the node, losslessly even
/// in applications that do not know the owning domain. Conventions for ext
/// entries (see [`Ref::set_ext`]):
///
/// - Store only non-default data, so a default-configured reference keeps the
///   address it would have without the entry.
/// - One value type per key, owned by one domain.
/// - Ext data must not carry graph references: dependency collection
///   ([`Node::required_addrs`], clipboard export) cannot see inside ext, so a
///   smuggled content address would dangle.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Ref {
    addr: gantz_ca::ContentAddr,
    ext: BTreeMap<String, Datum>,
}

impl Ref {
    /// Create a new [`Ref`] node that references the node at the given address.
    pub fn new(addr: gantz_ca::ContentAddr) -> Self {
        Self {
            addr,
            ext: BTreeMap::new(),
        }
    }

    /// The content address of the referenced node.
    pub fn content_addr(&self) -> gantz_ca::ContentAddr {
        self.addr
    }

    /// The same reference (including its ext data) pointing at `addr`.
    ///
    /// For repointing operations where the referenced content is equivalent
    /// (e.g. forking a reference to a new name), so domain flags still apply.
    pub fn retarget(&self, addr: gantz_ca::ContentAddr) -> Self {
        Self {
            addr,
            ext: self.ext.clone(),
        }
    }

    /// The raw extension datum stored under `key`, if any.
    pub fn ext(&self, key: &str) -> Option<&Datum> {
        self.ext.get(key)
    }

    /// Decode the extension value stored under `key` as a `T`.
    ///
    /// `None` when no entry exists or the datum does not decode as `T` (by
    /// convention a key holds one value type, so a mismatch means the entry
    /// is not the caller's).
    pub fn ext_as<T: DeserializeOwned>(&self, key: &str) -> Option<T> {
        self.ext
            .get(key)
            .and_then(|d| datum::from_datum(d.clone()).ok())
    }

    /// Store `value` as this reference's extension data under `key`.
    ///
    /// The value is converted to its canonical datum form once, here. See the
    /// type-level docs for the conventions ext entries follow.
    pub fn set_ext(
        &mut self,
        key: impl Into<String>,
        value: &impl Serialize,
    ) -> Result<(), DatumError> {
        let mut d = datum::to_datum(value)?;
        d.canonicalize();
        self.ext.insert(key.into(), d);
        Ok(())
    }

    /// Remove and return the extension datum stored under `key`, if any.
    pub fn remove_ext(&mut self, key: &str) -> Option<Datum> {
        self.ext.remove(key)
    }
}

/// The ext-carrying inner wire form: an explicit map of `addr` + `ext`.
struct ExtMap<'a>(&'a Ref);

impl Serialize for ExtMap<'_> {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        let mut map = s.serialize_map(Some(2))?;
        map.serialize_entry("addr", &self.0.addr)?;
        map.serialize_entry("ext", &self.0.ext)?;
        map.end()
    }
}

// Hand-written serde keeps the ext-free wire form byte-identical to the
// previous newtype derive (a bare address, `(("hex"))` in RON, a string datum
// in the `Datum` codec). An ext-carrying reference wraps a map of
// `addr` + `ext` in the same newtype, so both forms parse through one entry
// point (RON is not self-describing at the top level; the newtype is where
// its parser branches).
impl Serialize for Ref {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        if self.ext.is_empty() {
            s.serialize_newtype_struct("Ref", &self.addr)
        } else {
            s.serialize_newtype_struct("Ref", &ExtMap(self))
        }
    }
}

impl<'de> Deserialize<'de> for Ref {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        struct RefVisitor;

        impl<'de> Visitor<'de> for RefVisitor {
            type Value = Ref;

            fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.write_str("a content address or a map of `addr` + `ext`")
            }

            fn visit_newtype_struct<D: Deserializer<'de>>(self, d: D) -> Result<Ref, D::Error> {
                d.deserialize_any(RefVisitor)
            }

            fn visit_str<E: de::Error>(self, v: &str) -> Result<Ref, E> {
                let addr = v.parse::<gantz_ca::ContentAddr>().map_err(|_| {
                    de::Error::invalid_value(de::Unexpected::Str(v), &"a hex content address")
                })?;
                Ok(Ref::new(addr))
            }

            // RON's self-describing pass reports the address tuple-struct's
            // parens as a seq whose one element is the hex string.
            fn visit_seq<A: de::SeqAccess<'de>>(self, mut seq: A) -> Result<Ref, A::Error> {
                let hex: String = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(0, &self))?;
                self.visit_str(&hex)
            }

            fn visit_map<A: de::MapAccess<'de>>(self, mut map: A) -> Result<Ref, A::Error> {
                let mut addr = None;
                let mut ext = BTreeMap::new();
                while let Some(key) = map.next_key::<String>()? {
                    match key.as_str() {
                        "addr" => addr = Some(map.next_value::<gantz_ca::ContentAddr>()?),
                        "ext" => ext = map.next_value::<BTreeMap<String, Datum>>()?,
                        _ => {
                            map.next_value::<de::IgnoredAny>()?;
                        }
                    }
                }
                let addr = addr.ok_or_else(|| de::Error::missing_field("addr"))?;
                // Canonicalize so identity is stable regardless of how the
                // wire form ordered nested map entries.
                ext.values_mut().for_each(Datum::canonicalize);
                Ok(Ref { addr, ext })
            }
        }

        d.deserialize_newtype_struct("Ref", RefVisitor)
    }
}

impl AsRefNode for Ref {
    fn as_ref_node(&self) -> Option<&Ref> {
        Some(self)
    }
}

impl gantz_ca::CaHash for Ref {
    /// Reproduces the previously-derived byte stream exactly (discriminator
    /// tag then the address), folding `ext` only when non-empty so ext-free
    /// references keep their address.
    fn hash(&self, hasher: &mut gantz_ca::Hasher) {
        hasher.update("gantz.ref".as_bytes());
        gantz_ca::CaHash::hash(&self.addr, hasher);
        if !self.ext.is_empty() {
            hasher.update(b"ext");
            for (k, v) in &self.ext {
                hasher.update(&(k.len() as u64).to_be_bytes());
                hasher.update(k.as_bytes());
                gantz_ca::CaHash::hash(v, hasher);
            }
        }
    }
}

impl Node for Ref {
    fn n_inputs(&self, ctx: node::MetaCtx) -> usize {
        ctx.node(&self.addr).map(|n| n.n_inputs(ctx)).unwrap_or(0)
    }

    fn n_outputs(&self, ctx: node::MetaCtx) -> usize {
        ctx.node(&self.addr).map(|n| n.n_outputs(ctx)).unwrap_or(0)
    }

    fn branches(&self, ctx: node::MetaCtx) -> Vec<node::EvalConf> {
        ctx.node(&self.addr)
            .map(|n| n.branches(ctx))
            .unwrap_or_default()
    }

    fn expr(&self, ctx: node::ExprCtx<'_, '_>) -> node::ExprResult {
        match ctx.node(&self.addr) {
            Some(n) => n.expr(ctx),
            None => Err(node::ExprError::custom(format!(
                "node not found for address {:?}",
                self.addr
            ))),
        }
    }

    fn push_eval(&self, ctx: node::MetaCtx) -> Vec<node::EvalConf> {
        ctx.node(&self.addr)
            .map(|n| n.push_eval(ctx))
            .unwrap_or_default()
    }

    fn pull_eval(&self, ctx: node::MetaCtx) -> Vec<node::EvalConf> {
        ctx.node(&self.addr)
            .map(|n| n.pull_eval(ctx))
            .unwrap_or_default()
    }

    fn stateful(&self, ctx: node::MetaCtx) -> bool {
        ctx.node(&self.addr)
            .map(|n| n.stateful(ctx))
            .unwrap_or(false)
    }

    fn register(&self, ctx: node::RegCtx<'_, '_>) {
        // Check if node exists first, then decompose context to pass to nested register.
        if ctx.node(&self.addr).is_some() {
            let (get_node, path, vm) = ctx.into_parts();
            // Safe to unwrap since we checked above.
            let n = (get_node)(&self.addr).unwrap();
            n.register(node::RegCtx::new(get_node, path, vm));
        }
    }

    fn inlet(&self, ctx: node::MetaCtx) -> bool {
        ctx.node(&self.addr).map(|n| n.inlet(ctx)).unwrap_or(false)
    }

    fn outlet(&self, ctx: node::MetaCtx) -> bool {
        ctx.node(&self.addr).map(|n| n.outlet(ctx)).unwrap_or(false)
    }

    fn delay(&self, ctx: node::MetaCtx) -> bool {
        ctx.node(&self.addr).map(|n| n.delay(ctx)).unwrap_or(false)
    }

    fn required_addrs(&self) -> Vec<gantz_ca::ContentAddr> {
        vec![self.addr]
    }

    fn visit(&self, ctx: visit::Ctx<'_, '_>, visitor: &mut dyn node::Visitor) {
        if let Some(n) = ctx.node(&self.addr) {
            n.visit(ctx, visitor);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};

    fn test_ref() -> Ref {
        Ref::new(gantz_ca::ContentAddr::from([0u8; 32]))
    }

    #[derive(Debug, PartialEq, Serialize, Deserialize)]
    struct TestExt {
        zeta: bool,
        alpha: u32,
    }

    /// The ext-free address must never change: it is the address every
    /// existing graph's references already hash to.
    #[test]
    fn ext_free_content_addr_is_pinned() {
        assert_eq!(
            gantz_ca::content_addr(&test_ref()).to_string(),
            "0f0d9b48ea1b758612ee71930395098005e9ae7b37560c48d4763dbfa950f421",
            "ext-free Ref CA changed - this breaks every existing graph address",
        );
    }

    /// Ext participates in the address, and removing it restores the
    /// ext-free address exactly.
    #[test]
    fn ext_affects_content_addr() {
        let plain = test_ref();
        let mut extended = test_ref();
        extended
            .set_ext(
                "test.ext",
                &TestExt {
                    zeta: true,
                    alpha: 3,
                },
            )
            .unwrap();
        assert_ne!(
            gantz_ca::content_addr(&plain),
            gantz_ca::content_addr(&extended)
        );
        extended.remove_ext("test.ext");
        assert_eq!(
            gantz_ca::content_addr(&plain),
            gantz_ca::content_addr(&extended)
        );
    }

    /// Both wire shapes round-trip through the Datum codec: ext-free stays
    /// the bare address string, ext-carrying takes the map form. The stored
    /// canonical form survives (struct field order does not leak).
    #[test]
    fn ref_roundtrips_through_datum_codec() {
        let plain = test_ref();
        let d = datum::to_datum(&plain).unwrap();
        assert!(
            matches!(d, Datum::Str(_)),
            "ext-free Ref must stay a bare address, got {d:?}"
        );
        assert_eq!(datum::from_datum::<Ref>(d).unwrap(), plain);

        let mut extended = test_ref();
        extended
            .set_ext(
                "test.ext",
                &TestExt {
                    zeta: true,
                    alpha: 3,
                },
            )
            .unwrap();
        let d = datum::to_datum(&extended).unwrap();
        assert!(
            matches!(d, Datum::Map(_)),
            "ext-carrying Ref must take the map form, got {d:?}"
        );
        let rt: Ref = datum::from_datum(d).unwrap();
        assert_eq!(rt, extended);
        assert_eq!(
            gantz_ca::content_addr(&rt),
            gantz_ca::content_addr(&extended)
        );
        // The stored datum is canonical despite TestExt's declaration order.
        assert_eq!(
            extended.ext("test.ext"),
            Some(&Datum::Map(vec![
                ("alpha".to_string(), Datum::U64(3)),
                ("zeta".to_string(), Datum::Bool(true)),
            ])),
        );
        assert_eq!(
            extended.ext_as::<TestExt>("test.ext"),
            Some(TestExt {
                zeta: true,
                alpha: 3
            })
        );
    }

    /// serde_json coverage for the second self-describing format: both
    /// shapes round-trip with identical addresses.
    #[test]
    fn ref_roundtrips_through_json() {
        let mut extended = test_ref();
        extended
            .set_ext(
                "test.ext",
                &TestExt {
                    zeta: false,
                    alpha: 1,
                },
            )
            .unwrap();
        for r in [test_ref(), extended] {
            let json = serde_json::to_string(&r).unwrap();
            let rt: Ref = serde_json::from_str(&json).unwrap();
            assert_eq!(rt, r);
            assert_eq!(gantz_ca::content_addr(&rt), gantz_ca::content_addr(&r));
        }
    }
}