From 08fc6757629ce38913cf594f8177dfb4496224d4 Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Wed, 1 Apr 2020 16:03:45 +0200 Subject: openpgp: Convert `Message::from_packet_pile` to `TryFrom` --- guide/src/chapter_03.md | 4 +-- openpgp/src/message/mod.rs | 73 +++++++++++++++++++++++++--------------------- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/guide/src/chapter_03.md b/guide/src/chapter_03.md index 91f07b04..19a42147 100644 --- a/guide/src/chapter_03.md +++ b/guide/src/chapter_03.md @@ -95,13 +95,13 @@ fn main() { [`PacketPile`]s are unstructured sequences of OpenPGP packets. Packet piles can be inspected, manipulated, validated using a formal grammar and thereby turned into [`Message`]s or [`Cert`]s using -[`Message::from_packet_pile`] or [`Cert::from_packet_pile`], or just +[`Message::try_from`] or [`Cert::from_packet_pile`], or just turned into a vector of [`Packet`]s: [`PacketPile`]: ../../sequoia_openpgp/struct.PacketPile.html [`Packet`]: ../../sequoia_openpgp/enum.Packet.html [`Cert::from_packet_pile`]: ../../sequoia_openpgp/cert/struct.Cert.html#method.from_packet_pile -[`Message::from_packet_pile`]: ../../sequoia_openpgp/struct.Message.html#method.from_packet_pile +[`Message::try_from`]: ../../sequoia_openpgp/struct.Message.html#method.try_from ```rust extern crate sequoia_openpgp as openpgp; diff --git a/openpgp/src/message/mod.rs b/openpgp/src/message/mod.rs index 9488e35b..975b8efd 100644 --- a/openpgp/src/message/mod.rs +++ b/openpgp/src/message/mod.rs @@ -10,6 +10,7 @@ //! //! [Section 11.3 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-11.3 +use std::convert::TryFrom; use std::fmt; use std::io; use std::path::Path; @@ -337,29 +338,29 @@ impl fmt::Debug for Message { impl<'a> Parse<'a, Message> for Message { /// Reads a `Message` from the specified reader. /// - /// See [`Message::from_packet_pile`] for more details. + /// See [`Message::try_from`] for more details. /// - /// [`Message::from_packet_pile`]: #method.from_packet_pile + /// [`Message::try_from`]: #method.try_from fn from_reader(reader: R) -> Result { - Self::from_packet_pile(PacketPile::from_reader(reader)?) + Self::try_from(PacketPile::from_reader(reader)?) } /// Reads a `Message` from the specified file. /// - /// See [`Message::from_packet_pile`] for more details. + /// See [`Message::try_from`] for more details. /// - /// [`Message::from_packet_pile`]: #method.from_packet_pile + /// [`Message::try_from`]: #method.try_from fn from_file>(path: P) -> Result { - Self::from_packet_pile(PacketPile::from_file(path)?) + Self::try_from(PacketPile::from_file(path)?) } /// Reads a `Message` from `buf`. /// - /// See [`Message::from_packet_pile`] for more details. + /// See [`Message::try_from`] for more details. /// - /// [`Message::from_packet_pile`]: #method.from_packet_pile + /// [`Message::try_from`]: #method.try_from fn from_bytes + ?Sized>(data: &'a D) -> Result { - Self::from_packet_pile(PacketPile::from_bytes(data)?) + Self::try_from(PacketPile::from_bytes(data)?) } } @@ -372,6 +373,34 @@ impl std::str::FromStr for Message { } impl Message { + /// Converts the vector of `Packets` to a `Message`. + /// + /// See [`Message::try_from`] for more details. + /// + /// [`Message::try_from`]: #method.try_from + pub fn from_packets(packets: Vec) -> Result { + Self::try_from(PacketPile::from(packets)) + } + + /// Returns the body of the message. + /// + /// Returns `None` if no literal data packet is found. This + /// happens if a SEIP container has not been decrypted. + pub fn body(&self) -> Option<&Literal> { + for packet in self.pile.descendants() { + if let &Packet::Literal(ref l) = packet { + return Some(l); + } + } + + // No literal data packet found. + None + } +} + +impl TryFrom for Message { + type Error = anyhow::Error; + /// Converts the `PacketPile` to a `Message`. /// /// Converting a `PacketPile` to a `Message` doesn't change the @@ -384,7 +413,7 @@ impl Message { /// or still compressed parts are valid messages. /// /// [Section 11.3 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-11.3 - pub fn from_packet_pile(pile: PacketPile) -> Result { + fn try_from(pile: PacketPile) -> Result { let mut v = MessageValidator::new(); for (mut path, packet) in pile.descendants().paths() { match packet { @@ -423,30 +452,6 @@ impl Message { MessageValidity::Error(e) => Err(e.into()), } } - - /// Converts the vector of `Packets` to a `Message`. - /// - /// See [`Message::from_packet_pile`] for more details. - /// - /// [`Message::from_packet_pile`]: #method.from_packet_pile - pub fn from_packets(packets: Vec) -> Result { - Self::from_packet_pile(PacketPile::from(packets)) - } - - /// Returns the body of the message. - /// - /// Returns `None` if no literal data packet is found. This - /// happens if a SEIP container has not been decrypted. - pub fn body(&self) -> Option<&Literal> { - for packet in self.pile.descendants() { - if let &Packet::Literal(ref l) = packet { - return Some(l); - } - } - - // No literal data packet found. - None - } } impl From for PacketPile { -- cgit v1.2.3