aboutsummaryrefslogtreecommitdiff
path: root/crates/mbe/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/mbe/src/lib.rs')
-rw-r--r--crates/mbe/src/lib.rs24
1 files changed, 13 insertions, 11 deletions
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> {
246 Ok(()) 246 Ok(())
247} 247}
248 248
249#[derive(Debug)] 249#[derive(Debug, Clone, Eq, PartialEq)]
250pub struct ExpandResult<T>(pub T, pub Option<ExpandError>); 250pub struct ExpandResult<T> {
251 pub value: T,
252 pub err: Option<ExpandError>,
253}
251 254
252impl<T> ExpandResult<T> { 255impl<T> ExpandResult<T> {
253 pub fn ok(t: T) -> ExpandResult<T> { 256 pub fn ok(value: T) -> Self {
254 ExpandResult(t, None) 257 Self { value, err: None }
255 } 258 }
256 259
257 pub fn only_err(err: ExpandError) -> ExpandResult<T> 260 pub fn only_err(err: ExpandError) -> Self
258 where 261 where
259 T: Default, 262 T: Default,
260 { 263 {
261 ExpandResult(Default::default(), Some(err)) 264 Self { value: Default::default(), err: Some(err) }
262 } 265 }
263 266
264 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExpandResult<U> { 267 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExpandResult<U> {
265 ExpandResult(f(self.0), self.1) 268 ExpandResult { value: f(self.value), err: self.err }
266 } 269 }
267 270
268 pub fn result(self) -> Result<T, ExpandError> { 271 pub fn result(self) -> Result<T, ExpandError> {
269 self.1.map(Err).unwrap_or(Ok(self.0)) 272 self.err.map(Err).unwrap_or(Ok(self.value))
270 } 273 }
271} 274}
272 275
273impl<T: Default> From<Result<T, ExpandError>> for ExpandResult<T> { 276impl<T: Default> From<Result<T, ExpandError>> for ExpandResult<T> {
274 fn from(result: Result<T, ExpandError>) -> ExpandResult<T> { 277 fn from(result: Result<T, ExpandError>) -> Self {
275 result 278 result.map_or_else(|e| Self::only_err(e), |it| Self::ok(it))
276 .map_or_else(|e| ExpandResult(Default::default(), Some(e)), |it| ExpandResult(it, None))
277 } 279 }
278} 280}