1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
mod _ref {
use crate::{signature::decode, Signature, SignatureRef};
use bstr::ByteSlice;
impl<'a> SignatureRef<'a> {
pub fn from_bytes<E>(data: &'a [u8]) -> Result<SignatureRef<'a>, nom::Err<E>>
where
E: nom::error::ParseError<&'a [u8]> + nom::error::ContextError<&'a [u8]>,
{
decode(data).map(|(_, t)| t)
}
pub fn to_owned(&self) -> Signature {
Signature {
name: self.name.to_owned(),
email: self.email.to_owned(),
time: self.time,
}
}
pub fn trim(&self) -> SignatureRef<'a> {
SignatureRef {
name: self.name.trim().as_bstr(),
email: self.email.trim().as_bstr(),
time: self.time,
}
}
}
}
mod convert {
use crate::{Signature, SignatureRef};
impl Signature {
pub fn empty() -> Self {
Signature::default()
}
pub fn to_ref(&self) -> SignatureRef<'_> {
SignatureRef {
name: self.name.as_ref(),
email: self.email.as_ref(),
time: self.time,
}
}
}
impl From<SignatureRef<'_>> for Signature {
fn from(other: SignatureRef<'_>) -> Signature {
let SignatureRef { name, email, time } = other;
Signature {
name: name.to_owned(),
email: email.to_owned(),
time,
}
}
}
}
mod write {
use std::io;
use bstr::{BStr, ByteSlice};
use quick_error::quick_error;
use crate::{Signature, SignatureRef, SPACE};
quick_error! {
#[derive(Debug)]
#[allow(missing_docs)]
enum Error {
IllegalCharacter {
display("Signature name or email must not contain '<', '>' or \\n")
}
}
}
impl From<Error> for io::Error {
fn from(err: Error) -> Self {
io::Error::new(io::ErrorKind::Other, err)
}
}
impl Signature {
pub fn write_to(&self, out: impl io::Write) -> io::Result<()> {
self.to_ref().write_to(out)
}
pub fn size(&self) -> usize {
self.to_ref().size()
}
}
impl<'a> SignatureRef<'a> {
pub fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
out.write_all(validated_token(self.name)?)?;
out.write_all(SPACE)?;
out.write_all(b"<")?;
out.write_all(validated_token(self.email)?)?;
out.write_all(b"> ")?;
self.time.write_to(out)
}
pub fn size(&self) -> usize {
self.name.len() + 2 + self.email.len() + 2 + self.time.size()
}
}
fn validated_token(name: &BStr) -> Result<&BStr, Error> {
if name.find_byteset(b"<>\n").is_some() {
return Err(Error::IllegalCharacter);
}
Ok(name)
}
}
mod init {
use bstr::BString;
use crate::{Signature, Time};
impl Signature {
#[cfg(feature = "local-time-support")]
pub fn now_local(
name: impl Into<BString>,
email: impl Into<BString>,
) -> Result<Self, git_features::time::tz::Error> {
let offset = git_features::time::tz::current_utc_offset()?;
Ok(Signature {
name: name.into(),
email: email.into(),
time: Time {
seconds_since_unix_epoch: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("the system time doesn't run backwards that much")
.as_secs() as u32,
offset_in_seconds: offset,
sign: offset.into(),
},
})
}
#[cfg(feature = "local-time-support")]
pub fn now_local_or_utc(name: impl Into<BString>, email: impl Into<BString>) -> Self {
let offset = git_features::time::tz::current_utc_offset().unwrap_or(0);
Signature {
name: name.into(),
email: email.into(),
time: Time {
seconds_since_unix_epoch: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("the system time doesn't run backwards that much")
.as_secs() as u32,
offset_in_seconds: offset,
sign: offset.into(),
},
}
}
pub fn now_utc(name: impl Into<BString>, email: impl Into<BString>) -> Self {
let utc_offset = 0;
Signature {
name: name.into(),
email: email.into(),
time: Time {
seconds_since_unix_epoch: seconds_since_unix_epoch(),
offset_in_seconds: utc_offset,
sign: utc_offset.into(),
},
}
}
}
fn seconds_since_unix_epoch() -> u32 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("the system time doesn't run backwards that much")
.as_secs() as u32
}
}
mod decode;
pub use decode::decode;