pub struct Commented<T>(pub T, pub String);Expand description
Attach an inline YAML comment to a value when serializing.
Requires the comments feature, which is enabled by default.
This wrapper lets you annotate a scalar with an inline YAML comment that is
emitted after the value when using block style. The typical form is:
value # comment. This is the most useful when deserializing the anchor
reference (so a human reader can see what a referenced value represents).
Comment is also captured into its field when deserializing YAML.
Behavior
- Block style (default): the comment appears after the scalar on the same line.
- Flow style (inside
[ ... ]or{ ... }): comments are suppressed to keep the flow representation compact and unambiguous. - Complex values (sequences/maps/structs): the comment is ignored; only the inner value is serialized to preserve indentation and layout.
- Newlines in comments are sanitized to spaces so the comment remains on a single line (e.g., “a\nb” becomes “a b”).
- Deserialization of
Commented<T>captures nearby source comments when theserde-saphyrdeserializer can provide them. Other deserializers treat it transparently and produce an empty comment string. - Comment capture is use-site oriented for replayed YAML. Comments from an
anchor definition are not copied through aliases or merge keys; a field
materialized by
<<: *defaultsdoes not inherit comments that were written above the field inside&defaults. - For container values, comments attached to the parent value itself are
captured only by
Commented<Container>and are not inherited by the first child field or element. A comment inside the container, directly above a child key or element, is captured by that child. - When an alias to a container is used as a nested value, leading comments
above the alias follow the same inside-container rule. For example,
root:\n # comment\n *defaultsleaves the comment available to the expanded container’s first child rather than capturing it on the alias use.
Examples
Basic scalar with a comment in block style:
use serde::Serialize;
// Re-exported from the crate root
use serde_saphyr::Commented;
let out = serde_saphyr::to_string(&Commented(42, "answer".to_string())).unwrap();
assert_eq!(out, "42 # answer\n");As a mapping value, still inline:
use serde::Serialize;
use serde_saphyr::Commented;
#[derive(Serialize)]
struct S { xn: Commented<i32> }
let s = S { xn: Commented(5, "send five starships first".into()) };
let out = serde_saphyr::to_string(&s).unwrap();
assert_eq!(out, "xn: 5 # send five starships first\n");Important: Comments are suppressed in flow contexts (no # appears), and
ignored for complex inner values during serialization. During deserialization,
parent-side comments on a container such as root: # comment are captured by
Commented<Container> only; comments inside the container, directly above the
first child key or element, remain available to that child. The same applies
to leading comments above a nested alias whose target is a container.
Tuple Fields§
§0: T§1: StringTrait Implementations§
Source§impl<'de, T: Deserialize<'de>> Deserialize<'de> for Commented<T>
Available on crate feature comments only.
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Commented<T>
comments only.