Skip to main content

jeff/
lib.rs

1//! The data model of the jeff representation.
2//!
3//! This crate defines data structures for zero-copy decoding of jeff files.
4mod capnp;
5mod jeff;
6
7#[cfg(test)]
8mod test;
9
10pub mod reader;
11pub mod types;
12pub use jeff::Jeff;
13
14// The capnp-generated code is re-exported here, but in general it should not be
15// used directly.
16//
17// No semver guarantees are provided for this module.
18#[doc(hidden)]
19pub use capnp::jeff_capnp;
20
21use derive_more::derive::{Display, Error, From};
22
23/// Latest version of the jeff schema.
24pub const SCHEMA_VERSION: semver::Version = semver::Version::new(
25    capnp::jeff_capnp::SCHEMA_VERSION_MAJOR as u64,
26    capnp::jeff_capnp::SCHEMA_VERSION_MINOR as u64,
27    capnp::jeff_capnp::SCHEMA_VERSION_PATCH as u64,
28);
29
30/// Errors that can occur when processing a jeff file.
31#[derive(Debug, Display, From, Error)]
32#[non_exhaustive]
33pub enum JeffError {
34    /// The jeff file is invalid.
35    #[display("Invalid jeff file: {_0}")]
36    #[from]
37    InvalidFile(::capnp::Error),
38    /// Invalid schema version.
39    #[display("Schema version {v} is too old. Expected {min}")]
40    VersionTooOld {
41        /// The invalid schema version.
42        v: semver::Version,
43        /// The minimum compatible version.
44        min: String,
45    },
46    /// The jeff file is too new.
47    #[display("Schema version {v} is too new. Expected {max}")]
48    VersionTooNew {
49        /// The invalid schema version.
50        v: semver::Version,
51        /// The maximum compatible version.
52        max: String,
53    },
54    /// Error while reading the internal structure.
55    #[from]
56    ReadError(reader::ReadError),
57}
58
59/// Direction of a port.
60#[derive(Clone, Copy, Debug, Display, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
61pub enum Direction {
62    /// Input to a node.
63    #[default]
64    Incoming = 0,
65    /// Output from a node.
66    Outgoing = 1,
67}
68
69impl Direction {
70    /// Incoming and outgoing directions.
71    pub const BOTH: [Direction; 2] = [Direction::Incoming, Direction::Outgoing];
72
73    /// Returns the opposite direction.
74    #[inline(always)]
75    pub fn reverse(self) -> Direction {
76        match self {
77            Direction::Incoming => Direction::Outgoing,
78            Direction::Outgoing => Direction::Incoming,
79        }
80    }
81}