Skip to main content

Tagged

Struct Tagged 

Source
pub struct Tagged<T>(pub T, pub Option<String>);
Expand description

Capture and emit the resolved YAML tag attached to a value.

The first field contains the value and the second contains its resolved tag. Some(tag) is how an arbitrary tag is put on a node: the serializer emits it as that node’s explicit tag. None requests no tag and serializes exactly as the unwrapped T would, which is also what deserializing an untagged node produces.

Resolution deliberately discards source spelling: for example, !!str is captured as Some("tag:yaml.org,2002:str"), while a local tag such as !widget is captured as Some("!widget"). Tags resolved through a %TAG directive are captured as their complete URI. A present tag always has a non-empty resolved identity; Some("") is invalid when serializing rather than an alternative spelling of None.

During serialization, a resolved global tag URI is emitted using YAML’s verbatim form (!<...>). This preserves the resolved tag identity across a round trip, but does not reproduce tag handles or %TAG directives. Local tags are emitted directly. A non-local identity must have valid absolute-URI structure; characters requiring URI escaping are percent-encoded on output.

With the comments feature, Tagged<Commented<T>> and Commented<Tagged<T>> are both supported. Tag capture remains subject to the deserializer’s normal tag semantics and reject_unsupported_tags option.

§Clashing tags

A YAML node carries at most one tag, and serializing certain values produces a tag without being asked: !!binary for a byte buffer, or the variant tag emitted under the tagged_enums option. Where such a generated tag meets the wrapper’s own on one node, they must match or wrappers’ own tag must be None. Otherwise, error is reported.

Deserializers other than serde-saphyr do not provide YAML tag metadata; when used with one of them, Tagged<T> behaves transparently and gets None as its tag.

use serde_saphyr::{Tagged, from_str, to_string};

let value: Tagged<String> = from_str("!!str value").unwrap();
assert_eq!(
    value,
    Tagged("value".into(), Some("tag:yaml.org,2002:str".into())),
);
assert_eq!(
    to_string(&value).unwrap(),
    "!<tag:yaml.org,2002:str> value\n",
);

use serde_saphyr::Commented;

let tagged_comment: Tagged<Commented<String>> =
    from_str("!widget value # note").unwrap();
assert_eq!(
    tagged_comment,
    Tagged(
        Commented("value".into(), "note".into()),
        Some("!widget".into()),
    ),
);

let commented_tag: Commented<Tagged<String>> =
    from_str("!widget value # note").unwrap();
assert_eq!(
    commented_tag,
    Commented(
        Tagged("value".into(), Some("!widget".into())),
        "note".into(),
    ),
);

Tuple Fields§

§0: T§1: Option<String>

Trait Implementations§

Source§

impl<T: Clone> Clone for Tagged<T>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Tagged<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 Tagged<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: Eq> Eq for Tagged<T>

Source§

impl<T: PartialEq> PartialEq for Tagged<T>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T: Serialize> Serialize for Tagged<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: PartialEq> StructuralPartialEq for Tagged<T>

Auto Trait Implementations§

§

impl<T> Freeze for Tagged<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Tagged<T>
where T: RefUnwindSafe,

§

impl<T> Send for Tagged<T>
where T: Send,

§

impl<T> Sync for Tagged<T>
where T: Sync,

§

impl<T> Unpin for Tagged<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Tagged<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Tagged<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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.