1use std::io;
2use std::str::FromStr;
3
4use crate::{Error, Time};
5
6#[derive(Default, Clone)]
8pub struct TimeBuf {
9 end_idx: usize,
11 buf: [u8; Time::MIN.size()],
13}
14
15struct TimeBufWriter<'a>(&'a mut TimeBuf);
20
21impl io::Write for TimeBufWriter<'_> {
22 fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
23 let idx = self.0.end_idx;
24 let end_idx = idx
25 .checked_add(buf.len())
26 .ok_or_else(|| io::Error::from(io::ErrorKind::OutOfMemory))?;
27 if end_idx > Time::MIN.size() {
28 return Err(io::Error::from(io::ErrorKind::StorageFull));
29 }
30 self.0.buf[idx..end_idx].copy_from_slice(buf);
31 self.0.end_idx = end_idx;
32 Ok(buf.len())
33 }
34
35 fn flush(&mut self) -> io::Result<()> {
36 Ok(())
37 }
38}
39
40impl TimeBuf {
41 pub fn as_str(&self) -> &str {
44 let time_bytes = &self.buf[..self.end_idx];
46 std::str::from_utf8(time_bytes).expect("time serializes as valid UTF-8")
47 }
48
49 fn clear(&mut self) {
51 self.end_idx = 0;
52 }
53}
54
55impl Time {
56 pub fn to_str<'a>(&self, buf: &'a mut TimeBuf) -> &'a str {
59 buf.clear();
60 self.write_to(&mut TimeBufWriter(buf))
61 .expect("write to memory of just the right size cannot fail");
62 buf.as_str()
63 }
64}
65
66impl FromStr for Time {
67 type Err = Error;
68
69 fn from_str(s: &str) -> Result<Self, Self::Err> {
70 crate::parse_header(s).ok_or_else(|| Error::new_with_input("invalid time", s))
71 }
72}
73
74pub(crate) mod function;
75mod git;
76mod raw;
77mod relative;