diff options
-rw-r--r-- | openpgp/src/packet/header/ctb.rs | 28 | ||||
-rw-r--r-- | openpgp/src/parse.rs | 3 |
2 files changed, 18 insertions, 13 deletions
diff --git a/openpgp/src/packet/header/ctb.rs b/openpgp/src/packet/header/ctb.rs index 5c90ee8c..b6cbd457 100644 --- a/openpgp/src/packet/header/ctb.rs +++ b/openpgp/src/packet/header/ctb.rs @@ -188,11 +188,23 @@ impl CTB { CTB::New(CTBNew::new(tag)) } + /// Returns the packet's tag. + pub fn tag(&self) -> Tag { + match self { + CTB::New(c) => c.tag(), + CTB::Old(c) => c.tag(), + } + } +} + +impl TryFrom<u8> for CTB { + type Error = anyhow::Error; + /// Parses a CTB as described in [Section 4.2 of RFC 4880]. This /// function parses both new and old format CTBs. /// /// [Section 4.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-4.2 - pub fn from_ptag(ptag: u8) -> Result<CTB> { + fn try_from(ptag: u8) -> Result<CTB> { // The top bit of the ptag must be set. if ptag & 0b1000_0000 == 0 { return Err( @@ -228,20 +240,12 @@ impl CTB { Ok(ctb) } - - /// Returns the packet's tag. - pub fn tag(&self) -> Tag { - match self { - CTB::New(c) => c.tag(), - CTB::Old(c) => c.tag(), - } - } } #[test] fn ctb() { // 0x99 = public key packet - if let CTB::Old(ctb) = CTB::from_ptag(0x99).unwrap() { + if let CTB::Old(ctb) = CTB::try_from(0x99).unwrap() { assert_eq!(ctb.tag(), Tag::PublicKey); assert_eq!(ctb.length_type, PacketLengthType::TwoOctets); } else { @@ -249,7 +253,7 @@ fn ctb() { } // 0xa3 = old compressed packet - if let CTB::Old(ctb) = CTB::from_ptag(0xa3).unwrap() { + if let CTB::Old(ctb) = CTB::try_from(0xa3).unwrap() { assert_eq!(ctb.tag(), Tag::CompressedData); assert_eq!(ctb.length_type, PacketLengthType::Indeterminate); } else { @@ -257,7 +261,7 @@ fn ctb() { } // 0xcb: new literal - if let CTB::New(ctb) = CTB::from_ptag(0xcb).unwrap() { + if let CTB::New(ctb) = CTB::try_from(0xcb).unwrap() { assert_eq!(ctb.tag(), Tag::Literal); } else { panic!("Expected a new format packet."); diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index 8b6f5ee7..609ca82e 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -92,6 +92,7 @@ use std; use std::io; use std::io::prelude::*; +use std::convert::TryFrom; use std::cmp; use std::str; use std::mem; @@ -912,7 +913,7 @@ impl Header { pub(crate) fn parse<R: BufferedReader<C>, C> (bio: &mut R) -> Result<Header> { - let ctb = CTB::from_ptag(bio.data_consume_hard(1)?[0])?; + let ctb = CTB::try_from(bio.data_consume_hard(1)?[0])?; let length = match ctb { CTB::New(_) => BodyLength::parse_new_format(bio)?, CTB::Old(ref ctb) => |