Don't use badarg for TryFrom<Term> for Base
use std::convert::{TryFrom, TryInto};
-use liblumen_alloc::badarg;
-use liblumen_alloc::erts::exception::Exception;
use liblumen_alloc::erts::term::prelude::*;
// 2-36
@@ -16,35 +14,20 @@ impl Base {
self.0 as u32
}
- const MIN_BASE: u8 = 2;
- const MAX_BASE: u8 = 36;
+ const MIN: u8 = 2;
+ const MAX: u8 = 36;
}
impl TryFrom<Term> for Base {
- type Error = Exception;
+ type Error = TryIntoIntegerError;
fn try_from(term: Term) -> Result<Self, Self::Error> {
- term.decode().unwrap().try_into()
- }
-}
-
-impl TryFrom<TypedTerm> for Base {
- type Error = Exception;
+ let base_u8: u8 = term.try_into()?;
- fn try_from(typed_term: TypedTerm) -> Result<Self, Self::Error> {
- match typed_term {
- TypedTerm::SmallInteger(small_integer) => {
- let small_integer_isize: isize = small_integer.into();
-
- if (Self::MIN_BASE as isize) <= small_integer_isize
- && small_integer_isize <= (Self::MAX_BASE as isize)
- {
- Ok(Base(small_integer_isize as u8))
+ if (Self::MIN <= base_u8) && (base_u8 <= Self::MAX) {
+ Ok(Self(base_u8))
} else {
- Err(badarg!().into())
- }
- }
- _ => Err(badarg!().into()),
+ Err(TryIntoIntegerError::OutOfRange)
}
}
}
