1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

impl from trait instead of into

This commit is contained in:
Arpit Bhadauria 2023-12-04 22:44:18 +00:00
parent 9ecd6a296e
commit 4d2ae8485c

View file

@ -89,8 +89,8 @@ impl RelationOp {
impl NumericOp { impl NumericOp {
fn eval(&self, left: &AstNode, right: &AstNode) -> ExprResult<NumOrStr> { fn eval(&self, left: &AstNode, right: &AstNode) -> ExprResult<NumOrStr> {
let a = <NumOrStr as Into<ExprResult<BigInt>>>::into(left.eval()?)?; let a = ExprResult::<BigInt>::from(left.eval()?)?;
let b = <NumOrStr as Into<ExprResult<BigInt>>>::into(right.eval()?)?; let b = ExprResult::<BigInt>::from(right.eval()?)?;
Ok(NumOrStr::Num(match self { Ok(NumOrStr::Num(match self {
Self::Add => a + b, Self::Add => a + b,
Self::Sub => a - b, Self::Sub => a - b,
@ -218,29 +218,29 @@ impl From<String> for NumOrStr {
} }
} }
impl Into<Option<usize>> for NumOrStr { impl From<NumOrStr> for Option<usize> {
fn into(self) -> Option<usize> { fn from(s: NumOrStr) -> Self {
match self.into() { match s.into() {
Ok(num) => num.to_usize(), Ok(num) => num.to_usize(),
Err(_) => None, Err(_) => None,
} }
} }
} }
impl Into<String> for NumOrStr { impl From<NumOrStr> for String {
fn into(self) -> String { fn from(s: NumOrStr) -> Self {
match self { match s {
Self::Num(num) => num.to_string(), NumOrStr::Num(num) => num.to_string(),
Self::Str(str) => str.to_string(), NumOrStr::Str(str) => str.to_string(),
} }
} }
} }
impl Into<ExprResult<BigInt>> for NumOrStr { impl From<NumOrStr> for ExprResult<BigInt> {
fn into(self) -> ExprResult<BigInt> { fn from(s: NumOrStr) -> Self {
match self { match s {
Self::Num(num) => Ok(num), NumOrStr::Num(num) => Ok(num),
Self::Str(str) => str NumOrStr::Str(str) => str
.parse::<BigInt>() .parse::<BigInt>()
.map_err(|_| ExprError::NonIntegerArgument), .map_err(|_| ExprError::NonIntegerArgument),
} }
@ -303,9 +303,8 @@ impl AstNode {
// //
// So we coerce errors into 0 to make that the only case we // So we coerce errors into 0 to make that the only case we
// have to care about. // have to care about.
let pos: usize = <NumOrStr as Into<Option<usize>>>::into(pos.eval()?).unwrap_or(0); let pos: usize = Option::<usize>::from(pos.eval()?).unwrap_or(0);
let length: usize = let length: usize = Option::<usize>::from(length.eval()?).unwrap_or(0);
<NumOrStr as Into<Option<usize>>>::into(length.eval()?).unwrap_or(0);
let (Some(pos), Some(_)) = (pos.checked_sub(1), length.checked_sub(1)) else { let (Some(pos), Some(_)) = (pos.checked_sub(1), length.checked_sub(1)) else {
return Ok(NumOrStr::from(String::new())); return Ok(NumOrStr::from(String::new()));
@ -315,11 +314,9 @@ impl AstNode {
string.chars().skip(pos).take(length).collect::<String>(), string.chars().skip(pos).take(length).collect::<String>(),
)) ))
} }
Self::Length { string } => Ok(NumOrStr::from( Self::Length { string } => {
<NumOrStr as Into<String>>::into(string.eval()?) Ok(NumOrStr::from(String::from(string.eval()?).chars().count()))
.chars() }
.count(),
)),
} }
} }
} }