aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/lib.rs
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2020-03-16 11:22:10 +0000
committerFlorian Diebold <[email protected]>2020-03-16 17:38:19 +0000
commitd655749aaeb31461f9af923bbf0b36d219cff343 (patch)
treed7f8e8a11bd4794567fb24ac29b2b85ebd247881 /crates/ra_mbe/src/lib.rs
parentf3c6a2e3dbe477a7e0ac714a5bdbda6e8838fcfa (diff)
Turn ExpandResult into struct
Diffstat (limited to 'crates/ra_mbe/src/lib.rs')
-rw-r--r--crates/ra_mbe/src/lib.rs32
1 files changed, 30 insertions, 2 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index 3adec4978..6a9037bfc 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -30,8 +30,6 @@ pub enum ExpandError {
30 InvalidRepeat, 30 InvalidRepeat,
31} 31}
32 32
33pub type ExpandResult<T> = (T, Option<ExpandError>);
34
35pub use crate::syntax_bridge::{ 33pub use crate::syntax_bridge::{
36 ast_to_token_tree, parse_to_token_tree, syntax_node_to_token_tree, token_tree_to_syntax_node, 34 ast_to_token_tree, parse_to_token_tree, syntax_node_to_token_tree, token_tree_to_syntax_node,
37 TokenMap, 35 TokenMap,
@@ -211,5 +209,35 @@ fn validate(pattern: &tt::Subtree) -> Result<(), ParseError> {
211 Ok(()) 209 Ok(())
212} 210}
213 211
212pub struct ExpandResult<T>(pub T, pub Option<ExpandError>);
213
214impl<T> ExpandResult<T> {
215 pub fn ok(t: T) -> ExpandResult<T> {
216 ExpandResult(t, None)
217 }
218
219 pub fn only_err(err: ExpandError) -> ExpandResult<T>
220 where
221 T: Default,
222 {
223 ExpandResult(Default::default(), Some(err))
224 }
225
226 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExpandResult<U> {
227 ExpandResult(f(self.0), self.1)
228 }
229
230 pub fn result(self) -> Result<T, ExpandError> {
231 self.1.map(Err).unwrap_or(Ok(self.0))
232 }
233}
234
235impl<T: Default> From<Result<T, ExpandError>> for ExpandResult<T> {
236 fn from(result: Result<T, ExpandError>) -> ExpandResult<T> {
237 result
238 .map_or_else(|e| ExpandResult(Default::default(), Some(e)), |it| ExpandResult(it, None))
239 }
240}
241
214#[cfg(test)] 242#[cfg(test)]
215mod tests; 243mod tests;