From 96f3d8c846c7c6a0c06e19690e4a7d64c8b83817 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 9 Jun 2020 12:38:21 -0400 Subject: [PATCH] Add a "last_item" entry for Section. This will make it simpler to implement digests for sections. --- tor-netdoc/src/parse.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tor-netdoc/src/parse.rs b/tor-netdoc/src/parse.rs index 9a0c35c52..544cb79e3 100644 --- a/tor-netdoc/src/parse.rs +++ b/tor-netdoc/src/parse.rs @@ -72,12 +72,21 @@ impl<'a, K: Keyword> TokVal<'a, K> { TokVal::Multi(v) => &v[..], } } + /// Return the last Item for this value, if any. + fn last(&self) -> Option<&Item<'a, K>> { + match self { + TokVal::None => None, + TokVal::Some([t]) => Some(&t), + TokVal::Multi(v) => Some(&v[v.len() - 1]), + } + } } /// A Section is the result of sorting a document's entries by keyword. pub struct Section<'a, T: Keyword> { /// Map from Keyword index to TokVal v: Vec>, + last: Option, } impl<'a, T: Keyword> Section<'a, T> { @@ -86,7 +95,8 @@ impl<'a, T: Keyword> Section<'a, T> { let n = T::n_vals(); let mut v = Vec::with_capacity(n); v.resize(n, TokVal::None); - Section { v } + let last = None; + Section { v, last } } /// Helper: return the tokval for some Keyword. fn get_tokval(&self, t: T) -> &TokVal<'a, T> { @@ -116,6 +126,14 @@ 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 last item that was accepted for this section, or None + /// if no items were accepted for this section. + pub fn last_item(&self) -> Option<&Item<'a, T>> { + match self.last { + None => None, + Some(t) => self.get_tokval(t).last(), + } + } /// Insert an `item`. /// /// The `item` must have parsed Keyword `t`. @@ -135,6 +153,7 @@ impl<'a, T: Keyword> Section<'a, T> { v.push(item); } }; + self.last = Some(t); } }