diff options
Diffstat (limited to 'vendor/gix-config/src/parse/comment.rs')
-rw-r--r-- | vendor/gix-config/src/parse/comment.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/vendor/gix-config/src/parse/comment.rs b/vendor/gix-config/src/parse/comment.rs new file mode 100644 index 000000000..6d4bb15ff --- /dev/null +++ b/vendor/gix-config/src/parse/comment.rs @@ -0,0 +1,50 @@ +use std::{borrow::Cow, fmt::Display}; + +use bstr::BString; + +use crate::parse::Comment; + +impl Comment<'_> { + /// Turn this instance into a fully owned one with `'static` lifetime. + #[must_use] + pub fn to_owned(&self) -> Comment<'static> { + Comment { + tag: self.tag, + text: Cow::Owned(self.text.as_ref().into()), + } + } + + /// Serialize this type into a `BString` for convenience. + /// + /// Note that `to_string()` can also be used, but might not be lossless. + #[must_use] + pub fn to_bstring(&self) -> BString { + let mut buf = Vec::new(); + self.write_to(&mut buf).expect("io error impossible"); + buf.into() + } + + /// Stream ourselves to the given `out`, in order to reproduce this comment losslessly. + pub fn write_to(&self, mut out: impl std::io::Write) -> std::io::Result<()> { + out.write_all(&[self.tag])?; + out.write_all(self.text.as_ref()) + } +} + +impl Display for Comment<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Display::fmt(&self.to_bstring(), f) + } +} + +impl From<Comment<'_>> for BString { + fn from(c: Comment<'_>) -> Self { + c.into() + } +} + +impl From<&Comment<'_>> for BString { + fn from(c: &Comment<'_>) -> Self { + c.to_bstring() + } +} |