use std::str::FromStr; use crate::{Error, Time}; use smallvec::SmallVec; /// A container for just enough bytes to hold the largest-possible [`time`](Time) instance. /// It's used in conjunction with #[derive(Default, Clone)] pub struct TimeBuf { buf: SmallVec<[u8; Time::MAX.size()]>, } impl TimeBuf { /// Represent this instance as standard string, serialized in a format compatible with /// signature fields in Git commits, also known as anything parseable as [raw format](function::parse_header()). pub fn as_str(&self) -> &str { // Time serializes as ASCII, which is a subset of UTF-8. let time_bytes = self.buf.as_slice(); std::str::from_utf8(time_bytes).expect("time serializes as valid UTF-8") } /// Clear the previous content. fn clear(&mut self) { self.buf.clear(); } } impl Time { /// Serialize this instance into `buf`, exactly as it would appear in the header of a Git commit, /// and return `buf` as `&str` for easy consumption. pub fn to_str<'a>(&self, buf: &'a mut TimeBuf) -> &'a str { buf.clear(); self.write_to(&mut buf.buf) .expect("write to memory of just the right size cannot fail"); buf.as_str() } } impl FromStr for Time { type Err = Error; fn from_str(s: &str) -> Result { crate::parse_header(s).ok_or_else(|| Error::new_with_input("invalid time", s)) } } pub(crate) mod function; mod git; mod raw; mod relative;