ternary-bridge 0.1.0

Bridge pattern for connecting heterogeneous ternary systems with protocol and codec translation
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#![forbid(unsafe_code)]

//! Bridge pattern for connecting heterogeneous ternary systems.
//!
//! In a tri-axial ternary fleet, different subsystems speak different protocols,
//! use different wire formats, and store data in different memory models. This
//! crate provides the `Bridge` trait and concrete bridge implementations:
//! `ProtocolBridge`, `CodecBridge`, `MemoryBridge`, and `SkillBridge` — the
//! interop layer that makes the fleet talk to each other.

use std::collections::HashMap;

// ── TernaryValue ───────────────────────────────────────────────────────────

/// A ternary value: Negative, Zero, or Positive.
/// Maps to -1, 0, +1 in balanced ternary arithmetic.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TernaryValue {
    Negative,
    Zero,
    Positive,
}

impl TernaryValue {
    pub fn to_i8(self) -> i8 {
        match self {
            TernaryValue::Negative => -1,
            TernaryValue::Zero => 0,
            TernaryValue::Positive => 1,
        }
    }

    pub fn from_i8(v: i8) -> Option<Self> {
        match v {
            -1 => Some(TernaryValue::Negative),
            0 => Some(TernaryValue::Zero),
            1 => Some(TernaryValue::Positive),
            _ => None,
        }
    }
}

// ── Bridge Trait ───────────────────────────────────────────────────────────

/// A bridge translates between two representations.
///
/// `A` is the source representation, `B` is the target.
/// Translations can fail if the source has no valid target mapping.
pub trait Bridge<A, B> {
    /// The error type for failed translations.
    type Error;

    /// Translate from A to B.
    fn forward(&self, value: &A) -> Result<B, Self::Error>;

    /// Translate from B to A.
    fn backward(&self, value: &B) -> Result<A, Self::Error>;
}

// ── BridgeError ────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BridgeError {
    pub message: String,
}

impl BridgeError {
    pub fn new(msg: &str) -> Self {
        Self { message: msg.to_string() }
    }
}

impl std::fmt::Display for BridgeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "BridgeError: {}", self.message)
    }
}

// ── WireFormat ─────────────────────────────────────────────────────────────

/// Different wire formats for encoding ternary data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WireFormat {
    /// Each ternary digit as one byte: -1 → 0xFF, 0 → 0x00, +1 → 0x01.
    BytePerTrit,
    /// Two trits per byte (packed nibble format).
    Packed,
    /// UTF-8 string representation: "T", "0", "1" (negative="T", zero="0", positive="1").
    Text,
}

// ── ProtocolMessage ────────────────────────────────────────────────────────

/// A message in the ternary-protocol format.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProtocolMessage {
    pub source: String,
    pub destination: String,
    pub payload: Vec<TernaryValue>,
    pub sequence: u32,
}

impl ProtocolMessage {
    pub fn new(source: &str, destination: &str, payload: Vec<TernaryValue>) -> Self {
        Self {
            source: source.to_string(),
            destination: destination.to_string(),
            payload,
            sequence: 0,
        }
    }
}

// ── OracleMessage ──────────────────────────────────────────────────────────

/// A message in the Oracle1 I2I format (simulated).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OracleMessage {
    pub from_node: String,
    pub to_node: String,
    pub data: Vec<i8>,
    pub msg_id: u32,
}

impl OracleMessage {
    pub fn new(from: &str, to: &str, data: Vec<i8>) -> Self {
        Self {
            from_node: from.to_string(),
            to_node: to.to_string(),
            data,
            msg_id: 0,
        }
    }
}

// ── ProtocolBridge ─────────────────────────────────────────────────────────

/// Translates between ternary-protocol messages and Oracle1 I2I messages.
pub struct ProtocolBridge {
    next_sequence: std::cell::Cell<u32>,
}

impl ProtocolBridge {
    pub fn new() -> Self {
        Self {
            next_sequence: std::cell::Cell::new(1),
        }
    }

    fn alloc_sequence(&self) -> u32 {
        let seq = self.next_sequence.get();
        self.next_sequence.set(seq + 1);
        seq
    }
}

impl Default for ProtocolBridge {
    fn default() -> Self {
        Self::new()
    }
}

impl Bridge<ProtocolMessage, OracleMessage> for ProtocolBridge {
    type Error = BridgeError;

    fn forward(&self, msg: &ProtocolMessage) -> Result<OracleMessage, Self::Error> {
        let data: Vec<i8> = msg.payload.iter().map(|t| t.to_i8()).collect();
        let mut oracle = OracleMessage::new(&msg.source, &msg.destination, data);
        oracle.msg_id = self.alloc_sequence();
        Ok(oracle)
    }

    fn backward(&self, msg: &OracleMessage) -> Result<ProtocolMessage, Self::Error> {
        let payload: Result<Vec<TernaryValue>, _> = msg.data.iter()
            .map(|&v| TernaryValue::from_i8(v).ok_or_else(|| BridgeError::new(&format!("Invalid ternary value: {}", v))))
            .collect();
        let mut proto = ProtocolMessage::new(&msg.from_node, &msg.to_node, payload?);
        proto.sequence = msg.msg_id;
        Ok(proto)
    }
}

// ── CodecBridge ────────────────────────────────────────────────────────────

/// Encodes and decodes ternary data between wire formats.
pub struct CodecBridge;

impl CodecBridge {
    /// Encode ternary values to the specified wire format.
    pub fn encode(values: &[TernaryValue], format: WireFormat) -> Result<Vec<u8>, BridgeError> {
        match format {
            WireFormat::BytePerTrit => {
                Ok(values.iter().map(|v| v.to_i8() as u8).collect())
            }
            WireFormat::Packed => {
                if values.len() % 2 != 0 {
                    return Err(BridgeError::new("Packed format requires even number of trits"));
                }
                let mut result = Vec::new();
                for chunk in values.chunks(2) {
                    // Map: Negative(-1)->3, Zero(0)->0, Positive(1)->1
                    let high = match chunk[0] {
                        TernaryValue::Negative => 3u8,
                        TernaryValue::Zero => 0u8,
                        TernaryValue::Positive => 1u8,
                    };
                    let low = match chunk[1] {
                        TernaryValue::Negative => 3u8,
                        TernaryValue::Zero => 0u8,
                        TernaryValue::Positive => 1u8,
                    };
                    result.push((high << 4) | low);
                }
                Ok(result)
            }
            WireFormat::Text => {
                let s: String = values.iter().map(|v| match v {
                    TernaryValue::Negative => 'T',
                    TernaryValue::Zero => '0',
                    TernaryValue::Positive => '1',
                }).collect();
                Ok(s.into_bytes())
            }
        }
    }

    fn nibble_to_trit(n: u8) -> Result<TernaryValue, BridgeError> {
        match n {
            0 => Ok(TernaryValue::Zero),
            1 => Ok(TernaryValue::Positive),
            3 => Ok(TernaryValue::Negative),
            _ => Err(BridgeError::new(&format!("Invalid packed trit nibble: {}", n))),
        }
    }

    /// Decode bytes from the specified wire format into ternary values.
    pub fn decode(bytes: &[u8], format: WireFormat) -> Result<Vec<TernaryValue>, BridgeError> {
        match format {
            WireFormat::BytePerTrit => {
                bytes.iter()
                    .map(|&b| {
                        let signed = b as i8;
                        TernaryValue::from_i8(signed)
                            .ok_or_else(|| BridgeError::new(&format!("Invalid trit byte: {}", signed)))
                    })
                    .collect()
            }
            WireFormat::Packed => {
                let mut result = Vec::new();
                for &byte in bytes {
                    let high = (byte >> 4) & 0x0F;
                    let low = byte & 0x0F;
                    result.push(Self::nibble_to_trit(high)?);
                    result.push(Self::nibble_to_trit(low)?);
                }
                Ok(result)
            }
            WireFormat::Text => {
                bytes.iter()
                    .map(|&b| match b as char {
                        'T' => Ok(TernaryValue::Negative),
                        '0' => Ok(TernaryValue::Zero),
                        '1' => Ok(TernaryValue::Positive),
                        c => Err(BridgeError::new(&format!("Invalid text trit: '{}'", c))),
                    })
                    .collect()
            }
        }
    }
}

impl Bridge<Vec<TernaryValue>, Vec<u8>> for CodecBridge {
    type Error = BridgeError;

    fn forward(&self, values: &Vec<TernaryValue>) -> Result<Vec<u8>, Self::Error> {
        Self::encode(values, WireFormat::BytePerTrit)
    }

    fn backward(&self, bytes: &Vec<u8>) -> Result<Vec<TernaryValue>, Self::Error> {
        Self::decode(bytes, WireFormat::BytePerTrit)
    }
}

// ── MemoryBridge ───────────────────────────────────────────────────────────

/// Short-term memory entry: simple key-value with a creation tick.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StmEntry {
    pub key: String,
    pub value: Vec<TernaryValue>,
    pub created_at: u64,
}

/// Long-term memory entry: key-value with an access count and weight.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LtmEntry {
    pub key: String,
    pub value: Vec<TernaryValue>,
    pub access_count: u32,
    pub weight: u32,
}

/// Translates between short-term and long-term memory representations.
/// STM entries are ephemeral; when promoted to LTM, they gain access tracking.
pub struct MemoryBridge;

impl Bridge<StmEntry, LtmEntry> for MemoryBridge {
    type Error = BridgeError;

    fn forward(&self, stm: &StmEntry) -> Result<LtmEntry, Self::Error> {
        Ok(LtmEntry {
            key: stm.key.clone(),
            value: stm.value.clone(),
            access_count: 0,
            weight: 1,
        })
    }

    fn backward(&self, ltm: &LtmEntry) -> Result<StmEntry, Self::Error> {
        Ok(StmEntry {
            key: ltm.key.clone(),
            value: ltm.value.clone(),
            created_at: 0,
        })
    }
}

// ── SkillBridge ────────────────────────────────────────────────────────────

/// A skill descriptor in TypeScript/Equipment format.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TsSkill {
    pub name: String,
    pub inputs: Vec<String>,
    pub outputs: Vec<String>,
    pub source: String,
}

/// A skill descriptor in Rust/Construct format.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RustSkill {
    pub name: String,
    pub params: Vec<String>,
    pub returns: Vec<String>,
    pub module_path: String,
}

/// Translates skill descriptors between TypeScript (Equipment) and Rust (Construct) formats.
pub struct SkillBridge;

impl Bridge<TsSkill, RustSkill> for SkillBridge {
    type Error = BridgeError;

    fn forward(&self, ts: &TsSkill) -> Result<RustSkill, Self::Error> {
        Ok(RustSkill {
            name: ts.name.clone(),
            params: ts.inputs.clone(),
            returns: ts.outputs.clone(),
            module_path: format!("construct::skills::{}", ts.name.replace('-', "_")),
        })
    }

    fn backward(&self, rs: &RustSkill) -> Result<TsSkill, Self::Error> {
        Ok(TsSkill {
            name: rs.name.clone(),
            inputs: rs.params.clone(),
            outputs: rs.returns.clone(),
            source: format!("equipment/skills/{}.ts", rs.name),
        })
    }
}

// ── BridgeRegistry ─────────────────────────────────────────────────────────

/// A registry that stores named bridges for runtime lookup.
pub struct BridgeRegistry {
    bridges: HashMap<String, String>, // name -> bridge type description
}

impl BridgeRegistry {
    pub fn new() -> Self {
        Self { bridges: HashMap::new() }
    }

    pub fn register(&mut self, name: &str, bridge_type: &str) {
        self.bridges.insert(name.to_string(), bridge_type.to_string());
    }

    pub fn get(&self, name: &str) -> Option<&str> {
        self.bridges.get(name).map(|s| s.as_str())
    }

    pub fn list(&self) -> Vec<(&str, &str)> {
        self.bridges.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect()
    }

    pub fn remove(&mut self, name: &str) -> bool {
        self.bridges.remove(name).is_some()
    }
}

impl Default for BridgeRegistry {
    fn default() -> Self {
        Self::new()
    }
}

// ── Tests ──────────────────────────────────────────────────────────────────

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

    #[test]
    fn test_ternary_value_roundtrip() {
        for v in &[TernaryValue::Negative, TernaryValue::Zero, TernaryValue::Positive] {
            assert_eq!(TernaryValue::from_i8(v.to_i8()).unwrap(), *v);
        }
    }

    #[test]
    fn test_ternary_invalid_i8() {
        assert!(TernaryValue::from_i8(2).is_none());
        assert!(TernaryValue::from_i8(-2).is_none());
    }

    #[test]
    fn test_protocol_bridge_forward() {
        let bridge = ProtocolBridge::new();
        let proto = ProtocolMessage::new("node-a", "node-b", vec![
            TernaryValue::Positive, TernaryValue::Zero, TernaryValue::Negative,
        ]);
        let oracle = bridge.forward(&proto).unwrap();
        assert_eq!(oracle.from_node, "node-a");
        assert_eq!(oracle.data, vec![1, 0, -1]);
        assert_eq!(oracle.msg_id, 1);
    }

    #[test]
    fn test_protocol_bridge_backward() {
        let bridge = ProtocolBridge::new();
        let oracle = OracleMessage::new("x", "y", vec![1, 0, -1]);
        let proto = bridge.backward(&oracle).unwrap();
        assert_eq!(proto.payload, vec![
            TernaryValue::Positive, TernaryValue::Zero, TernaryValue::Negative,
        ]);
        assert_eq!(proto.sequence, 0);
    }

    #[test]
    fn test_protocol_bridge_roundtrip() {
        let bridge = ProtocolBridge::new();
        let proto = ProtocolMessage::new("a", "b", vec![
            TernaryValue::Negative, TernaryValue::Zero, TernaryValue::Positive,
        ]);
        let oracle = bridge.forward(&proto).unwrap();
        let back = bridge.backward(&oracle).unwrap();
        assert_eq!(back.payload, proto.payload);
        assert_eq!(back.source, proto.source);
        assert_eq!(back.destination, proto.destination);
    }

    #[test]
    fn test_protocol_bridge_invalid_backward() {
        let bridge = ProtocolBridge::new();
        let oracle = OracleMessage::new("a", "b", vec![5, 0]);
        assert!(bridge.backward(&oracle).is_err());
    }

    #[test]
    fn test_codec_byte_per_trit() {
        let values = vec![TernaryValue::Negative, TernaryValue::Zero, TernaryValue::Positive];
        let encoded = CodecBridge::encode(&values, WireFormat::BytePerTrit).unwrap();
        let decoded = CodecBridge::decode(&encoded, WireFormat::BytePerTrit).unwrap();
        assert_eq!(decoded, values);
    }

    #[test]
    fn test_codec_packed() {
        let values = vec![TernaryValue::Negative, TernaryValue::Positive, TernaryValue::Zero, TernaryValue::Zero];
        let encoded = CodecBridge::encode(&values, WireFormat::Packed).unwrap();
        assert_eq!(encoded.len(), 2); // 4 trits = 2 bytes packed
        let decoded = CodecBridge::decode(&encoded, WireFormat::Packed).unwrap();
        assert_eq!(decoded, values);
    }

    #[test]
    fn test_codec_packed_odd_fails() {
        let values = vec![TernaryValue::Positive, TernaryValue::Zero, TernaryValue::Negative];
        assert!(CodecBridge::encode(&values, WireFormat::Packed).is_err());
    }

    #[test]
    fn test_codec_text() {
        let values = vec![TernaryValue::Negative, TernaryValue::Zero, TernaryValue::Positive];
        let encoded = CodecBridge::encode(&values, WireFormat::Text).unwrap();
        assert_eq!(&String::from_utf8_lossy(&encoded), "T01");
        let decoded = CodecBridge::decode(&encoded, WireFormat::Text).unwrap();
        assert_eq!(decoded, values);
    }

    #[test]
    fn test_codec_text_invalid_char() {
        assert!(CodecBridge::decode(b"X", WireFormat::Text).is_err());
    }

    #[test]
    fn test_memory_bridge_forward() {
        let bridge = MemoryBridge;
        let stm = StmEntry {
            key: "temp-reading".into(),
            value: vec![TernaryValue::Positive],
            created_at: 100,
        };
        let ltm = bridge.forward(&stm).unwrap();
        assert_eq!(ltm.key, "temp-reading");
        assert_eq!(ltm.access_count, 0);
        assert_eq!(ltm.weight, 1);
    }

    #[test]
    fn test_memory_bridge_backward() {
        let bridge = MemoryBridge;
        let ltm = LtmEntry {
            key: "pattern".into(),
            value: vec![TernaryValue::Negative],
            access_count: 42,
            weight: 10,
        };
        let stm = bridge.backward(&ltm).unwrap();
        assert_eq!(stm.key, "pattern");
        assert_eq!(stm.created_at, 0); // STM doesn't preserve LTM metadata
    }

    #[test]
    fn test_memory_bridge_roundtrip() {
        let bridge = MemoryBridge;
        let stm = StmEntry {
            key: "k".into(),
            value: vec![TernaryValue::Zero],
            created_at: 50,
        };
        let ltm = bridge.forward(&stm).unwrap();
        let back = bridge.backward(&ltm).unwrap();
        assert_eq!(back.key, stm.key);
        assert_eq!(back.value, stm.value);
    }

    #[test]
    fn test_skill_bridge_forward() {
        let bridge = SkillBridge;
        let ts = TsSkill {
            name: "detect-anomaly".into(),
            inputs: vec!["sensor-data".into()],
            outputs: vec!["is-anomaly".into()],
            source: "equipment/skills/detect-anomaly.ts".into(),
        };
        let rs = bridge.forward(&ts).unwrap();
        assert_eq!(rs.name, "detect-anomaly");
        assert_eq!(rs.module_path, "construct::skills::detect_anomaly");
        assert_eq!(rs.params, vec!["sensor-data"]);
    }

    #[test]
    fn test_skill_bridge_backward() {
        let bridge = SkillBridge;
        let rs = RustSkill {
            name: "classify".into(),
            params: vec!["input".into()],
            returns: vec!["label".into()],
            module_path: "construct::skills::classify".into(),
        };
        let ts = bridge.backward(&rs).unwrap();
        assert_eq!(ts.name, "classify");
        assert_eq!(ts.source, "equipment/skills/classify.ts");
    }

    #[test]
    fn test_skill_bridge_roundtrip() {
        let bridge = SkillBridge;
        let ts = TsSkill {
            name: "transform".into(),
            inputs: vec!["data".into()],
            outputs: vec!["result".into()],
            source: "".into(),
        };
        let rs = bridge.forward(&ts).unwrap();
        let back = bridge.backward(&rs).unwrap();
        assert_eq!(back.name, ts.name);
        assert_eq!(back.inputs, ts.inputs);
        assert_eq!(back.outputs, ts.outputs);
    }

    #[test]
    fn test_bridge_registry_register_and_get() {
        let mut reg = BridgeRegistry::new();
        reg.register("proto", "ProtocolBridge");
        assert_eq!(reg.get("proto"), Some("ProtocolBridge"));
        assert_eq!(reg.get("missing"), None);
    }

    #[test]
    fn test_bridge_registry_remove() {
        let mut reg = BridgeRegistry::new();
        reg.register("codec", "CodecBridge");
        assert!(reg.remove("codec"));
        assert!(!reg.remove("codec"));
    }

    #[test]
    fn test_bridge_registry_list() {
        let mut reg = BridgeRegistry::new();
        reg.register("a", "TypeA");
        reg.register("b", "TypeB");
        let list = reg.list();
        assert_eq!(list.len(), 2);
    }

    #[test]
    fn test_codec_default_impl() {
        let bridge = CodecBridge;
        let values = vec![TernaryValue::Positive, TernaryValue::Zero];
        let encoded = bridge.forward(&values).unwrap();
        let decoded = bridge.backward(&encoded).unwrap();
        assert_eq!(decoded, values);
    }

    #[test]
    fn test_bridge_error_display() {
        let err = BridgeError::new("test error");
        assert_eq!(format!("{}", err), "BridgeError: test error");
    }
}