gix_mailmap/lib.rs
1//! [Parse][parse()] .mailmap files as used in git repositories and remap names and emails
2//! using an [accelerated data-structure][Snapshot].
3//!
4//! ## Examples
5//!
6//! ```
7//! use gix_actor::SignatureRef;
8//!
9//! let input = b"
10//! Joe R. Developer <joe@example.com> <bugs@example.com>
11//! Jane Doe <jane@example.com> Jane <bugs@example.com>
12//! ";
13//!
14//! let parsed = gix_mailmap::parse(input)
15//! .collect::<Result<Vec<_>, _>>()
16//! .unwrap();
17//! assert_eq!(parsed.len(), 2);
18//!
19//! let snapshot = gix_mailmap::Snapshot::new(parsed);
20//! let resolved = snapshot.resolve(
21//! SignatureRef::from_bytes(b"Jane <bugs@example.com> 1711398853 +0800").unwrap(),
22//! );
23//!
24//! assert_eq!(resolved.name, "Jane Doe");
25//! assert_eq!(resolved.email, "jane@example.com");
26//! ```
27//! ## Feature Flags
28#![cfg_attr(
29 all(doc, feature = "document-features"),
30 doc = ::document_features::document_features!()
31)]
32#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg))]
33#![deny(missing_docs, rust_2018_idioms)]
34#![forbid(unsafe_code)]
35
36use bstr::BStr;
37
38///
39pub mod parse;
40
41/// Parse the given `buf` of bytes line by line into mapping [Entries][Entry].
42///
43/// Errors may occur per line, but it's up to the caller to stop iteration when
44/// one is encountered.
45pub fn parse(buf: &[u8]) -> parse::Lines<'_> {
46 parse::Lines::new(buf)
47}
48
49/// Similar to [parse()], but will skip all lines that didn't parse correctly, silently squelching all errors.
50pub fn parse_ignore_errors(buf: &[u8]) -> impl Iterator<Item = Entry<'_>> {
51 parse(buf).filter_map(Result::ok)
52}
53
54mod entry;
55
56///
57pub mod snapshot;
58
59/// A data-structure to efficiently store a list of entries for optimal, case-insensitive lookup by email and
60/// optionally name to find mappings to new names and/or emails.
61///
62/// The memory layout is efficient, even though lots of small allocations are performed to store strings of emails and names.
63///
64/// ### Handling of invalid `SignatureRef::time`
65///
66/// As the `time` field in [`SignatureRef`](gix_actor::SignatureRef) as passed by the caller maybe invalid,
67/// something that should be very rare but is possible, we decided to not expose this fallibility in the API.
68/// Hence, the user may separately check for the correctness of `time`, which we replace with [`gix_date::Time::default()`]
69/// in case of parse errors.
70#[derive(Default, Clone, Debug, Eq, PartialEq, Hash)]
71pub struct Snapshot {
72 /// Sorted by `old_email`
73 entries_by_old_email: Vec<snapshot::EmailEntry>,
74}
75
76/// An typical entry of a mailmap, which always contains an `old_email` by which
77/// the mapping is performed to replace the given `new_name` and `new_email`.
78///
79/// Optionally, `old_name` is also used for lookup.
80///
81/// Typically created by [parse()].
82#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy, Default)]
83#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
84pub struct Entry<'a> {
85 #[cfg_attr(feature = "serde", serde(borrow))]
86 /// The name to map to.
87 pub(crate) new_name: Option<&'a BStr>,
88 /// The email map to.
89 pub(crate) new_email: Option<&'a BStr>,
90 /// The name to look for and replace.
91 pub(crate) old_name: Option<&'a BStr>,
92 /// The email to look for and replace.
93 pub(crate) old_email: &'a BStr,
94}