@@ -5,6 +5,7 @@ mod _ref {
5
5
6
6
use crate :: { signature:: decode, IdentityRef , Signature , SignatureRef } ;
7
7
8
+ /// Lifecycle
8
9
impl < ' a > SignatureRef < ' a > {
9
10
/// Deserialize a signature from the given `data`.
10
11
pub fn from_bytes < E > ( mut data : & ' a [ u8 ] ) -> Result < SignatureRef < ' a > , winnow:: error:: ErrMode < E > >
@@ -15,15 +16,18 @@ mod _ref {
15
16
}
16
17
17
18
/// 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 {
20
21
name : self . name . to_owned ( ) ,
21
22
email : self . email . to_owned ( ) ,
22
- time : Time :: from_bytes ( self . time ) . expect ( "Time must be valid" ) ,
23
- }
23
+ time : self . time ( ) ? ,
24
+ } )
24
25
}
26
+ }
25
27
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.
27
31
pub fn trim ( & self ) -> SignatureRef < ' a > {
28
32
SignatureRef {
29
33
name : self . name . trim ( ) . as_bstr ( ) ,
@@ -32,22 +36,40 @@ mod _ref {
32
36
}
33
37
}
34
38
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.
36
40
pub fn actor ( & self ) -> IdentityRef < ' a > {
37
41
IdentityRef {
38
42
name : self . name ,
39
43
email : self . email ,
40
44
}
41
45
}
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
+ }
42
65
}
43
66
}
44
67
45
68
mod convert {
46
69
use crate :: { Signature , SignatureRef } ;
47
- use gix_date:: Time ;
48
70
49
71
impl Signature {
50
- /// Borrow this instance as immutable
72
+ /// Borrow this instance as immutable, serializing the `time` field into `buf`.
51
73
pub fn to_ref < ' a > ( & ' a self , buf : & ' a mut Vec < u8 > ) -> SignatureRef < ' a > {
52
74
SignatureRef {
53
75
name : self . name . as_ref ( ) ,
@@ -57,14 +79,11 @@ mod convert {
57
79
}
58
80
}
59
81
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 ( )
68
87
}
69
88
}
70
89
}
0 commit comments