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::parse::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::parse::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    /// The Error produced by [`Signature::write_to()`].
104    #[derive(Debug, thiserror::Error)]
105    #[allow(missing_docs)]
106    pub(crate) enum Error {
107        #[error(r"Signature name or email must not contain '<', '>' or \n")]
108        IllegalCharacter,
109    }
110
111    impl From<Error> for std::io::Error {
112        fn from(err: Error) -> Self {
113            std::io::Error::other(err)
114        }
115    }
116
117    /// Output
118    impl Signature {
119        /// Serialize this instance to `out` in the git serialization format for actors.
120        pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
121            let mut buf = TimeBuf::default();
122            self.to_ref(&mut buf).write_to(out)
123        }
124        /// Computes the number of bytes necessary to serialize this signature
125        pub fn size(&self) -> usize {
126            self.name.len() + 2 /* space <*/ + self.email.len() +  2 /* > space */ + self.time.size()
127        }
128    }
129
130    impl SignatureRef<'_> {
131        /// Serialize this instance to `out` in the git serialization format for actors.
132        pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
133            out.write_all(validated_token(self.name)?)?;
134            out.write_all(b" ")?;
135            out.write_all(b"<")?;
136            out.write_all(validated_token(self.email)?)?;
137            out.write_all(b"> ")?;
138            out.write_all(validated_token(self.time.into())?)
139        }
140        /// Computes the number of bytes necessary to serialize this signature
141        pub fn size(&self) -> usize {
142            self.name.len() + 2 /* space <*/ + self.email.len() +  2 /* > space */ + self.time.len()
143        }
144    }
145
146    pub(crate) fn validated_token(name: &BStr) -> Result<&BStr, Error> {
147        if name.find_byteset(b"<>\n").is_some() {
148            return Err(Error::IllegalCharacter);
149        }
150        Ok(name)
151    }
152}
153
154///
155pub mod decode;
156pub use decode::function::decode;