Skip to content

Commit fe054c4

Browse files
committed
feat(picky): add HttpSignature::to_signing_string() method
Equivalent to HttpSignature::to_string().
1 parent 4eda6bf commit fe054c4

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

picky/src/http/http_signature.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ impl Header {
117117
}
118118
}
119119

120-
impl ToString for Header {
121-
fn to_string(&self) -> String {
122-
self.as_str().to_owned()
120+
impl fmt::Display for Header {
121+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122+
write!(f, "{}", self.as_str())
123123
}
124124
}
125125

@@ -230,18 +230,8 @@ impl HttpSignature {
230230
inner: Default::default(),
231231
}
232232
}
233-
}
234233

235-
const HTTP_SIGNATURE_HEADER: &str = "Signature";
236-
const HTTP_SIGNATURE_KEY_ID: &str = "keyId";
237-
const HTTP_SIGNATURE_SIGNATURE: &str = "signature";
238-
const HTTP_SIGNATURE_CREATED: &str = "created";
239-
const HTTP_SIGNATURE_EXPIRES: &str = "expires";
240-
const HTTP_SIGNATURE_HEADERS: &str = "headers";
241-
const HTTP_SIGNATURE_ALGORITHM: &str = "algorithm";
242-
243-
impl ToString for HttpSignature {
244-
fn to_string(&self) -> String {
234+
pub fn to_signing_string(&self) -> String {
245235
let mut acc = Vec::with_capacity(5);
246236

247237
if self.legacy {
@@ -308,6 +298,20 @@ impl ToString for HttpSignature {
308298
}
309299
}
310300

301+
const HTTP_SIGNATURE_HEADER: &str = "Signature";
302+
const HTTP_SIGNATURE_KEY_ID: &str = "keyId";
303+
const HTTP_SIGNATURE_SIGNATURE: &str = "signature";
304+
const HTTP_SIGNATURE_CREATED: &str = "created";
305+
const HTTP_SIGNATURE_EXPIRES: &str = "expires";
306+
const HTTP_SIGNATURE_HEADERS: &str = "headers";
307+
const HTTP_SIGNATURE_ALGORITHM: &str = "algorithm";
308+
309+
impl fmt::Display for HttpSignature {
310+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
311+
write!(f, "{}", self.to_signing_string())
312+
}
313+
}
314+
311315
impl FromStr for HttpSignature {
312316
type Err = HttpSignatureError;
313317

@@ -965,7 +969,7 @@ mod tests {
965969
.generate_signing_string_using_http_request(&parts)
966970
.build()
967971
.expect("couldn't generate http signature");
968-
let http_signature_str = http_signature.to_string();
972+
let http_signature_str = http_signature.to_signing_string();
969973

970974
pretty_assertions::assert_eq!(http_signature_str, HTTP_SIGNATURE_EXAMPLE);
971975

@@ -984,7 +988,7 @@ mod tests {
984988
.generate_signing_string_using_http_request(&parts_2)
985989
.build()
986990
.expect("couldn't generate http signature 2");
987-
let http_signature_str_2 = http_signature_2.to_string();
991+
let http_signature_str_2 = http_signature_2.to_signing_string();
988992

989993
pretty_assertions::assert_eq!(http_signature_str_2, http_signature_str);
990994
}
@@ -1146,7 +1150,7 @@ mod tests {
11461150
.pre_generated_signing_string(signing_string)
11471151
.build()
11481152
.expect("couldn't generate http signature using pre-generated signing string");
1149-
let http_signature_str = http_signature.to_string();
1153+
let http_signature_str = http_signature.to_signing_string();
11501154
assert_eq!(http_signature_str, HTTP_SIGNATURE_EXAMPLE);
11511155
}
11521156

@@ -1238,7 +1242,7 @@ mod tests {
12381242
.build()
12391243
.expect("build http signature");
12401244

1241-
pretty_assertions::assert_eq!(http_signature.to_string(), HTTP_SIGNATURE_LEGACY);
1245+
pretty_assertions::assert_eq!(http_signature.to_signing_string(), HTTP_SIGNATURE_LEGACY);
12421246
}
12431247

12441248
{

0 commit comments

Comments
 (0)