diff --git a/tasks/ast_codegen/src/util.rs b/tasks/ast_codegen/src/util.rs index cb63a9abd7612f..615037ea997d6f 100644 --- a/tasks/ast_codegen/src/util.rs +++ b/tasks/ast_codegen/src/util.rs @@ -7,6 +7,9 @@ use crate::{CodegenCtx, TypeRef}; pub trait NormalizeError { fn normalize(self) -> crate::Result; + fn normalize_with(self, err: E) -> crate::Result + where + E: ToString; } impl NormalizeError for Result @@ -16,6 +19,26 @@ where fn normalize(self) -> crate::Result { self.map_err(|e| e.to_string()) } + + fn normalize_with(self, err: U) -> crate::Result + where + U: ToString, + { + self.map_err(|_| err.to_string()) + } +} + +impl NormalizeError for Option { + fn normalize(self) -> crate::Result { + self.normalize_with(String::default()) + } + + fn normalize_with(self, err: E) -> crate::Result + where + E: ToString, + { + self.map_or_else(|| Err(err.to_string()), |r| Ok(r)) + } } pub trait TokenStreamExt {