Skip to content

Commit

Permalink
feat(ast_codegen): add normalize_with to create errors from options.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Jul 30, 2024
1 parent af18a31 commit e3a6632
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tasks/ast_codegen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use crate::{CodegenCtx, TypeRef};

pub trait NormalizeError<T> {
fn normalize(self) -> crate::Result<T>;
fn normalize_with<E>(self, err: E) -> crate::Result<T>
where
E: ToString;
}

impl<T, E> NormalizeError<T> for Result<T, E>
Expand All @@ -16,6 +19,26 @@ where
fn normalize(self) -> crate::Result<T> {
self.map_err(|e| e.to_string())
}

fn normalize_with<U>(self, err: U) -> crate::Result<T>
where
U: ToString,
{
self.map_err(|_| err.to_string())
}
}

impl<T> NormalizeError<T> for Option<T> {
fn normalize(self) -> crate::Result<T> {
self.normalize_with(String::default())
}

fn normalize_with<E>(self, err: E) -> crate::Result<T>
where
E: ToString,
{
self.map_or_else(|| Err(err.to_string()), |r| Ok(r))
}
}

pub trait TokenStreamExt {
Expand Down

0 comments on commit e3a6632

Please sign in to comment.