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>