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#[derive(Default, Clone, Debug, Eq, PartialEq)]
40pub struct Snapshot {
41    /// Sorted by `old_email`
42    entries_by_old_email: Vec<snapshot::EmailEntry>,
43}
44
45/// An typical entry of a mailmap, which always contains an `old_email` by which
46/// the mapping is performed to replace the given `new_name` and `new_email`.
47///
48/// Optionally, `old_name` is also used for lookup.
49///
50/// Typically created by [parse()].
51#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy, Default)]
52#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
53pub struct Entry<'a> {
54    #[cfg_attr(feature = "serde", serde(borrow))]
55    /// The name to map to.
56    pub(crate) new_name: Option<&'a BStr>,
57    /// The email map to.
58    pub(crate) new_email: Option<&'a BStr>,
59    /// The name to look for and replace.
60    pub(crate) old_name: Option<&'a BStr>,
61    /// The email to look for and replace.
62    pub(crate) old_email: &'a BStr,
63}