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, Derive, Discriminant, DispatchArm, DispatchDef, Endian, EnumDef, Field,
35    Item, Kind, LayoutDef, Len, MessageDef, Param, Presence, Range, Scalar, Segment, Stated, Unit,
36    Variant,
37};
38#[cfg(feature = "walk")]
39pub use root::{Root, Spelling};
40pub use validate::{Invalid, validate};
41
42/// Used by `layline-derive`. Not covered by semver.
43#[cfg(feature = "walk")]
44#[doc(hidden)]
45pub mod __derive {
46    pub use crate::model::split_ref;
47    pub use crate::root::respan;
48    pub use crate::validate::validate_container;
49    pub use crate::walk::row::FieldDef;
50    pub use crate::walk::{
51        Check, ChoiceParts, DispatchParts, EnumParts, LayoutParts, MessageParts, WidthAttr,
52        choice_parts, dispatch_parts, enum_parts, field_rows, kind_ty, layout_parts, message_parts,
53        width_attr,
54    };
55}
56
57/// Why a model could not be turned into code.
58#[cfg(feature = "walk")]
59#[derive(Debug)]
60pub enum Error {
61    /// An item is invalid. Holds the item name and the reason.
62    ///
63    /// Unparsable names, type paths and derive paths are reported here too.
64    Invalid(String, Invalid),
65    /// Each item is valid, but the model as a whole is not.
66    ///
67    /// For example, a type of infinite size, or a verbatim item that declares a codec.
68    Refused(String),
69    /// The generated source did not parse. This is a bug in `layline-codegen`.
70    Internal(String),
71}
72
73#[cfg(feature = "walk")]
74impl core::fmt::Display for Error {
75    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
76        match self {
77            Self::Invalid(name, why) => write!(f, "{name}: {why}"),
78            Self::Refused(why) => f.write_str(why),
79            Self::Internal(why) => write!(f, "internal error: {why}"),
80        }
81    }
82}
83
84#[cfg(feature = "walk")]
85impl core::error::Error for Error {}