Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Implement Display trait for serde_yaml::value::Tag #307

Merged
merged 1 commit into from Aug 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/value/tagged.rs
Expand Up @@ -7,7 +7,7 @@ use serde::de::{
};
use serde::ser::{Serialize, SerializeMap, Serializer};
use std::cmp::Ordering;
use std::fmt::{self, Debug};
use std::fmt::{self, Debug, Display};
use std::hash::{Hash, Hasher};

/// A representation of YAML's `!Tag` syntax, used for enums.
Expand Down Expand Up @@ -67,10 +67,12 @@ impl Tag {
/// let tag = Tag::new("Thing");
/// assert!(tag == "Thing");
/// assert!(tag == "!Thing");
/// assert!(tag.to_string() == "!Thing");
///
/// let tag = Tag::new("!Thing");
/// assert!(tag == "Thing");
/// assert!(tag == "!Thing");
/// assert!(tag.to_string() == "!Thing");
/// ```
///
/// Such a tag would serialize to `!Thing` in YAML regardless of whether a
Expand Down Expand Up @@ -153,6 +155,12 @@ impl Debug for Tag {
}
}

impl Display for Tag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "!{}", nobang(&self.string))
}
}

impl Serialize for TaggedValue {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down