summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWiktor Kwapisiewicz <wiktor@metacode.biz>2020-04-01 16:19:42 +0200
committerWiktor Kwapisiewicz <wiktor@metacode.biz>2020-04-03 14:22:57 +0200
commitb0dbca2064163a41f73559d787456e8bab7b5cb9 (patch)
treeee3c75f5fdf920e99c3bf4b1e12f7056b4dd9e14
parent86bda990a425c93950714e7e36e14f4308a593f0 (diff)
openpgp: Convert `CTB::from_ptag` to `TryFrom<u8>`
-rw-r--r--openpgp/src/packet/header/ctb.rs28
-rw-r--r--openpgp/src/parse.rs3
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) =>