Skip to main content

layline_codegen/
lib.rs

1//! Rust source generation for layline.
2//!
3//! Without features, the crate is `no_std` and provides the model types and [`validate()`].
4//!
5//! | Feature | Enables | Default |
6//! |---|---|---|
7//! | `walk` | [`Root`] and [`Error`]. Adds `proc-macro2`, `quote` and `syn`. | No |
8//! | `emit` | [`emit`], which writes source files. Implies `walk`. | Yes |
9//!
10//! The stable API is the model types, [`validate()`], [`Error`], [`Root`] and [`emit`].
11//! Model structs are `#[non_exhaustive]`. Build them with `new` and the `with_*` methods.
12
13#![cfg_attr(not(feature = "walk"), no_std)]
14
15extern crate alloc;
16
17mod model;
18mod validate;
19#[cfg(feature = "walk")]
20mod walk;
21
22#[cfg(feature = "emit")]
23mod audit;
24#[cfg(feature = "emit")]
25pub mod emit;
26#[cfg(feature = "walk")]
27mod root;
28#[cfg(feature = "emit")]
29mod source;
30
31pub use layline_core::table::StatedUnit;
32pub use model::{
33    Absence, Arm, BitOrder, By, ChoiceDef, Collection, Container, Count, CoverFrom, CoverTo,
34    Coverage, Covered, Discriminant, DispatchArm, DispatchDef, Endian, EnumDef, Field, Item, Kind,
35    LayoutDef, Len, MessageDef, Param, Presence, Range, Scalar, Segment, Stated, Unit, Variant,
36};
37#[cfg(feature = "walk")]
38pub use root::{Root, Spelling};
39pub use validate::{Invalid, validate};
40
41/// Used by `layline-derive`. Not covered by semver.
42#[cfg(feature = "walk")]
43#[doc(hidden)]
44pub mod __derive {
45    pub use crate::model::split_ref;
46    pub use crate::root::respan;
47    pub use crate::validate::validate_container;
48    pub use crate::walk::row::FieldDef;
49    pub use crate::walk::{
50        Check, ChoiceParts, DispatchParts, EnumParts, LayoutParts, MessageParts, WidthAttr,
51        choice_parts, dispatch_parts, enum_parts, field_rows, kind_ty, layout_parts, message_parts,
52        width_attr,
53    };
54}
55
56/// Why a model could not be turned into code.
57#[cfg(feature = "walk")]
58#[derive(Debug)]
59pub enum Error {
60    /// An item is invalid. Holds the item name and the reason.
61    ///
62    /// Unparsable names, type paths and derive paths are reported here too.
63    Invalid(String, Invalid),
64    /// Each item is valid, but the model as a whole is not.
65    ///
66    /// For example, a type of infinite size, or a verbatim item that declares a codec.
67    Refused(String),
68    /// The generated source did not parse. This is a bug in `layline-codegen`.
69    Internal(String),
70}
71
72#[cfg(feature = "walk")]
73impl core::fmt::Display for Error {
74    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
75        match self {
76            Self::Invalid(name, why) => write!(f, "{name}: {why}"),
77            Self::Refused(why) => f.write_str(why),
78            Self::Internal(why) => write!(f, "internal error: {why}"),
79        }
80    }
81}
82
83#[cfg(feature = "walk")]
84impl core::error::Error for Error {}