gix_mailmap/snapshot/
signature.rs

1use std::borrow::Cow;
2
3use bstr::{BStr, BString, ByteSlice};
4
5/// A signature like [`gix_actor::Signature`], but with all string fields being a `Cow`.
6#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct Signature<'a> {
9    /// The possibly mapped name.
10    pub name: Cow<'a, BStr>,
11    /// The possibly mapped email.
12    pub email: Cow<'a, BStr>,
13    /// The time stamp at which the signature is performed.
14    pub time: gix_date::Time,
15}
16
17impl<'a> From<Signature<'a>> for gix_actor::Signature {
18    fn from(s: Signature<'a>) -> Self {
19        gix_actor::Signature {
20            name: s.name.into_owned(),
21            email: s.email.into_owned(),
22            time: s.time,
23        }
24    }
25}
26
27impl<'a> From<gix_actor::SignatureRef<'a>> for Signature<'a> {
28    fn from(s: gix_actor::SignatureRef<'a>) -> Self {
29        Signature {
30            name: s.name.into(),
31            email: s.email.into(),
32            time: s.time,
33        }
34    }
35}
36
37/// A resolved signature with borrowed fields for a mapped `name` and/or `email`.
38pub struct ResolvedSignature<'a> {
39    /// The mapped name.
40    pub name: Option<&'a BStr>,
41    /// The mapped email.
42    pub email: Option<&'a BStr>,
43}
44
45impl<'a> ResolvedSignature<'a> {
46    pub(crate) fn try_new(
47        new_email: Option<&'a BString>,
48        matched_email: &'a BStr,
49        current_email: &'_ BStr,
50        new_name: Option<&'a BString>,
51    ) -> Option<Self> {
52        let new_email = new_email
53            .map(|n| n.as_bstr())
54            .or_else(|| (matched_email != current_email).then_some(matched_email));
55        match (new_email, new_name) {
56            (None, None) => None,
57            (new_email, new_name) => Some(ResolvedSignature {
58                email: new_email.map(|v| v.as_bstr()),
59                name: new_name.map(|v| v.as_bstr()),
60            }),
61        }
62    }
63}