pub struct Commented<T>(pub T, pub String);Expand description
Attach an inline YAML comment to a value when serializing.
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.
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>ignores comments: it behaves likeTand produces an empty comment string.
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 { n: Commented<i32> }
let s = S { n: Commented(5, "send five starships first".into()) };
let out = serde_saphyr::to_string(&s).unwrap();
assert_eq!(out, "n: 5 # send five starships first\n");Important: Comments are suppressed in flow contexts (no # appears), and
ignored for complex inner values. Value with Commented wrapper will be
deserializaed correctly as well, but deserialization of comment is
currently not supported.
Tuple Fields§
§0: T§1: StringTrait Implementations§
Source§impl<'de, T: Deserialize<'de>> Deserialize<'de> for Commented<T>
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Commented<T>
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Deserialize this value from the given Serde deserializer. Read more
impl<T: Eq> Eq for Commented<T>
impl<T> StructuralPartialEq for Commented<T>
Auto Trait Implementations§
impl<T> Freeze for Commented<T>where
T: Freeze,
impl<T> RefUnwindSafe for Commented<T>where
T: RefUnwindSafe,
impl<T> Send for Commented<T>where
T: Send,
impl<T> Sync for Commented<T>where
T: Sync,
impl<T> Unpin for Commented<T>where
T: Unpin,
impl<T> UnwindSafe for Commented<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more