diff options
Diffstat (limited to 'crates/ra_mbe/src/lib.rs')
-rw-r--r-- | crates/ra_mbe/src/lib.rs | 32 |
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 | ||
33 | pub type ExpandResult<T> = (T, Option<ExpandError>); | ||
34 | |||
35 | pub use crate::syntax_bridge::{ | 33 | pub 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 | ||
212 | pub struct ExpandResult<T>(pub T, pub Option<ExpandError>); | ||
213 | |||
214 | impl<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 | |||
235 | impl<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)] |
215 | mod tests; | 243 | mod tests; |