Skip to content

Commit

Permalink
added error handling for from_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
joalopez1206 committed Dec 29, 2024
1 parent ea1f664 commit dfa7bc9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/message/rdata/opt_rdata/option_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ impl ToBytes for OptionData {
}

impl OptionData {
pub fn from_with_opt_type(bytes: Vec<u8>, opt_t: OptionCode) -> OptionData {
pub fn from_with_opt_type(bytes: Vec<u8>, opt_t: OptionCode) -> Result<OptionData, &'static str> {
match opt_t {
OptionCode::NSID => {
OptionData::NSID(String::from_utf8(bytes).unwrap())
let nsid = String::from_utf8(bytes).map_err(|_| "Error parsing NSID")?;
Ok(OptionData::NSID(nsid))
},
OptionCode::EDE => {
OptionData::EDE(EdeOptData::from_bytes(&bytes).unwrap())
let ede = EdeOptData::from_bytes(&bytes).map_err(|_| "Error parsing EDE")?;
Ok(OptionData::EDE(ede))
},
_ => OptionData::Unknown(bytes)
_ => Ok(OptionData::Unknown(bytes))
}
}
}

0 comments on commit dfa7bc9

Please sign in to comment.