diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-12-03 13:31:25 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-12-03 13:31:25 +0100 |
commit | 88442e46a8f5639c4493713baeb5b90f7cf6a84b (patch) | |
tree | 6ef574940c13e9d7245c64d2dfea1ede2d91770e /openpgp | |
parent | 3731a890de264f1f7def2af50b4e6abde536fb76 (diff) |
openpgp: Make Literal::set_filename polymorphic over AsRef<[u8]>.
- And drop Literal::set_filename_from_bytes.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/packet/literal.rs | 25 | ||||
-rw-r--r-- | openpgp/src/parse/parse.rs | 2 | ||||
-rw-r--r-- | openpgp/src/serialize/stream.rs | 2 |
3 files changed, 9 insertions, 20 deletions
diff --git a/openpgp/src/packet/literal.rs b/openpgp/src/packet/literal.rs index b384e62f..fa97d3e3 100644 --- a/openpgp/src/packet/literal.rs +++ b/openpgp/src/packet/literal.rs @@ -109,7 +109,7 @@ impl Literal { self.filename.as_ref().map(|b| b.as_slice()) } - /// Sets the literal packet's filename field from a byte sequence. + /// Sets the literal packet's filename field. /// /// The standard does not specify the encoding. Filenames must /// not be longer than 255 bytes. @@ -117,8 +117,11 @@ impl Literal { /// Note: when a literal data packet is protected by a signature, /// only the literal data packet's body is protected, not the /// meta-data. As such, this field should not be used. - pub fn set_filename_from_bytes(&mut self, filename: &[u8]) - -> Result<Option<Vec<u8>>> { + pub fn set_filename<F>(&mut self, filename: F) + -> Result<Option<Vec<u8>>> + where F: AsRef<[u8]> + { + let filename = filename.as_ref(); Ok(::std::mem::replace(&mut self.filename, match filename.len() { 0 => None, 1..=255 => Some(filename.to_vec()), @@ -128,20 +131,6 @@ impl Literal { })) } - /// Sets the literal packet's filename field from a UTF-8 encoded - /// string. - /// - /// This is a convenience function, since the field is actually a - /// raw byte string. Filenames must not be longer than 255 bytes. - /// - /// Note: when a literal data packet is protected by a signature, - /// only the literal data packet's body is protected, not the - /// meta-data. As such, this field should not be used. - pub fn set_filename(&mut self, filename: &str) - -> Result<Option<Vec<u8>>> { - self.set_filename_from_bytes(filename.as_bytes()) - } - /// Gets the literal packet's date field. /// /// Note: when a literal data packet is protected by a signature, @@ -184,7 +173,7 @@ impl Arbitrary for Literal { fn arbitrary<G: Gen>(g: &mut G) -> Self { let mut l = Literal::new(DataFormat::arbitrary(g)); l.set_body(Vec::<u8>::arbitrary(g)); - while let Err(_) = l.set_filename_from_bytes(&Vec::<u8>::arbitrary(g)) { + while let Err(_) = l.set_filename(&Vec::<u8>::arbitrary(g)) { // Too long, try again. } l.set_date(Some(Timestamp::arbitrary(g).into())).unwrap(); diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs index 4776ba0a..a06a710c 100644 --- a/openpgp/src/parse/parse.rs +++ b/openpgp/src/parse/parse.rs @@ -1715,7 +1715,7 @@ impl Literal { let mut literal = Literal::new(format.into()); if let Some(filename) = filename { - literal.set_filename_from_bytes(&filename) + literal.set_filename(&filename) .expect("length checked above"); } literal.set_date( diff --git a/openpgp/src/serialize/stream.rs b/openpgp/src/serialize/stream.rs index 2b53cb7a..40b18984 100644 --- a/openpgp/src/serialize/stream.rs +++ b/openpgp/src/serialize/stream.rs @@ -578,7 +578,7 @@ impl<'a> LiteralWriter<'a> { /// The standard does not specify the encoding. Filenames must /// not be longer than 255 bytes. pub fn filename<B: AsRef<[u8]>>(mut self, filename: B) -> Result<Self> { - self.template.set_filename_from_bytes(filename.as_ref())?; + self.template.set_filename(filename.as_ref())?; Ok(self) } |