gix_actor/lib.rs
1//! This crate provides ways of identifying an actor within the git repository both in shared and mutable variants.
2//!
3//! ## Examples
4//!
5//! ```
6//! use gix_actor::{IdentityRef, SignatureRef};
7//!
8//! let actor = IdentityRef::from_bytes::<()>(b" Taylor Example < taylor@example.com >")
9//! .unwrap()
10//! .trim();
11//! assert_eq!(actor.name, "Taylor Example");
12//! assert_eq!(actor.email, "taylor@example.com");
13//!
14//! let signature = SignatureRef::from_bytes::<()>(b"Taylor Example <taylor@example.com> 1711398853 +0800")
15//! .unwrap()
16//! .trim();
17//! assert_eq!(signature.actor(), actor);
18//!
19//! let time = signature.time().unwrap();
20//! assert_eq!(time.seconds, 1_711_398_853);
21//! assert_eq!(time.offset, 8 * 60 * 60);
22//!
23//! let owned = signature.to_owned().unwrap();
24//! let mut out = Vec::new();
25//! owned.write_to(&mut out).unwrap();
26//! assert_eq!(out, b"Taylor Example <taylor@example.com> 1711398853 +0800");
27//! ```
28//!
29//! ## Feature Flags
30#![cfg_attr(
31 all(doc, feature = "document-features"),
32 doc = ::document_features::document_features!()
33)]
34#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg))]
35#![deny(missing_docs, rust_2018_idioms, unsafe_code)]
36
37/// The re-exported `bstr` crate.
38///
39/// For convenience to allow using `bstr` without adding it to own cargo manifest.
40pub use bstr;
41use bstr::{BStr, BString};
42/// The re-exported `gix-date` crate.
43///
44/// For convenience to allow using `gix-date` without adding it to own cargo manifest.
45pub use gix_date as date;
46
47mod identity;
48///
49pub mod signature;
50
51/// A person with name and email.
52#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
53#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
54pub struct Identity {
55 /// The actors name, potentially with whitespace as parsed.
56 ///
57 /// Use [IdentityRef::trim()] or trim manually to be able to clean it up.
58 pub name: BString,
59 /// The actor's email, potentially with whitespace and garbage as parsed.
60 ///
61 /// Use [IdentityRef::trim()] or trim manually to be able to clean it up.
62 pub email: BString,
63}
64
65/// A person with name and email, as reference.
66#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
67#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
68pub struct IdentityRef<'a> {
69 /// The actors name, potentially with whitespace as parsed.
70 ///
71 /// Use [IdentityRef::trim()] or trim manually to be able to clean it up.
72 #[cfg_attr(feature = "serde", serde(borrow))]
73 pub name: &'a BStr,
74 /// The actor's email, potentially with whitespace and garbage as parsed.
75 ///
76 /// Use [IdentityRef::trim()] or trim manually to be able to clean it up.
77 pub email: &'a BStr,
78}
79
80/// A mutable signature that is created by an actor at a certain time.
81///
82/// Note that this is not a cryptographical signature.
83#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
84#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
85pub struct Signature {
86 /// The actors name, potentially with whitespace as parsed.
87 ///
88 /// Use [SignatureRef::trim()] or trim manually to be able to clean it up.
89 pub name: BString,
90 /// The actor's email, potentially with whitespace and garbage as parsed.
91 ///
92 /// Use [SignatureRef::trim()] or trim manually to be able to clean it up.
93 pub email: BString,
94 /// The time stamp at which the signature is performed.
95 pub time: date::Time,
96}
97
98/// An immutable signature that is created by an actor at a certain time.
99///
100/// All of its fields are references to the backing buffer to allow lossless
101/// round-tripping, as decoding the `time` field could be a lossy transformation.
102///
103/// Note that this is not a cryptographical signature.
104#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
105#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
106pub struct SignatureRef<'a> {
107 /// The actors name, potentially with whitespace as parsed.
108 ///
109 /// Use [SignatureRef::trim()] or trim manually for cleanup.
110 #[cfg_attr(feature = "serde", serde(borrow))]
111 pub name: &'a BStr,
112 /// The actor's email, potentially with whitespace and garbage as parsed.
113 ///
114 /// Use [SignatureRef::trim()] or trim manually for cleanup.
115 pub email: &'a BStr,
116 /// The timestamp at which the signature was performed,
117 /// potentially malformed due to lenient parsing.
118 ///
119 /// Use [`SignatureRef::time()`] to decode.
120 pub time: &'a str,
121}