diff options
-rw-r--r-- | openpgp/src/message/mod.rs | 14 | ||||
-rw-r--r-- | openpgp/src/packet/mod.rs | 38 | ||||
-rw-r--r-- | openpgp/src/packet_pile.rs | 24 | ||||
-rw-r--r-- | openpgp/src/parse/packet_pile_parser.rs | 6 | ||||
-rw-r--r-- | openpgp/src/serialize/mod.rs | 22 |
5 files changed, 62 insertions, 42 deletions
diff --git a/openpgp/src/message/mod.rs b/openpgp/src/message/mod.rs index 0efd6bef..517c859f 100644 --- a/openpgp/src/message/mod.rs +++ b/openpgp/src/message/mod.rs @@ -380,7 +380,9 @@ impl Message { // we treat the content as an opaque message. path.push(0); - if packet.children.is_none() && packet.body().is_some() { + if packet.children().next().is_none() + && packet.body().is_some() + { v.push_token(Token::OpaqueContent, &path); } } @@ -996,10 +998,10 @@ mod tests { // 1: MDC // => good. let mut seip = SEIP1::new(); - seip.common.children = Some(Container::new()); - seip.common.children.as_mut().unwrap().push( + seip.set_children(Some(Container::new())); + seip.children_mut().unwrap().push( lit.clone().into()); - seip.common.children.as_mut().unwrap().push( + seip.children_mut().unwrap().push( MDC::from([0u8; 20]).into()); packets[1] = seip.into(); @@ -1079,7 +1081,7 @@ mod tests { // 2: Literal // => bad. packets.remove(3); - packets[2].children.as_mut().unwrap().push(lit.clone().into()); + packets[2].children_mut().unwrap().push(lit.clone().into()); assert!(packets.iter().map(|p| p.tag()).collect::<Vec<Tag>>() == [ Tag::SKESK, Tag::SKESK, Tag::SEIP ]); @@ -1093,7 +1095,7 @@ mod tests { // 2: SEIP // 0: Literal // => good. - packets[2].children.as_mut().unwrap().packets.pop().unwrap(); + packets[2].children_mut().unwrap().packets.pop().unwrap(); #[allow(deprecated)] packets.insert( diff --git a/openpgp/src/packet/mod.rs b/openpgp/src/packet/mod.rs index b048e271..a3f81168 100644 --- a/openpgp/src/packet/mod.rs +++ b/openpgp/src/packet/mod.rs @@ -118,7 +118,7 @@ pub struct Common { /// [`PacketPile`]: ../struct.PacketPile.html /// [`PacketPile::from_file`]: ../struct.PacketPile.html#method.from_file /// [`PacketParser`]: ../parse/struct.PacketParser.html - pub children: Option<Container>, + children: Option<Container>, /// Holds a packet's body. /// @@ -186,16 +186,40 @@ impl Default for Common { } impl Common { + pub(crate) // for packet_pile.rs + fn children_ref(&self) -> Option<&Container> { + self.children.as_ref() + } + + pub(crate) // for packet_pile.rs + fn children_mut(&mut self) -> Option<&mut Container> { + self.children.as_mut() + } + + pub(crate) // for packet_pile.rs + fn set_children(&mut self, v: Option<Container>) -> Option<Container> { + std::mem::replace(&mut self.children, v) + } + + fn children_iter<'a>(&'a self) -> slice::Iter<'a, Packet> { + if let Some(ref container) = self.children { + container.packets.iter() + } else { + let empty_packet_slice : &[Packet] = &[]; + empty_packet_slice.iter() + } + } + + /// Returns an iterator over the packet's immediate children. + pub fn children<'a>(&'a self) -> impl Iterator<Item = &'a Packet> { + self.children_iter() + } + /// Returns an iterator over all of the packet's descendants, in /// depth-first order. pub fn descendants(&self) -> Iter { return Iter { - children: if let Some(ref container) = self.children { - container.packets.iter() - } else { - let empty_packet_slice : &[Packet] = &[]; - empty_packet_slice.iter() - }, + children: self.children_iter(), child: None, grandchildren: None, depth: 0, diff --git a/openpgp/src/packet_pile.rs b/openpgp/src/packet_pile.rs index 76c8b611..d5958b7c 100644 --- a/openpgp/src/packet_pile.rs +++ b/openpgp/src/packet_pile.rs @@ -126,7 +126,7 @@ impl PacketPile { if *i < c.packets.len() { let p = &c.packets[*i]; packet = Some(p); - cont = p.children.as_ref(); + cont = p.children_ref(); continue; } } @@ -156,7 +156,7 @@ impl PacketPile { return Some(p) } - container = p.children.as_mut().unwrap(); + container = p.children_mut().unwrap(); } None @@ -244,12 +244,12 @@ impl PacketPile { } let p = &mut tmp.packets[i]; - if p.children.is_none() { + if p.children_ref().is_none() { match p { Packet::CompressedData(_) | Packet::SEIP(_) => { // We have a container with no children. // That's okay. We can create the container. - p.children = Some(Container::new()); + p.set_children(Some(Container::new())); }, _ => { return Err(Error::IndexOutOfRange.into()); @@ -257,7 +257,7 @@ impl PacketPile { } } - container = p.children.as_mut().unwrap(); + container = p.children_mut().unwrap(); } return Err(Error::IndexOutOfRange.into()); @@ -332,7 +332,7 @@ impl PacketPile { let packets_len = tmp.packets.len(); let p = &mut tmp.packets[packets_len - 1]; - container = p.children.as_mut().unwrap(); + container = p.children_mut().unwrap(); } if relative_position < 0 { @@ -347,9 +347,9 @@ impl PacketPile { // Create a new container. let tmp = container; let i = tmp.packets.len() - 1; - assert!(tmp.packets[i].children.is_none()); - tmp.packets[i].children = Some(Container::new()); - container = tmp.packets[i].children.as_mut().unwrap(); + assert!(tmp.packets[i].children_ref().is_none()); + tmp.packets[i].set_children(Some(Container::new())); + container = tmp.packets[i].children_mut().unwrap(); } container.packets.push(packet); @@ -644,8 +644,8 @@ mod test { } let mut seip = SEIP1::new(); - seip.common.children = Some(Container::new()); - seip.common.children.as_mut().unwrap().push(cd.into()); + seip.set_children(Some(Container::new())); + seip.children_mut().unwrap().push(cd.into()); packets.push(seip.into()); eprintln!("{:#?}", packets); @@ -813,7 +813,7 @@ mod test { assert_eq!(top_level.len(), 1); let values = top_level[0] - .children.as_ref().unwrap().children() + .children() .map(|p| { if let Packet::Literal(ref literal) = p { literal.body().unwrap() diff --git a/openpgp/src/parse/packet_pile_parser.rs b/openpgp/src/parse/packet_pile_parser.rs index e1525707..36247e2e 100644 --- a/openpgp/src/parse/packet_pile_parser.rs +++ b/openpgp/src/parse/packet_pile_parser.rs @@ -146,17 +146,17 @@ impl<'a> PacketPileParser<'a> { let tmp = container; let packets_len = tmp.packets.len(); let p = &mut tmp.packets[packets_len - 1]; - if p.children.is_none() { + if p.children().next().is_none() { if i == position - 1 { // This is the leaf. Create a new container // here. - p.children = Some(Container::new()); + p.set_children(Some(Container::new())); } else { panic!("Internal inconsistency while building message."); } } - container = p.children.as_mut().unwrap(); + container = p.children_mut().unwrap(); } container.packets.push(packet); diff --git a/openpgp/src/serialize/mod.rs b/openpgp/src/serialize/mod.rs index 82b68bd8..fe1a901d 100644 --- a/openpgp/src/serialize/mod.rs +++ b/openpgp/src/serialize/mod.rs @@ -1676,8 +1676,7 @@ impl Serialize for CompressedData { eprintln!("CompressedData::serialize(\ algo: {}, {:?} children, {:?} bytes)", self.algorithm(), - self.common.children.as_ref().map( - |cont| cont.children().len()), + self.children().count(), self.body().as_ref().map(|body| body.len())); } @@ -1686,10 +1685,8 @@ impl Serialize for CompressedData { o, self.algorithm(), Default::default(), 0)?; // Serialize the packets. - if let Some(ref children) = self.common.children { - for p in children.children() { - p.serialize(&mut o)?; - } + for p in self.children() { + p.serialize(&mut o)?; } // Append the data. @@ -1704,10 +1701,7 @@ impl Serialize for CompressedData { impl NetLength for CompressedData { fn net_len(&self) -> usize { let inner_length = - self.common.children.as_ref().map(|children| { - children.packets.iter().map(|p| p.serialized_len()) - .sum() - }).unwrap_or(0) + self.children().map(|p| p.serialized_len()).sum::<usize>() + self.body().as_ref().map(|body| body.len()).unwrap_or(0); // Worst case, the data gets larger. Account for that. @@ -1906,7 +1900,7 @@ impl Serialize for SEIP { /// To construct an encrypted message, use /// `serialize::stream::Encryptor`. fn serialize(&self, o: &mut dyn std::io::Write) -> Result<()> { - if let Some(ref _children) = self.common.children { + if self.children().next().is_some() { return Err(Error::InvalidOperation( "Cannot encrypt, use serialize::stream::Encryptor".into()) .into()); @@ -1930,7 +1924,7 @@ impl NetLength for SEIP { impl SerializeInto for SEIP { fn serialized_len(&self) -> usize { - if self.common.children.is_some() { + if self.children().next().is_some() { 0 // XXX } else { self.gross_len() @@ -2009,7 +2003,7 @@ impl Serialize for AED1 { /// To construct an encrypted message, use /// `serialize::stream::Encryptor`. fn serialize(&self, o: &mut dyn std::io::Write) -> Result<()> { - if let Some(ref _children) = self.common.children { + if self.children().next().is_some() { return Err(Error::InvalidOperation( "Cannot encrypt, use serialize::stream::Encryptor".into()) .into()); @@ -2027,7 +2021,7 @@ impl Serialize for AED1 { impl NetLength for AED1 { fn net_len(&self) -> usize { - if self.common.children.is_some() { + if self.children().next().is_some() { 0 } else { 4 // Headers. |