Skip to main content

gix_actor/signature/
mod.rs

1mod _ref {
2    use bstr::ByteSlice;
3    use winnow::{error::StrContext, prelude::*};
4
5    use crate::{signature::decode, IdentityRef, Signature, SignatureRef};
6
7    /// Lifecycle
8    impl<'a> SignatureRef<'a> {
9        /// Deserialize a signature from the given `data`.
10        pub fn from_bytes<E>(mut data: &'a [u8]) -> Result<SignatureRef<'a>, winnow::error::ErrMode<E>>
11        where
12            E: winnow::error::ParserError<&'a [u8]> + winnow::error::AddContext<&'a [u8], StrContext>,
13        {
14            decode.parse_next(&mut data)
15        }
16
17        /// Try to parse the timestamp and create an owned instance from this shared one.
18        pub fn to_owned(&self) -> Result<Signature, gix_date::Error> {
19            Ok(Signature {
20                name: self.name.to_owned(),
21                email: self.email.to_owned(),
22                time: self.time()?,
23            })
24        }
25    }
26
27    /// Access
28    impl<'a> SignatureRef<'a> {
29        /// Trim the whitespace surrounding the `name`, `email` and `time` and return a new signature.
30        pub fn trim(&self) -> SignatureRef<'a> {
31            SignatureRef {
32                name: self.name.trim().as_bstr(),
33                email: self.email.trim().as_bstr(),
34                time: self.time.trim(),
35            }
36        }
37
38        /// Return the actor's name and email, effectively excluding the timestamp of this signature.
39        pub fn actor(&self) -> IdentityRef<'a> {
40            IdentityRef {
41                name: self.name,
42                email: self.email,
43            }
44        }
45
46        /// Parse only the seconds since unix epoch from the `time` field, or silently default to 0
47        /// if parsing fails. Note that this ignores the timezone, so it can parse otherwise broken dates.
48        ///
49        /// For a fallible and more complete, but slower version, use [`time()`](Self::time).
50        pub fn seconds(&self) -> gix_date::SecondsSinceUnixEpoch {
51            self.time
52                .trim()
53                .split(' ')
54                .next()
55                .and_then(|i| i.parse().ok())
56                .unwrap_or_default()
57        }
58
59        /// Parse the `time` field for access to the passed time since unix epoch, and the time offset.
60        /// The format is expected to be [raw](gix_date::parse_header()).
61        pub fn time(&self) -> Result<gix_date::Time, gix_date::Error> {
62            self.time.parse()
63        }
64    }
65}
66
67mod convert {
68    use gix_date::parse::TimeBuf;
69
70    use crate::{Signature, SignatureRef};
71
72    impl Signature {
73        /// Borrow this instance as immutable, serializing the `time` field into `buf`.
74        ///
75        /// Commonly used as [`signature.to_ref(&mut TimeBuf::default())`](TimeBuf::default).
76        pub fn to_ref<'a>(&'a self, time_buf: &'a mut TimeBuf) -> SignatureRef<'a> {
77            SignatureRef {
78                name: self.name.as_ref(),
79                email: self.email.as_ref(),
80                time: self.time.to_str(time_buf),
81            }
82        }
83    }
84
85    /// Note that this conversion is lossy due to the lenient parsing of the [`time`](SignatureRef::time) field.
86    impl From<SignatureRef<'_>> for Signature {
87        fn from(other: SignatureRef<'_>) -> Signature {
88            Signature {
89                name: other.name.to_owned(),
90                email: other.email.to_owned(),
91                time: other.time().unwrap_or_default(),
92            }
93        }
94    }
95}
96
97pub(crate) mod write {
98    use bstr::{BStr, ByteSlice};
99    use gix_date::parse::TimeBuf;
100
101    use crate::{Signature, SignatureRef};
102
103    /// Output
104    impl Signature {
105        /// Serialize this instance to `out` in the git serialization format for actors.
106        pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
107            let mut buf = TimeBuf::default();
108            self.to_ref(&mut buf).write_to(out)
109        }
110        /// Computes the number of bytes necessary to serialize this signature
111        pub fn size(&self) -> usize {
112            self.name.len() + 2 /* space <*/ + self.email.len() +  2 /* > space */ + self.time.size()
113        }
114    }
115
116    impl SignatureRef<'_> {
117        /// Serialize this instance to `out` in the git serialization format for actors.
118        pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
119            out.write_all(validated_token(self.name).map_err(std::io::Error::other)?)?;
120            out.write_all(b" ")?;
121            out.write_all(b"<")?;
122            out.write_all(validated_token(self.email).map_err(std::io::Error::other)?)?;
123            out.write_all(b"> ")?;
124            out.write_all(validated_token(self.time.into()).map_err(std::io::Error::other)?)
125        }
126        /// Computes the number of bytes necessary to serialize this signature
127        pub fn size(&self) -> usize {
128            self.name.len() + 2 /* space <*/ + self.email.len() +  2 /* > space */ + self.time.len()
129        }
130    }
131
132    pub(crate) fn validated_token(name: &BStr) -> Result<&BStr, gix_error::ValidationError> {
133        if name.find_byteset(b"<>\n").is_some() {
134            return Err(gix_error::ValidationError::new_with_input(
135                "Signature name or email must not contain '<', '>' or \\n",
136                name,
137            ));
138        }
139        Ok(name)
140    }
141}
142
143///
144pub mod decode;
145pub use decode::function::decode;