use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use crate::value::Value;
const CORE_PREFIX: &str = "tag:yaml.org,2002:";
#[derive(Clone, Debug)]
pub struct Tag {
handle: String,
suffix: String,
}
impl Tag {
#[must_use]
pub fn new(handle: impl Into<String>, suffix: impl Into<String>) -> Self {
let handle = handle.into();
let suffix = suffix.into();
if handle.is_empty() {
if let Some(rest) = suffix.strip_prefix(CORE_PREFIX) {
return Self {
handle: CORE_PREFIX.to_owned(),
suffix: rest.to_owned(),
};
}
if let Some(rest) = suffix.strip_prefix('!') {
return Self {
handle: "!".to_owned(),
suffix: rest.to_owned(),
};
}
}
Self { handle, suffix }
}
pub(crate) fn is_core_collection(&self) -> bool {
self.is_core() && matches!(self.suffix(), "seq" | "map" | "set" | "omap" | "pairs")
}
pub(crate) fn serde_name(&self) -> String {
if self.handle == "!" && !self.suffix.is_empty() && !self.suffix.starts_with(['!', '<']) {
self.suffix.clone()
} else {
format!("<{}{}>", self.handle, self.suffix)
}
}
pub(crate) fn from_serde_name(name: &str) -> Self {
match name
.strip_prefix('<')
.and_then(|rest| rest.strip_suffix('>'))
{
Some(full) => Self::new("", full),
None if name.starts_with('!') => Self::new("", name),
None => Self::local(name),
}
}
fn full(&self) -> impl Iterator<Item = u8> + '_ {
self.handle.bytes().chain(self.suffix.bytes())
}
#[must_use]
pub fn core(suffix: impl Into<String>) -> Self {
Self::new(CORE_PREFIX, suffix)
}
#[must_use]
pub fn local(suffix: impl Into<String>) -> Self {
Self::new("!", suffix)
}
#[must_use]
pub fn handle(&self) -> &str {
&self.handle
}
#[must_use]
pub fn suffix(&self) -> &str {
&self.suffix
}
#[must_use]
pub fn is_core(&self) -> bool {
self.handle == CORE_PREFIX
}
}
impl PartialEq for Tag {
fn eq(&self, other: &Self) -> bool {
self.handle.len() + self.suffix.len() == other.handle.len() + other.suffix.len()
&& self.full().eq(other.full())
}
}
impl Eq for Tag {}
impl Hash for Tag {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write(self.handle.as_bytes());
state.write(self.suffix.as_bytes());
state.write_u8(0xff);
}
}
impl PartialOrd for Tag {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Tag {
fn cmp(&self, other: &Self) -> Ordering {
self.full().cmp(other.full())
}
}
impl fmt::Display for Tag {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.handle == "!" {
write!(formatter, "!{}", self.suffix)
} else if self.handle == CORE_PREFIX {
write!(formatter, "!!{}", self.suffix)
} else {
write!(formatter, "{}{}", self.handle, self.suffix)
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TaggedValue {
pub tag: Tag,
pub value: Value,
}
impl TaggedValue {
#[must_use]
pub fn new(tag: Tag, value: Value) -> Self {
Self { tag, value }
}
}
#[cfg(test)]
mod tests {
use super::{Tag, TaggedValue};
use crate::value::Value;
#[test]
fn local_and_core_tags_display_canonically() {
assert_eq!(Tag::local("Custom").to_string(), "!Custom");
assert_eq!(Tag::core("str").to_string(), "!!str");
assert_eq!(
Tag::new("tag:example.com,2026:", "foo").to_string(),
"tag:example.com,2026:foo"
);
}
#[test]
fn serde_names_read_back() {
for tag in [
Tag::local("Foo"),
Tag::local(""),
Tag::local("!"),
Tag::local("<x>"),
Tag::local("a:b"),
Tag::core("set"),
Tag::new("tag:example.com,2000:", "foo"),
Tag::new("", "tag:x"),
] {
assert_eq!(Tag::from_serde_name(&tag.serde_name()), tag, "{tag}");
}
assert_eq!(Tag::local("Foo").serde_name(), "Foo");
assert_eq!(Tag::core("set").serde_name(), "<tag:yaml.org,2002:set>");
}
#[test]
fn tags_compare_by_full_name() {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
fn hash(tag: &Tag) -> u64 {
let mut hasher = DefaultHasher::new();
tag.hash(&mut hasher);
hasher.finish()
}
let verbatim = Tag::new("", "tag:yaml.org,2002:str");
assert_eq!(verbatim, Tag::core("str"));
assert!(verbatim.is_core());
assert_eq!(verbatim.suffix(), "str");
assert_eq!(Tag::new("", "!x"), Tag::local("x"));
let handle = Tag::new("tag:example.com,2000:app/", "foo");
let full = Tag::new("", "tag:example.com,2000:app/foo");
assert_eq!(handle, full);
assert_eq!(hash(&handle), hash(&full));
assert_eq!(handle.cmp(&full), std::cmp::Ordering::Equal);
assert_ne!(Tag::local("a"), Tag::local("b"));
assert!(Tag::local("a") < Tag::local("b"));
}
#[test]
fn core_tag_is_recognized() {
assert!(Tag::core("int").is_core());
assert!(!Tag::local("int").is_core());
}
#[test]
fn tagged_value_keeps_its_tag_and_payload() {
let tagged = TaggedValue::new(Tag::local("Custom"), Value::from(1_u64));
assert_eq!(tagged.tag.to_string(), "!Custom");
assert_eq!(tagged.value, Value::from(1_u64));
}
}