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