ordofp_core 0.1.0

OrdoFP core provides developers with HList, Disiunctio, NominataUniversalis, Universalis, and functional type classes
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
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Distributed Communication Protocol
//!
//! > *"Protocollum est lingua machinarum"*
//! > — Protocol is the language of machines. (Latin)
//!
//! This module defines the wire protocol for distributed
//! communication between nodes.

use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
use core::time::Duration;

use super::node::{InformationesNodi, NodusIdentitas, StatusNodi};
use super::serializable::NodusSerializabilis;

/// Default priority for submitted computations.
const DEFAULT_COMPUTATION_PRIORITY: u32 = 100;

// =============================================================================
// Protocol Version
// =============================================================================

/// Protocol version for compatibility checking.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VersioProtocolli {
    /// Major version (breaking changes).
    pub maior: u16,
    /// Minor version (backward compatible features).
    pub minor: u16,
    /// Patch version (bug fixes).
    pub emendatio: u16,
}

impl VersioProtocolli {
    /// Current protocol version.
    pub const CURRENS: Self = VersioProtocolli {
        maior: 1,
        minor: 0,
        emendatio: 0,
    };

    /// Create a new version.
    #[inline]
    pub const fn new(maior: u16, minor: u16, emendatio: u16) -> Self {
        VersioProtocolli {
            maior,
            minor,
            emendatio,
        }
    }

    /// Check if this version is compatible with another.
    pub fn is_compatible(&self, other: &Self) -> bool {
        // Major version must match, minor version must be >= other
        self.maior == other.maior && self.minor >= other.minor
    }
}

impl fmt::Display for VersioProtocolli {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}.{}", self.maior, self.minor, self.emendatio)
    }
}

// =============================================================================
// Message Types
// =============================================================================

/// Message header for all protocol messages.
#[derive(Debug, Clone)]
pub struct CaputNuntii {
    /// Protocol version.
    pub versio: VersioProtocolli,
    /// Message ID for correlation.
    pub id: u64,
    /// Sender node ID.
    pub mittens: NodusIdentitas,
    /// Recipient node ID (None = broadcast).
    pub recipiens: Option<NodusIdentitas>,
    /// Timestamp (duration since Unix epoch).
    pub tempus: Duration,
    /// Message type.
    pub genus: GenusNuntii,
    /// Request correlation ID (for responses).
    pub correlatio: Option<u64>,
}

/// Message type enumeration.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GenusNuntii {
    // === Cluster Management ===
    /// Heartbeat/ping.
    Pulsatio,
    /// Join cluster request.
    Coniunctio,
    /// Leave cluster notification.
    Abscessio,
    /// Node status update.
    StatusMutatio,

    // === Discovery ===
    /// Request node list.
    InquisitioNodorum,
    /// Response with node list.
    ResponsumNodorum,

    // === Computation ===
    /// Submit computation.
    SubmissioComputationis,
    /// Computation result.
    ResultatumComputationis,
    /// Cancel computation.
    CancellatioComputationis,

    // === Effects ===
    /// Effect operation request.
    OperatioEffectus,
    /// Effect operation response.
    ResponsumEffectus,

    // === Consensus ===
    /// Leader election vote.
    Suffragium,
    /// Leader announcement.
    AnnuntiatioDucis,
    /// Log replication.
    ReplicatioActorum,

    // === Error ===
    /// Error response.
    Error,
}

// =============================================================================
// Protocol Messages
// =============================================================================

/// Complete protocol message.
#[derive(Debug, Clone)]
pub struct Nuntius {
    /// Message header.
    pub caput: CaputNuntii,
    /// Message payload.
    pub corpus: CorpusNuntii,
}

impl Nuntius {
    /// Create a new message.
    ///
    /// **Placeholder semantics:** the header timestamp (`tempus`) is always
    /// zero — there is no clock in `no_std` scope, so callers that need real
    /// timestamps must set them from system time themselves.
    pub fn new(mittens: NodusIdentitas, genus: GenusNuntii, corpus: CorpusNuntii) -> Self {
        static COUNTER: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(1);

        Nuntius {
            caput: CaputNuntii {
                versio: VersioProtocolli::CURRENS,
                id: COUNTER.fetch_add(1, core::sync::atomic::Ordering::SeqCst),
                mittens,
                recipiens: None,
                tempus: Duration::from_secs(0), // Would be set from system time
                genus,
                correlatio: None,
            },
            corpus,
        }
    }

    /// Set recipient.
    pub fn to(mut self, recipiens: NodusIdentitas) -> Self {
        self.caput.recipiens = Some(recipiens);
        self
    }

    /// Set correlation ID.
    pub fn correlating(mut self, id: u64) -> Self {
        self.caput.correlatio = Some(id);
        self
    }

    /// Create a response to this message.
    ///
    /// **Placeholder semantics:** the response's sender is taken from this
    /// message's recipient field (falling back to the original sender) —
    /// the constructor has no notion of "the local node". Wire-up code with
    /// a real node identity should overwrite the sender.
    pub fn respond(&self, genus: GenusNuntii, corpus: CorpusNuntii) -> Self {
        Nuntius::new(
            // Would be local node ID, using sender as placeholder
            self.caput.recipiens.unwrap_or(self.caput.mittens),
            genus,
            corpus,
        )
        .to(self.caput.mittens)
        .correlating(self.caput.id)
    }
}

/// Message body/payload.
#[derive(Debug, Clone)]
pub enum CorpusNuntii {
    /// Empty payload.
    Vacuum,

    /// Heartbeat.
    Pulsatio(PulsatioCorpus),

    /// Join request.
    Coniunctio(ConiunctioCorpus),

    /// Node list.
    Nodi(Vec<InformationesNodi>),

    /// Status update.
    Status(StatusNodi),

    /// Computation submission.
    Computatio(ComputatioCorpus),

    /// Computation result.
    Resultatum(ResultatumCorpus),

    /// Effect operation.
    Effectus(EffectusCorpus),

    /// Effect response.
    ResponsumEffectus(ResponsumEffectusCorpus),

    /// Vote request/response.
    Suffragium(SuffragiumCorpus),

    /// Log entries.
    Acta(ActaCorpus),

    /// Error.
    Error(ErrorCorpus),
}

// =============================================================================
// Specific Message Bodies
// =============================================================================

/// Heartbeat message body.
#[derive(Debug, Clone)]
pub struct PulsatioCorpus {
    /// Current node status.
    pub status: StatusNodi,
    /// Current load factor.
    pub onus: f32,
    /// Active task count.
    pub munera_activa: u32,
    /// Cluster generation this node knows.
    pub generatio: u64,
}

/// Join request body.
#[derive(Debug, Clone)]
pub struct ConiunctioCorpus {
    /// Joining node info.
    pub informationes: InformationesNodi,
    /// Requested role.
    pub munus_petitus: super::node::MunusNodi,
}

/// Computation submission body.
#[derive(Debug, Clone)]
pub struct ComputatioCorpus {
    /// Computation ID.
    pub id: u64,
    /// Serialized computation.
    pub nodus: NodusSerializabilis,
    /// Required effects.
    pub effectus_requiriti: Vec<u64>,
    /// Priority.
    pub prioritas: u32,
    /// Timeout.
    pub mora: Option<Duration>,
}

/// Computation result body.
#[derive(Debug, Clone)]
pub struct ResultatumCorpus {
    /// Computation ID.
    pub computatio_id: u64,
    /// Success or failure.
    pub exitus: ExitusComputationis,
    /// Execution time.
    pub tempus: Duration,
}

/// Computation outcome.
#[derive(Debug, Clone)]
pub enum ExitusComputationis {
    /// Successful with result.
    Successus(Vec<u8>),
    /// Failed with error.
    Defectio(String),
    /// Cancelled.
    Cancellatus,
    /// Timed out.
    MoraExcessit,
}

/// Effect operation body.
#[derive(Debug, Clone)]
pub struct EffectusCorpus {
    /// Effect type ID.
    pub effectus_id: u64,
    /// Operation ID for correlation.
    pub operatio_id: u64,
    /// Serialized operation data.
    pub data: Vec<u8>,
}

/// Effect response body.
#[derive(Debug, Clone)]
pub struct ResponsumEffectusCorpus {
    /// Operation ID.
    pub operatio_id: u64,
    /// Success or failure.
    pub exitus: ExitusEffectus,
}

/// Effect operation outcome.
#[derive(Debug, Clone)]
pub enum ExitusEffectus {
    /// Successful with result.
    Successus(Vec<u8>),
    /// Failed.
    Defectio(String),
    /// Effect not supported.
    NonSuffultus,
}

/// Vote message body.
#[derive(Debug, Clone)]
pub struct SuffragiumCorpus {
    /// Term/epoch.
    pub terminus: u64,
    /// Candidate ID.
    pub candidatus: NodusIdentitas,
    /// Last log index.
    pub ultimus_index: u64,
    /// Last log term.
    pub ultimus_terminus: u64,
    /// Is this a vote grant?
    pub concessum: bool,
}

/// Log replication body.
#[derive(Debug, Clone)]
pub struct ActaCorpus {
    /// Leader term.
    pub terminus: u64,
    /// Leader ID.
    pub dux: NodusIdentitas,
    /// Previous log index.
    pub index_prior: u64,
    /// Previous log term.
    pub terminus_prior: u64,
    /// Log entries.
    pub ingressus: Vec<IngressusActorum>,
    /// Leader commit index.
    pub index_commissi: u64,
}

/// A log entry.
#[derive(Debug, Clone)]
pub struct IngressusActorum {
    /// Entry term.
    pub terminus: u64,
    /// Entry index.
    pub index: u64,
    /// Entry data.
    pub data: Vec<u8>,
}

/// Error message body.
#[derive(Debug, Clone)]
pub struct ErrorCorpus {
    /// Error code.
    pub codex: u32,
    /// Error message.
    pub nuntius: String,
    /// Retriable.
    pub iterabilis: bool,
}

impl ErrorCorpus {
    /// Error code: the addressed node is not known to the cluster.
    pub const NODE_NOT_FOUND: u32 = 1;
    /// Error code: the receiving node has no handler for the requested
    /// effect ID.
    pub const EFFECT_NOT_SUPPORTED: u32 = 2;
    /// Error code: the operation did not complete within its deadline.
    pub const TIMEOUT: u32 = 3;
    /// Error code: a message body could not be (de)serialized.
    pub const SERIALIZATION_ERROR: u32 = 4;
    /// Error code: a leader-only request reached a node that is not the
    /// current leader.
    pub const NOT_LEADER: u32 = 5;
    /// Error code: not enough nodes were reachable to form a quorum.
    pub const NO_QUORUM: u32 = 6;
    /// Error code: the remote computation itself failed after being
    /// successfully delivered.
    pub const COMPUTATION_FAILED: u32 = 7;
    /// Error code: the operation was cancelled before completion.
    pub const CANCELLED: u32 = 8;

    /// Create an error with code and message.
    #[inline]
    pub fn new(codex: u32, nuntius: impl Into<String>) -> Self {
        ErrorCorpus {
            codex,
            nuntius: nuntius.into(),
            iterabilis: false,
        }
    }

    /// Mark as retriable.
    pub fn retriable(mut self) -> Self {
        self.iterabilis = true;
        self
    }
}

// =============================================================================
// Wire Format
// =============================================================================

/// Wire format for serialization.
///
/// # Latin Etymology
/// *Forma fili* = wire format.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FormaFili {
    /// Binary format (compact).
    #[default]
    Binarius,
    /// JSON format (human-readable).
    Json,
    /// `MessagePack` format.
    MessagePack,
    /// Protocol Buffers.
    Protobuf,
}

/// Trait for message serialization.
pub trait Serializabilis: Sized {
    /// Serialize to bytes.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorSerialization`] when the value cannot be encoded in the
    /// requested [`FormaFili`] — for example, a format the implementation
    /// does not support. The message lives in
    /// [`nuntius`](ErrorSerialization::nuntius); `offset` is typically `None`
    /// for encoding failures.
    fn serialize(&self, forma: FormaFili) -> Result<Vec<u8>, ErrorSerialization>;

    /// Deserialize from bytes.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorSerialization`] when `bytes` is not a valid encoding of
    /// `Self` in the given [`FormaFili`] (malformed, truncated, or produced
    /// by a different format). Where the failing position is known,
    /// implementations record it in
    /// [`offset`](ErrorSerialization::offset).
    fn deserialize(bytes: &[u8], forma: FormaFili) -> Result<Self, ErrorSerialization>;
}

/// Serialization error.
#[derive(Debug, Clone)]
pub struct ErrorSerialization {
    /// Error message.
    pub nuntius: String,
    /// Byte offset where error occurred.
    pub offset: Option<usize>,
}

impl fmt::Display for ErrorSerialization {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.offset {
            Some(off) => write!(f, "Serialization error at byte {}: {}", off, self.nuntius),
            None => write!(f, "Serialization error: {}", self.nuntius),
        }
    }
}

// =============================================================================
// Message Builder
// =============================================================================

/// Builder for constructing protocol messages.
pub struct AedificatorNuntii {
    mittens: NodusIdentitas,
}

impl AedificatorNuntii {
    /// Create a new message builder.
    #[inline]
    pub fn new(mittens: NodusIdentitas) -> Self {
        AedificatorNuntii { mittens }
    }

    /// Build a heartbeat message.
    #[inline]
    pub fn pulsatio(&self, status: StatusNodi, onus: f32, munera: u32, generatio: u64) -> Nuntius {
        Nuntius::new(
            self.mittens,
            GenusNuntii::Pulsatio,
            CorpusNuntii::Pulsatio(PulsatioCorpus {
                status,
                onus,
                munera_activa: munera,
                generatio,
            }),
        )
    }

    /// Build a join request.
    #[inline]
    pub fn coniunctio(&self, info: InformationesNodi) -> Nuntius {
        Nuntius::new(
            self.mittens,
            GenusNuntii::Coniunctio,
            CorpusNuntii::Coniunctio(ConiunctioCorpus {
                munus_petitus: info.munus,
                informationes: info,
            }),
        )
    }

    /// Build a computation submission.
    #[inline]
    pub fn computatio(&self, id: u64, nodus: NodusSerializabilis, effectus: Vec<u64>) -> Nuntius {
        Nuntius::new(
            self.mittens,
            GenusNuntii::SubmissioComputationis,
            CorpusNuntii::Computatio(ComputatioCorpus {
                id,
                nodus,
                effectus_requiriti: effectus,
                prioritas: DEFAULT_COMPUTATION_PRIORITY,
                mora: None,
            }),
        )
    }

    /// Build an effect operation request.
    #[inline]
    pub fn effectus(&self, effectus_id: u64, operatio_id: u64, data: Vec<u8>) -> Nuntius {
        Nuntius::new(
            self.mittens,
            GenusNuntii::OperatioEffectus,
            CorpusNuntii::Effectus(EffectusCorpus {
                effectus_id,
                operatio_id,
                data,
            }),
        )
    }

    /// Build an error response.
    #[inline]
    pub fn error(&self, codex: u32, nuntius: impl Into<String>) -> Nuntius {
        Nuntius::new(
            self.mittens,
            GenusNuntii::Error,
            CorpusNuntii::Error(ErrorCorpus::new(codex, nuntius)),
        )
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_versio_compatibility() {
        let v1 = VersioProtocolli::new(1, 0, 0);
        let v2 = VersioProtocolli::new(1, 1, 0);
        let v3 = VersioProtocolli::new(2, 0, 0);

        assert!(v2.is_compatible(&v1));
        assert!(!v1.is_compatible(&v2));
        assert!(!v3.is_compatible(&v1));
    }

    #[test]
    fn test_nuntius_creation() {
        let sender = NodusIdentitas::new(1, 1);
        let msg = Nuntius::new(sender, GenusNuntii::Pulsatio, CorpusNuntii::Vacuum);

        assert_eq!(msg.caput.mittens, sender);
        assert_eq!(msg.caput.genus, GenusNuntii::Pulsatio);
    }

    #[test]
    fn test_nuntius_response() {
        let sender = NodusIdentitas::new(1, 1);
        let recipient = NodusIdentitas::new(2, 2);

        let request = Nuntius::new(sender, GenusNuntii::InquisitioNodorum, CorpusNuntii::Vacuum)
            .to(recipient);

        let response = request.respond(
            GenusNuntii::ResponsumNodorum,
            CorpusNuntii::Nodi(Vec::new()),
        );

        assert_eq!(response.caput.recipiens, Some(sender));
        assert_eq!(response.caput.correlatio, Some(request.caput.id));
    }

    #[test]
    fn test_aedificator_nuntii() {
        let sender = NodusIdentitas::new(1, 1);
        let builder = AedificatorNuntii::new(sender);

        let msg = builder.pulsatio(StatusNodi::Sanus, 0.5, 10, 1);
        assert_eq!(msg.caput.genus, GenusNuntii::Pulsatio);

        if let CorpusNuntii::Pulsatio(body) = msg.corpus {
            assert_eq!(body.status, StatusNodi::Sanus);
            assert!((body.onus - 0.5).abs() < f32::EPSILON);
        } else {
            panic!("Wrong message body type");
        }
    }

    #[test]
    fn test_error_corpus() {
        let err = ErrorCorpus::new(ErrorCorpus::TIMEOUT, "Operation timed out").retriable();

        assert_eq!(err.codex, ErrorCorpus::TIMEOUT);
        assert!(err.iterabilis);
    }
}