From 47c3a11d2c7758ed9cbc3810216746cf44436094 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 9 Jun 2020 12:38:21 -0400 Subject: [PATCH] Add a "first_item" entry for Section. This will make it simpler to implement digests for sections. --- tor-netdoc/src/parse.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tor-netdoc/src/parse.rs b/tor-netdoc/src/parse.rs index 544cb79e3..c20f037ff 100644 --- a/tor-netdoc/src/parse.rs +++ b/tor-netdoc/src/parse.rs @@ -86,6 +86,7 @@ impl<'a, K: Keyword> TokVal<'a, K> { pub struct Section<'a, T: Keyword> { /// Map from Keyword index to TokVal v: Vec>, + first: Option, last: Option, } @@ -95,8 +96,11 @@ impl<'a, T: Keyword> Section<'a, T> { let n = T::n_vals(); let mut v = Vec::with_capacity(n); v.resize(n, TokVal::None); - let last = None; - Section { v, last } + Section { + v, + first: None, + last: None, + } } /// Helper: return the tokval for some Keyword. fn get_tokval(&self, t: T) -> &TokVal<'a, T> { @@ -126,8 +130,18 @@ impl<'a, T: Keyword> Section<'a, T> { pub fn maybe<'b>(&'b self, t: T) -> MaybeItem<'b, 'a, T> { MaybeItem::from_option(self.get(t)) } + /// Return the first item that was accepted for this section, or None + /// if no items were accepted for this section. + #[allow(dead_code)] + pub fn first_item(&self) -> Option<&Item<'a, T>> { + match self.first { + None => None, + Some(t) => self.get_tokval(t).first(), + } + } /// Return the last item that was accepted for this section, or None /// if no items were accepted for this section. + #[allow(dead_code)] pub fn last_item(&self) -> Option<&Item<'a, T>> { match self.last { None => None, @@ -153,6 +167,9 @@ impl<'a, T: Keyword> Section<'a, T> { v.push(item); } }; + if self.first.is_none() { + self.first = Some(t); + } self.last = Some(t); } }