From bc4e1564ea816a66d1191fc310ac6d2b02d1a92f Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Mon, 20 Apr 2020 17:15:08 +0200 Subject: openpgp: Add armor::Writer::with_headers. - Add a new constructor that takes headers. This allows us to make the header argument polymorphic. --- openpgp/src/armor.rs | 52 +++++++++++++++++++++++++++++------ openpgp/src/serialize/cert_armored.rs | 3 +- 2 files changed, 45 insertions(+), 10 deletions(-) (limited to 'openpgp/src') diff --git a/openpgp/src/armor.rs b/openpgp/src/armor.rs index 34b068bc..ef528cba 100644 --- a/openpgp/src/armor.rs +++ b/openpgp/src/armor.rs @@ -160,8 +160,36 @@ impl Writer { /// use openpgp::armor::{Writer, Kind}; /// # fn main() { f().unwrap(); } /// # fn f() -> std::io::Result<()> { - /// let mut writer = Writer::new(Vec::new(), Kind::File, - /// &[ ("Key", "Value") ][..])?; + /// let mut writer = Writer::new(Vec::new(), Kind::File)?; + /// writer.write_all(b"Hello world!")?; + /// let buffer = writer.finalize()?; + /// assert_eq!( + /// String::from_utf8_lossy(&buffer), + /// "-----BEGIN PGP ARMORED FILE----- + /// + /// SGVsbG8gd29ybGQh + /// =s4Gu + /// -----END PGP ARMORED FILE----- + /// "); + /// # Ok(()) + /// # } + /// ``` + pub fn new(inner: W, kind: Kind) -> Result { + Self::with_headers(inner, kind, Option::<(&str, &str)>::None) + } + + /// Constructs a new filter for the given type of data. + /// + /// # Example + /// + /// ``` + /// use std::io::{Read, Write, Cursor}; + /// use sequoia_openpgp as openpgp; + /// use openpgp::armor::{Writer, Kind}; + /// # fn main() { f().unwrap(); } + /// # fn f() -> std::io::Result<()> { + /// let mut writer = Writer::with_headers(Vec::new(), Kind::File, + /// vec![ ("Key", "Value") ])?; /// writer.write_all(b"Hello world!")?; /// let buffer = writer.finalize()?; /// assert_eq!( @@ -176,7 +204,12 @@ impl Writer { /// # Ok(()) /// # } /// ``` - pub fn new(inner: W, kind: Kind, headers: &[(&str, &str)]) -> Result { + pub fn with_headers(inner: W, kind: Kind, headers: I) + -> Result + where I: IntoIterator, + K: AsRef, + V: AsRef, + { let mut w = Writer { sink: inner, kind, @@ -192,7 +225,8 @@ impl Writer { write!(&mut cur, "{}{}", kind.begin(), LINE_ENDING)?; for h in headers { - write!(&mut cur, "{}: {}{}", h.0, h.1, LINE_ENDING)?; + write!(&mut cur, "{}: {}{}", h.0.as_ref(), h.1.as_ref(), + LINE_ENDING)?; } // A blank line separates the headers from the body. @@ -1294,7 +1328,7 @@ mod test { fn enarmor() { for (bin, asc) in TEST_BIN.iter().zip(TEST_ASC.iter()) { let mut w = - Writer::new(Vec::new(), Kind::File, &[]).unwrap(); + Writer::new(Vec::new(), Kind::File).unwrap(); w.write(&[]).unwrap(); // Avoid zero-length optimization. w.write_all(bin).unwrap(); let buf = w.finalize().unwrap(); @@ -1306,7 +1340,7 @@ mod test { #[test] fn enarmor_bytewise() { for (bin, asc) in TEST_BIN.iter().zip(TEST_ASC.iter()) { - let mut w = Writer::new(Vec::new(), Kind::File, &[]).unwrap(); + let mut w = Writer::new(Vec::new(), Kind::File).unwrap(); w.write(&[]).unwrap(); // Avoid zero-length optimization. for b in bin.iter() { w.write(&[*b]).unwrap(); @@ -1321,12 +1355,12 @@ mod test { fn drop_writer() { // No ASCII frame shall be emitted if the writer is dropped // unused. - assert!(Writer::new(Vec::new(), Kind::File, &[]).unwrap() + assert!(Writer::new(Vec::new(), Kind::File).unwrap() .finalize().unwrap().is_empty()); // However, if the user insists, we will encode a zero-byte // string. - let mut w = Writer::new(Vec::new(), Kind::File, &[]).unwrap(); + let mut w = Writer::new(Vec::new(), Kind::File).unwrap(); w.write(&[]).unwrap(); let buf = w.finalize().unwrap(); assert_eq!( @@ -1567,7 +1601,7 @@ mod test { return true; } - let mut w = Writer::new(Vec::new(), kind, &[]).unwrap(); + let mut w = Writer::new(Vec::new(), kind).unwrap(); w.write_all(&payload).unwrap(); let encoded = w.finalize().unwrap(); diff --git a/openpgp/src/serialize/cert_armored.rs b/openpgp/src/serialize/cert_armored.rs index bc52c657..6ff69846 100644 --- a/openpgp/src/serialize/cert_armored.rs +++ b/openpgp/src/serialize/cert_armored.rs @@ -116,7 +116,8 @@ impl<'a> Encoder<'a> { .map(|value| ("Comment", value.as_str())) .collect(); - let mut w = armor::Writer::new(o, armor::Kind::PublicKey, &headers)?; + let mut w = + armor::Writer::with_headers(o, armor::Kind::PublicKey, headers)?; if export { self.cert.export(&mut w)?; } else { -- cgit v1.2.3