@@ -5,6 +5,7 @@ mod _ref {
55
66 use crate :: { signature:: decode, IdentityRef , Signature , SignatureRef } ;
77
8+ /// Lifecycle
89 impl < ' a > SignatureRef < ' a > {
910 /// Deserialize a signature from the given `data`.
1011 pub fn from_bytes < E > ( mut data : & ' a [ u8 ] ) -> Result < SignatureRef < ' a > , winnow:: error:: ErrMode < E > >
@@ -15,15 +16,18 @@ mod _ref {
1516 }
1617
1718 /// Create an owned instance from this shared one.
18- pub fn to_owned ( & self ) -> Signature {
19- Signature {
19+ pub fn to_owned ( & self ) -> Result < Signature , gix_date :: parse :: Error > {
20+ Ok ( Signature {
2021 name : self . name . to_owned ( ) ,
2122 email : self . email . to_owned ( ) ,
22- time : Time :: from_bytes ( self . time ) . expect ( "Time must be valid" ) ,
23- }
23+ time : self . time ( ) ? ,
24+ } )
2425 }
26+ }
2527
26- /// Trim whitespace surrounding the name and email and return a new signature.
28+ /// Access
29+ impl < ' a > SignatureRef < ' a > {
30+ /// Trim the whitespace surrounding the `name`, `email` and `time` and return a new signature.
2731 pub fn trim ( & self ) -> SignatureRef < ' a > {
2832 SignatureRef {
2933 name : self . name . trim ( ) . as_bstr ( ) ,
@@ -32,22 +36,40 @@ mod _ref {
3236 }
3337 }
3438
35- /// Return the actor's name and email, effectively excluding the time stamp of this signature.
39+ /// Return the actor's name and email, effectively excluding the timestamp of this signature.
3640 pub fn actor ( & self ) -> IdentityRef < ' a > {
3741 IdentityRef {
3842 name : self . name ,
3943 email : self . email ,
4044 }
4145 }
46+
47+ /// Parse only the seconds since unix epoch from the `time` field, or silently default to 0
48+ /// if parsing fails. Note that this ignores the timezone, so it can parse otherwise broken dates.
49+ ///
50+ /// For a fallible and more complete, but slower version, use [`time()`](Self::time).
51+ pub fn seconds ( & self ) -> gix_date:: SecondsSinceUnixEpoch {
52+ use winnow:: stream:: AsChar ;
53+ self . time
54+ . trim ( )
55+ . split ( |b| b. is_space ( ) )
56+ . next ( )
57+ . and_then ( |i| i. to_str ( ) . ok ( ) ?. parse ( ) . ok ( ) )
58+ . unwrap_or_default ( )
59+ }
60+
61+ /// Parse the `time` field for access to the passed time since unix epoch, and the time offset.
62+ pub fn time ( & self ) -> Result < gix_date:: Time , gix_date:: parse:: Error > {
63+ Time :: from_bytes ( self . time )
64+ }
4265 }
4366}
4467
4568mod convert {
4669 use crate :: { Signature , SignatureRef } ;
47- use gix_date:: Time ;
4870
4971 impl Signature {
50- /// Borrow this instance as immutable
72+ /// Borrow this instance as immutable, serializing the `time` field into `buf`.
5173 pub fn to_ref < ' a > ( & ' a self , buf : & ' a mut Vec < u8 > ) -> SignatureRef < ' a > {
5274 SignatureRef {
5375 name : self . name . as_ref ( ) ,
@@ -57,14 +79,11 @@ mod convert {
5779 }
5880 }
5981
60- impl From < SignatureRef < ' _ > > for Signature {
61- fn from ( other : SignatureRef < ' _ > ) -> Signature {
62- let SignatureRef { name, email, time } = other;
63- Signature {
64- name : name. to_owned ( ) ,
65- email : email. to_owned ( ) ,
66- time : Time :: from_bytes ( time) . expect ( "Time must be valid" ) ,
67- }
82+ impl TryFrom < SignatureRef < ' _ > > for Signature {
83+ type Error = gix_date:: parse:: Error ;
84+
85+ fn try_from ( other : SignatureRef < ' _ > ) -> Result < Signature , Self :: Error > {
86+ other. to_owned ( )
6887 }
6988 }
7089}
0 commit comments