Skip to main content

predsync/
lib.rs

1//! Public Rust API for predsync.
2//!
3//! This is the user-facing crate. It re-exports the schema, codec and compiler
4//! crates under namespace modules and also surfaces the most common types at
5//! the root. The command-line tool is installed with `cargo install predsync`.
6//!
7//! ```
8//! #[cfg(feature = "compiler")]
9//! fn main() {
10//! use predsync::{parse_schema, Decoder, DecoderConfig, Encoder, EncoderConfig, Slot, Value};
11//!
12//! let set = parse_schema(
13//!     "name: telemetry\nsync: { refresh: 8, reset: 32 }\nfields:\n  - name: x\n    type: f64\n    quantise: 0.01\n    range: [-100.0, 100.0]\n",
14//! )
15//! .unwrap()
16//! .set;
17//! let mut encoder = Encoder::new(&set, 0, EncoderConfig::default()).unwrap();
18//! let mut decoder = Decoder::new(&set, DecoderConfig { resync_n: 1 }).unwrap();
19//!
20//! let values = [Value::Scalar(Slot::from_f64(1.25))];
21//! let plan = encoder.plan(&values, 0).unwrap();
22//! let mut frame = vec![0u8; plan.len];
23//! let info = encoder.commit(plan, &mut frame).unwrap();
24//! frame.truncate(info.len);
25//! assert_eq!(decoder.feed(&frame).unwrap().fields[0].value, values[0]);
26//! }
27//! #[cfg(not(feature = "compiler"))]
28//! fn main() {}
29//! ```
30//!
31//! Embedded users should disable defaults. `compiler` implies `std`, so a
32//! `no_std` build needs `default-features = false` and, if allocation is
33//! available, the `alloc` feature alone.
34
35#![cfg_attr(not(feature = "std"), no_std)]
36
37#[cfg(feature = "compiler")]
38pub use predsync_compiler as compiler;
39pub use predsync_core as core;
40pub use predsync_schema as schema;
41
42pub use predsync_core::ScalarType;
43pub use predsync_core::{
44    BoundedValue, ConfigError, DecodeError, Decoder, DecoderConfig, EncodeError, EncodeInfo,
45    Encoder, EncoderConfig, PredictorBuilder, Presence, Slot, Validity, ValueRef,
46};
47#[cfg(feature = "alloc")]
48pub use predsync_core::{Decoded, FieldOut, Plan, PredictorRegistry, Value};
49
50#[cfg(feature = "alloc")]
51pub use predsync_schema::{
52    EnumDef, FieldSchema, MessageSchema, ParsedSchema, PredictorSpec, SchemaSet, Warning,
53};
54pub use predsync_schema::{
55    FieldType, PredictorKind, PredictorParamValue, SchemaError, StorageLayout, SyncDefaults,
56    ToleranceMode, ToleranceSpec, WarningKind,
57};
58
59#[cfg(feature = "compiler")]
60pub use predsync_compiler::{
61    Builder, CodegenError, GeneratedC, generate_c, generate_python, generate_rust,
62    generate_rust_with_options, parse_schema,
63};
64
65/// Returns the facade version marker used by the workspace smoke tests.
66pub fn version_marker() -> &'static str {
67    env!("CARGO_PKG_VERSION")
68}
69
70#[cfg(all(test, feature = "compiler"))]
71mod tests {
72    #[test]
73    fn facade_reexports_compiler() {
74        let set = super::parse_schema(
75            "name: telemetry\nsync: { refresh: 8, reset: 32 }\nfields:\n  - name: x\n    type: f64\n    quantise: 0.01\n    range: [-100.0, 100.0]\n",
76        )
77        .unwrap()
78        .set;
79        assert_eq!(set.messages.len(), 1);
80    }
81}