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::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::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 impl Signature {
105 pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
107 let mut buf = TimeBuf::default();
108 self.to_ref(&mut buf).write_to(out)
109 }
110 pub fn size(&self) -> usize {
112 self.name.len() + 2 + self.email.len() + 2 + self.time.size()
113 }
114 }
115
116 impl SignatureRef<'_> {
117 pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
119 out.write_all(validated_token(self.name).map_err(std::io::Error::other)?)?;
120 out.write_all(b" ")?;
121 out.write_all(b"<")?;
122 out.write_all(validated_token(self.email).map_err(std::io::Error::other)?)?;
123 out.write_all(b"> ")?;
124 out.write_all(validated_token(self.time.into()).map_err(std::io::Error::other)?)
125 }
126 pub fn size(&self) -> usize {
128 self.name.len() + 2 + self.email.len() + 2 + self.time.len()
129 }
130 }
131
132 pub(crate) fn validated_token(name: &BStr) -> Result<&BStr, gix_error::ValidationError> {
133 if name.find_byteset(b"<>\n").is_some() {
134 return Err(gix_error::ValidationError::new_with_input(
135 "Signature name or email must not contain '<', '>' or \\n",
136 name,
137 ));
138 }
139 Ok(name)
140 }
141}
142
143pub mod decode;
145pub use decode::function::decode;