From c784c8ec766edf4c7ab558f71d3f87e6c7c870b9 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 26 Nov 2020 16:04:23 +0100 Subject: Use named fields in `ExpandResult` --- crates/mbe/src/lib.rs | 24 +++++++++++++----------- crates/mbe/src/mbe_expander.rs | 15 +++++++++------ crates/mbe/src/mbe_expander/matcher.rs | 9 +++++---- crates/mbe/src/mbe_expander/transcriber.rs | 18 +++++++++++------- 4 files changed, 38 insertions(+), 28 deletions(-) (limited to 'crates/mbe') diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index f854ca09a..183e3b988 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs @@ -246,33 +246,35 @@ fn validate(pattern: &tt::Subtree) -> Result<(), ParseError> { Ok(()) } -#[derive(Debug)] -pub struct ExpandResult(pub T, pub Option); +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct ExpandResult { + pub value: T, + pub err: Option, +} impl ExpandResult { - pub fn ok(t: T) -> ExpandResult { - ExpandResult(t, None) + pub fn ok(value: T) -> Self { + Self { value, err: None } } - pub fn only_err(err: ExpandError) -> ExpandResult + pub fn only_err(err: ExpandError) -> Self where T: Default, { - ExpandResult(Default::default(), Some(err)) + Self { value: Default::default(), err: Some(err) } } pub fn map(self, f: impl FnOnce(T) -> U) -> ExpandResult { - ExpandResult(f(self.0), self.1) + ExpandResult { value: f(self.value), err: self.err } } pub fn result(self) -> Result { - self.1.map(Err).unwrap_or(Ok(self.0)) + self.err.map(Err).unwrap_or(Ok(self.value)) } } impl From> for ExpandResult { - fn from(result: Result) -> ExpandResult { - result - .map_or_else(|e| ExpandResult(Default::default(), Some(e)), |it| ExpandResult(it, None)) + fn from(result: Result) -> Self { + result.map_or_else(|e| Self::only_err(e), |it| Self::ok(it)) } } diff --git a/crates/mbe/src/mbe_expander.rs b/crates/mbe/src/mbe_expander.rs index 1ad8b9f8a..97bce0536 100644 --- a/crates/mbe/src/mbe_expander.rs +++ b/crates/mbe/src/mbe_expander.rs @@ -28,10 +28,10 @@ fn expand_rules(rules: &[crate::Rule], input: &tt::Subtree) -> ExpandResult ExpandResult String { diff --git a/crates/mbe/src/mbe_expander/matcher.rs b/crates/mbe/src/mbe_expander/matcher.rs index 39a8eefbd..3f8445897 100644 --- a/crates/mbe/src/mbe_expander/matcher.rs +++ b/crates/mbe/src/mbe_expander/matcher.rs @@ -158,7 +158,8 @@ fn match_subtree( continue; } }; - let ExpandResult(matched, match_err) = match_meta_var(kind.as_str(), src); + let ExpandResult { value: matched, err: match_err } = + match_meta_var(kind.as_str(), src); match matched { Some(fragment) => { res.bindings.inner.insert(name.clone(), Binding::Fragment(fragment)); @@ -342,17 +343,17 @@ impl<'a> TtIter<'a> { token_trees: res.into_iter().cloned().collect(), })), }; - ExpandResult(res, err) + ExpandResult { value: res, err } } pub(crate) fn eat_vis(&mut self) -> Option { let mut fork = self.clone(); match fork.expect_fragment(Visibility) { - ExpandResult(tt, None) => { + ExpandResult { value: tt, err: None } => { *self = fork; tt } - ExpandResult(_, Some(_)) => None, + ExpandResult { value: _, err: Some(_) } => None, } } } diff --git a/crates/mbe/src/mbe_expander/transcriber.rs b/crates/mbe/src/mbe_expander/transcriber.rs index c9525c5bf..616119ba9 100644 --- a/crates/mbe/src/mbe_expander/transcriber.rs +++ b/crates/mbe/src/mbe_expander/transcriber.rs @@ -93,17 +93,18 @@ fn expand_subtree( match op { Op::TokenTree(tt @ tt::TokenTree::Leaf(..)) => arena.push(tt.clone()), Op::TokenTree(tt::TokenTree::Subtree(tt)) => { - let ExpandResult(tt, e) = expand_subtree(ctx, tt, arena); + let ExpandResult { value: tt, err: e } = expand_subtree(ctx, tt, arena); err = err.or(e); arena.push(tt.into()); } Op::Var { name, kind: _ } => { - let ExpandResult(fragment, e) = expand_var(ctx, name); + let ExpandResult { value: fragment, err: e } = expand_var(ctx, name); err = err.or(e); push_fragment(arena, fragment); } Op::Repeat { subtree, kind, separator } => { - let ExpandResult(fragment, e) = expand_repeat(ctx, subtree, kind, separator, arena); + let ExpandResult { value: fragment, err: e } = + expand_repeat(ctx, subtree, kind, separator, arena); err = err.or(e); push_fragment(arena, fragment) } @@ -111,7 +112,7 @@ fn expand_subtree( } // drain the elements added in this instance of expand_subtree let tts = arena.drain(start_elements..arena.len()).collect(); - ExpandResult(tt::Subtree { delimiter: template.delimiter, token_trees: tts }, err) + ExpandResult { value: tt::Subtree { delimiter: template.delimiter, token_trees: tts }, err } } fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult { @@ -152,7 +153,7 @@ fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult { ExpandResult::ok(Fragment::Tokens(tt)) } else { ctx.bindings.get(&v, &mut ctx.nesting).map_or_else( - |e| ExpandResult(Fragment::Tokens(tt::TokenTree::empty()), Some(e)), + |e| ExpandResult { value: Fragment::Tokens(tt::TokenTree::empty()), err: Some(e) }, |b| ExpandResult::ok(b.clone()), ) } @@ -174,7 +175,7 @@ fn expand_repeat( let mut counter = 0; loop { - let ExpandResult(mut t, e) = expand_subtree(ctx, template, arena); + let ExpandResult { value: mut t, err: e } = expand_subtree(ctx, template, arena); let nesting_state = ctx.nesting.last_mut().unwrap(); if nesting_state.at_end || !nesting_state.hit { break; @@ -234,7 +235,10 @@ fn expand_repeat( let tt = tt::Subtree { delimiter: None, token_trees: buf }.into(); if RepeatKind::OneOrMore == kind && counter == 0 { - return ExpandResult(Fragment::Tokens(tt), Some(ExpandError::UnexpectedToken)); + return ExpandResult { + value: Fragment::Tokens(tt), + err: Some(ExpandError::UnexpectedToken), + }; } ExpandResult::ok(Fragment::Tokens(tt)) } -- cgit v1.2.3