Skip to main content

gix_actor/
identity.rs

1use bstr::ByteSlice;
2
3use crate::{signature::decode, Identity, IdentityRef};
4
5impl<'a> IdentityRef<'a> {
6    /// Deserialize an identity from the given `data`.
7    ///
8    /// Typical input is `Name <name@example.com> 1700000000 +0000`.
9    pub fn from_bytes(mut data: &'a [u8]) -> Result<Self, gix_error::ValidationError> {
10        Self::from_bytes_consuming(&mut data)
11    }
12
13    /// Deserialize an identity from the given `data` and advance it past the identity.
14    ///
15    /// Typical input is `Name <name@example.com> 1700000000 +0000`; on success,
16    /// `data` points to the bytes immediately after the closing `>`.
17    pub fn from_bytes_consuming(data: &mut &'a [u8]) -> Result<Self, gix_error::ValidationError> {
18        decode::identity(data)
19    }
20
21    /// Create an owned instance from this shared one.
22    pub fn to_owned(&self) -> Identity {
23        Identity {
24            name: self.name.to_owned(),
25            email: self.email.to_owned(),
26        }
27    }
28
29    /// Trim whitespace surrounding the name and email and return a new identity.
30    pub fn trim(&self) -> IdentityRef<'a> {
31        IdentityRef {
32            name: self.name.trim().as_bstr(),
33            email: self.email.trim().as_bstr(),
34        }
35    }
36}
37
38mod write {
39    use crate::{signature::write::validated_token, Identity, IdentityRef};
40
41    /// Output
42    impl Identity {
43        /// Serialize this instance to `out` in the git serialization format for signatures (but without timestamp).
44        pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
45            self.to_ref().write_to(out)
46        }
47    }
48
49    impl IdentityRef<'_> {
50        /// Serialize this instance to `out` in the git serialization format for signatures (but without timestamp).
51        pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
52            out.write_all(validated_token(self.name).map_err(std::io::Error::other)?)?;
53            out.write_all(b" ")?;
54            out.write_all(b"<")?;
55            out.write_all(validated_token(self.email).map_err(std::io::Error::other)?)?;
56            out.write_all(b">")
57        }
58    }
59}
60
61mod impls {
62    use crate::{Identity, IdentityRef, Signature, SignatureRef};
63
64    impl Identity {
65        /// Borrow this instance as immutable
66        pub fn to_ref(&self) -> IdentityRef<'_> {
67            IdentityRef {
68                name: self.name.as_ref(),
69                email: self.email.as_ref(),
70            }
71        }
72    }
73
74    impl From<IdentityRef<'_>> for Identity {
75        fn from(other: IdentityRef<'_>) -> Identity {
76            let IdentityRef { name, email } = other;
77            Identity {
78                name: name.to_owned(),
79                email: email.to_owned(),
80            }
81        }
82    }
83
84    impl<'a> From<&'a Identity> for IdentityRef<'a> {
85        fn from(other: &'a Identity) -> IdentityRef<'a> {
86            other.to_ref()
87        }
88    }
89
90    impl From<Signature> for Identity {
91        fn from(Signature { name, email, time: _ }: Signature) -> Self {
92            Identity { name, email }
93        }
94    }
95
96    impl<'a> From<SignatureRef<'a>> for IdentityRef<'a> {
97        fn from(SignatureRef { name, email, time: _ }: SignatureRef<'a>) -> Self {
98            IdentityRef { name, email }
99        }
100    }
101}