1
submitted 1 week ago* (last edited 1 week ago) by gomp@lemmy.ml to c/rust@lemmy.ml

I have two functions that are similar but can fail with different errors:

#[derive(Debug, thiserror::Error)]
enum MyError {
  #[error("error a")]
  MyErrorA,
  #[error("error b")]
  MyErrorB,
  #[error("bad value ({0})")]
  MyErrorCommon(String),
}

fn functionA() -> Result<String, MyError> {
  // can fail with MyErrorA MyErrorCommon
  todo!()
}

fn functionB() -> Result<String, MyError> {
  // can fail with MyErrorB MyErrorCommon
  todo!()
}

Is there an elegant (*) way I can express this?

If I split the error type into two separate types, is there a way to reuse the definition of MyErrorCommon?


(*) by "elegant" I mean something that improves the code - I'm sure one could define a few macros and solve that way, but I don't want to go there

edit: grammar (rust grammar)

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here
this post was submitted on 19 Nov 2024
1 points (100.0% liked)

Rust Programming

8190 readers
20 users here now

founded 5 years ago
MODERATORS