Skip to main content

Signature

Struct Signature 

Source
pub struct Signature {
    pub name: String,
    pub email: String,
    pub time: OffsetDateTime,
}
Expand description

Parsed author or committer identity from a full header or reflog identity line.

Fields§

§name: String

Display name (may be empty).

§email: String

Email address inside <…>.

§time: OffsetDateTime

Timestamp with the recorded ±HHMM offset preserved.

Implementations§

Source§

impl Signature

Source

pub fn new( name: impl Into<String>, email: impl Into<String>, time: OffsetDateTime, ) -> Result<Self, SignatureError>

Build a signature for writing into a commit or tag.

§Parameters
  • name: display name (may be empty).
  • email: address written inside <…> (may be empty).
  • time: timestamp; its UTC offset becomes the ±HHMM field.
§Errors

See Self::validate.

§Examples
use grit_lib::ident::Signature;
use time::{OffsetDateTime, UtcOffset};
let time = OffsetDateTime::from_unix_timestamp(1_700_000_000)?
    .to_offset(UtcOffset::from_hms(-8, 0, 0)?);
let sig = Signature::new("Ada Lovelace", "ada@example.com", time)?;
assert_eq!(sig.header_line(), "Ada Lovelace <ada@example.com> 1700000000 -0800");
assert!(Signature::new("Ada <x>", "ada@example.com", time).is_err());
Source

pub fn cleaned( name: &str, email: &str, time: OffsetDateTime, ) -> Result<Self, SignatureError>

Build a signature from a name and email that may hold characters an identity line can’t, cleaning them as Git cleans GIT_AUTHOR_NAME and friends: <, > and control characters other than tab are dropped, and punctuation and whitespace are trimmed from both ends. Use it for identities from untrusted input, such as a request to a server, so nothing in them can end the line early or add a header.

§Parameters
  • name: display name; empty after cleaning is allowed.
  • email: address written inside <…>; empty after cleaning is allowed.
  • time: timestamp; its UTC offset becomes the ±HHMM field.
§Returns

A signature whose Self::header_line git fsck accepts.

§Errors

SignatureError::BeforeEpoch and SignatureError::SubMinuteOffset for a time Self::validate refuses; the name and email always pass.

§Examples
use grit_lib::ident::Signature;
use time::OffsetDateTime;
let time = OffsetDateTime::from_unix_timestamp(1_700_000_000)?;
let sig = Signature::cleaned("Eve\nencoding bogus", " <e>v<e@x> ", time)?;
assert_eq!(sig.header_line(), "Eveencoding bogus <eve@x> 1700000000 +0000");
Source

pub fn validate(&self) -> Result<(), SignatureError>

Check that Self::header_line yields an identity line git fsck accepts.

§Errors
Source

pub fn reflog_name_email(&self) -> String

Name <email> for a reflog identity column; the timestamp is taken from crate::refs::store::ReflogUpdate::time when the entry is formatted.

Source

pub fn header_line(&self) -> String

Render as a header payload: Name <email> <unix-seconds> <±HHMM>.

The result is what follows author / committer / tagger in an object. It is only fsck-clean when Self::validate succeeds.

Source

pub fn format_rfc3339(&self) -> Option<String>

Format Self::time as RFC 3339 (includes the original offset).

Source

pub fn from_header_lossy(line: &str) -> Self

Parse an identity line as stored in a commit or tag, accepting what git log accepts.

Unlike str::parse, this never fails: a missing <email> leaves Self::email empty with the whole text as the name, and a missing, non-numeric or overflowing date becomes the Unix epoch in UTC, the same sentinel Git shows for such commits.

§Parameters
  • line: the text after author / committer / tagger in an object header.
§Examples
use grit_lib::ident::Signature;

let sig = Signature::from_header_lossy("Ada <ada@example.com> 1700000000 +0100");
assert_eq!(sig.email, "ada@example.com");
assert_eq!(sig.time.unix_timestamp(), 1_700_000_000);

let bogus = Signature::from_header_lossy("Ada <ada@example.com> totally_bogus");
assert_eq!(bogus.time.unix_timestamp(), 0);

Trait Implementations§

Source§

impl Clone for Signature

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Signature

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Signature

Source§

impl FromStr for Signature

Source§

fn from_str(line: &str) -> Result<Self, Self::Err>

Parse a full header identity line (Name <email> <unix-seconds> <±HHMM>).

§Errors

SignatureError::Malformed when the line does not have exactly one <email>, has a missing, non-numeric, or overflowing date, or has text after the ±HHMM timezone; otherwise the Signature::validate errors.

Source§

type Err = SignatureError

The associated error which can be returned from parsing.
Source§

impl PartialEq for Signature

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Signature

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.