From d9513492fe8a45af9b2f51a05ba93ddd089848b9 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sat, 9 May 2020 11:00:50 -0400 Subject: [PATCH] Move MaybeItem into tokenize.rs --- tor-netdoc/src/parse.rs | 40 +------------------------------------- tor-netdoc/src/tokenize.rs | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/tor-netdoc/src/parse.rs b/tor-netdoc/src/parse.rs index c0e8302b1..204b85208 100644 --- a/tor-netdoc/src/parse.rs +++ b/tor-netdoc/src/parse.rs @@ -12,7 +12,6 @@ use crate::rules::*; use crate::tokenize::*; use crate::{Error, Position, Result}; -use std::str::FromStr; /// Describe the rules for one section of a document. /// @@ -121,7 +120,7 @@ impl<'a, T: Keyword> Section<'a, T> { /// A MaybeItem is used to represent an object that might or might /// not be there. pub fn maybe<'b>(&'b self, t: T) -> MaybeItem<'b, 'a> { - MaybeItem(self.get(t)) + MaybeItem::from_option(self.get(t)) } /// Parsing implementation: try to insert an `item`. /// @@ -287,40 +286,3 @@ impl SectionRules { Ok(section) } } - -/// Represents an Item that might not be present, whose arguments we -/// want to inspect. If the Item is there, this acts like a proxy to the -/// item; otherwise, it treats the item as having no arguments. - -pub struct MaybeItem<'a, 'b>(Option<&'a Item<'b>>); - -// All methods here are as for Item. -impl<'a, 'b> MaybeItem<'a, 'b> { - pub fn parse_arg(&self, idx: usize) -> Result> - where - ::Err: std::error::Error, - { - match self.0 { - Some(item) => item.parse_arg(idx).map(Some), - None => Ok(None), // XXXX is this correct? - } - } - pub fn parse_optional_arg(&self, idx: usize) -> Result> - where - ::Err: std::error::Error, - { - match self.0 { - Some(item) => item.parse_optional_arg(idx), - None => Ok(None), - } - } - pub fn args_as_str(&self) -> Option<&str> { - self.0.map(|item| item.args_as_str()) - } - pub fn get_obj(&self, want_tag: &str) -> Result>> { - match self.0 { - Some(item) => Ok(Some(item.get_obj(want_tag)?)), - None => Ok(None), - } - } -} diff --git a/tor-netdoc/src/tokenize.rs b/tor-netdoc/src/tokenize.rs index 07fa8b88f..c8e000334 100644 --- a/tor-netdoc/src/tokenize.rs +++ b/tor-netdoc/src/tokenize.rs @@ -336,3 +336,43 @@ impl<'a> Item<'a> { Position::from_offset(s, self.off) } } + +/// Represents an Item that might not be present, whose arguments we +/// want to inspect. If the Item is there, this acts like a proxy to the +/// item; otherwise, it treats the item as having no arguments. + +pub struct MaybeItem<'a, 'b>(Option<&'a Item<'b>>); + +// All methods here are as for Item. +impl<'a, 'b> MaybeItem<'a, 'b> { + pub fn from_option(opt: Option<&'a Item<'b>>) -> Self { + MaybeItem(opt) + } + pub fn parse_arg(&self, idx: usize) -> Result> + where + ::Err: std::error::Error, + { + match self.0 { + Some(item) => item.parse_arg(idx).map(Some), + None => Ok(None), // XXXX is this correct? + } + } + pub fn parse_optional_arg(&self, idx: usize) -> Result> + where + ::Err: std::error::Error, + { + match self.0 { + Some(item) => item.parse_optional_arg(idx), + None => Ok(None), + } + } + pub fn args_as_str(&self) -> Option<&str> { + self.0.map(|item| item.args_as_str()) + } + pub fn get_obj(&self, want_tag: &str) -> Result>> { + match self.0 { + Some(item) => Ok(Some(item.get_obj(want_tag)?)), + None => Ok(None), + } + } +}