I have something like:
#[derive(Debug)]
pub struct InputData {
pos_string: Option<String>,
}
#[derive(Debug)]
pub struct ParsedData {
pos_string: Option<ParsedString>,
}
impl TryFrom<InputData> for ParsedData {
type Error = String;
fn try_from(value: InputData) -> Result<Self, Self::Error> {
let pos_string = match &value.pos_string {
Some(s) => Some(ParsedString::parse(s.to_string())?),
None => None,
};
Ok(Self { pos_string })
}
}
struct ParsedString(String);
impl ParsedString {
fn parse(s: String) -> Result<Self, String> {
if s == "string" {
Ok(Self(s))
} else {
Err(format!("{} is not a valid string.", s))
}
}
}
Is there some nicer way of doing the part:
let pos_string = match &value.pos_string {
Some(s) => Some(ParsedString::parse(s.to_string())?),
None => None,
};