facet_yaml/
lib.rs

1//! YAML serialization and deserialization for Facet types.
2//!
3//! This crate provides YAML parsing using a streaming event-based parser (saphyr-parser)
4//! and integrates with the facet reflection system for type-safe deserialization.
5//!
6//! # Example
7//!
8//! ```
9//! use facet::Facet;
10//! use facet_yaml::from_str;
11//!
12//! #[derive(Facet, Debug, PartialEq)]
13//! struct Config {
14//!     name: String,
15//!     port: u16,
16//! }
17//!
18//! let yaml = "name: myapp\nport: 8080";
19//! let config: Config = from_str(yaml).unwrap();
20//! assert_eq!(config.name, "myapp");
21//! assert_eq!(config.port, 8080);
22//! ```
23
24#![cfg_attr(not(feature = "std"), no_std)]
25#![warn(missing_docs)]
26#![warn(clippy::std_instead_of_core)]
27#![warn(clippy::std_instead_of_alloc)]
28
29extern crate alloc;
30
31mod deserialize;
32mod error;
33#[cfg(feature = "std")]
34mod serialize;
35
36pub use deserialize::{from_str, from_str_borrowed};
37pub use error::{YamlError, YamlErrorKind};
38#[cfg(feature = "std")]
39pub use serialize::{to_string, to_writer};
40
41mod yaml;
42pub use yaml::Yaml;
43
44#[cfg(feature = "axum")]
45mod axum;
46#[cfg(feature = "axum")]
47pub use self::axum::YamlRejection;
48
49// Re-export span types from facet-reflect
50pub use facet_reflect::{Span, Spanned};
51
52/// Serde-compatible extension attributes for YAML serialization.
53///
54/// This module provides extension attributes that mirror serde's attribute syntax,
55/// allowing users to write `#[facet(serde::rename = "name")]` for field renaming.
56///
57/// Users import `use facet_yaml::serde;` to use these attributes.
58pub mod serde {
59    // Generate serde attribute grammar using the grammar DSL.
60    // This generates:
61    // - `Attr` enum with all serde attribute variants
62    // - `__attr!` macro that dispatches to attribute handlers and returns ExtensionAttr
63    // - `__parse_attr!` macro for parsing (internal use)
64    facet::define_attr_grammar! {
65        ns "serde";
66        crate_path ::facet_yaml::serde;
67
68        /// Serde-compatible attribute types for field and container configuration.
69        pub enum Attr {
70            /// Rename a field during serialization/deserialization.
71            ///
72            /// Usage: `#[facet(serde::rename = "new_name")]`
73            Rename(&'static str),
74        }
75    }
76}