Skip to main content

links_notation/binary/
mod.rs

1//! Binary links notation version 1: packets and the marker-point LiNo mapping.
2//!
3//! Wire specification and shared vectors live in `docs/protocol/` in the
4//! repository. This module supplies codecs; transports supply their own framing.
5//!
6//! ```
7//! use links_notation::binary::{parse_document, BinaryLinoCodec};
8//! let document = parse_document("() ((1 1))")?;
9//! let codec = BinaryLinoCodec::new();
10//! let bytes = codec.encode(&document)?;
11//! assert_eq!(codec.decode(&bytes)?, document);
12//! # Ok::<(), links_notation::binary::BinaryError>(())
13//! ```
14
15mod error;
16mod format;
17mod mapping;
18pub mod packet;
19
20pub use error::{BinaryError, BinaryResult};
21pub use format::{canonical, format_document, format_link, format_reference, parse_document};
22pub use mapping::{
23    decode_document, encode_document, encode_document_with_limits, BinaryLinoOptions, LinoDocument,
24};
25pub use packet::{ArityRange, DecodeLimits, LinksPacket, Reference, Section};
26
27/// Convenience codec for complete LiNo packets.
28#[derive(Clone, Copy, Debug, Default)]
29pub struct BinaryLinoCodec {
30    pub options: BinaryLinoOptions,
31    pub limits: DecodeLimits,
32}
33
34impl BinaryLinoCodec {
35    pub fn new() -> Self {
36        Self::default()
37    }
38
39    pub fn with_options(options: BinaryLinoOptions) -> Self {
40        Self {
41            options,
42            ..Self::default()
43        }
44    }
45
46    /// Encodes the supplied model without canonicalizing its groups.
47    pub fn encode(&self, document: &[crate::LiNo<String>]) -> BinaryResult<Vec<u8>> {
48        encode_document_with_limits(document, self.options, &self.limits)?.to_bytes()
49    }
50
51    /// Decodes exactly one complete packet, rejecting trailing bytes.
52    pub fn decode(&self, bytes: &[u8]) -> BinaryResult<LinoDocument> {
53        decode_document(&LinksPacket::from_bytes(bytes, &self.limits)?, &self.limits)
54    }
55}