gix_actor/signature/
mod.rs1mod _ref {
2 use bstr::ByteSlice;
3 use winnow::{error::StrContext, prelude::*};
4
5 use crate::{signature::decode, IdentityRef, Signature, SignatureRef};
6
7 impl<'a> SignatureRef<'a> {
9 pub fn from_bytes<E>(mut data: &'a [u8]) -> Result<SignatureRef<'a>, winnow::error::ErrMode<E>>
11 where
12 E: winnow::error::ParserError<&'a [u8]> + winnow::error::AddContext<&'a [u8], StrContext>,
13 {
14 decode.parse_next(&mut data)
15 }
16
17 pub fn to_owned(&self) -> Result<Signature, gix_date::parse::Error> {
19 Ok(Signature {
20 name: self.name.to_owned(),
21 email: self.email.to_owned(),
22 time: self.time()?,
23 })
24 }
25 }
26
27 impl<'a> SignatureRef<'a> {
29 pub fn trim(&self) -> SignatureRef<'a> {
31 SignatureRef {
32 name: self.name.trim().as_bstr(),
33 email: self.email.trim().as_bstr(),
34 time: self.time.trim(),
35 }
36 }
37
38 pub fn actor(&self) -> IdentityRef<'a> {
40 IdentityRef {
41 name: self.name,
42 email: self.email,
43 }
44 }
45
46 pub fn seconds(&self) -> gix_date::SecondsSinceUnixEpoch {
51 self.time
52 .trim()
53 .split(' ')
54 .next()
55 .and_then(|i| i.parse().ok())
56 .unwrap_or_default()
57 }
58
59 pub fn time(&self) -> Result<gix_date::Time, gix_date::parse::Error> {
62 self.time.parse()
63 }
64 }
65}
66
67mod convert {
68 use gix_date::parse::TimeBuf;
69
70 use crate::{Signature, SignatureRef};
71
72 impl Signature {
73 pub fn to_ref<'a>(&'a self, time_buf: &'a mut TimeBuf) -> SignatureRef<'a> {
77 SignatureRef {
78 name: self.name.as_ref(),
79 email: self.email.as_ref(),
80 time: self.time.to_str(time_buf),
81 }
82 }
83 }
84
85 impl From<SignatureRef<'_>> for Signature {
87 fn from(other: SignatureRef<'_>) -> Signature {
88 Signature {
89 name: other.name.to_owned(),
90 email: other.email.to_owned(),
91 time: other.time().unwrap_or_default(),
92 }
93 }
94 }
95}
96
97pub(crate) mod write {
98 use bstr::{BStr, ByteSlice};
99 use gix_date::parse::TimeBuf;
100
101 use crate::{Signature, SignatureRef};
102
103 #[derive(Debug, thiserror::Error)]
105 #[allow(missing_docs)]
106 pub(crate) enum Error {
107 #[error(r"Signature name or email must not contain '<', '>' or \n")]
108 IllegalCharacter,
109 }
110
111 impl From<Error> for std::io::Error {
112 fn from(err: Error) -> Self {
113 std::io::Error::other(err)
114 }
115 }
116
117 impl Signature {
119 pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
121 let mut buf = TimeBuf::default();
122 self.to_ref(&mut buf).write_to(out)
123 }
124 pub fn size(&self) -> usize {
126 self.name.len() + 2 + self.email.len() + 2 + self.time.size()
127 }
128 }
129
130 impl SignatureRef<'_> {
131 pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
133 out.write_all(validated_token(self.name)?)?;
134 out.write_all(b" ")?;
135 out.write_all(b"<")?;
136 out.write_all(validated_token(self.email)?)?;
137 out.write_all(b"> ")?;
138 out.write_all(validated_token(self.time.into())?)
139 }
140 pub fn size(&self) -> usize {
142 self.name.len() + 2 + self.email.len() + 2 + self.time.len()
143 }
144 }
145
146 pub(crate) fn validated_token(name: &BStr) -> Result<&BStr, Error> {
147 if name.find_byteset(b"<>\n").is_some() {
148 return Err(Error::IllegalCharacter);
149 }
150 Ok(name)
151 }
152}
153
154pub mod decode;
156pub use decode::function::decode;