Commented

Struct Commented 

Source
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 like T and 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: String

Trait Implementations§

Source§

impl<T: Clone> Clone for Commented<T>

Source§

fn clone(&self) -> Commented<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Commented<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, T: Deserialize<'de>> Deserialize<'de> for Commented<T>

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: PartialEq> PartialEq for Commented<T>

Source§

fn eq(&self, other: &Commented<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Serialize> Serialize for Commented<T>

Source§

fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Eq> Eq for Commented<T>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,