forked from Rust-related/RustPython
replace Either usage to Option
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
use bstr::ByteSlice;
|
||||
use itertools::Itertools;
|
||||
use num_bigint::BigInt;
|
||||
use num_traits::ToPrimitive;
|
||||
|
||||
use crate::anystr::{self, AnyStr, AnyStrContainer, AnyStrWrapper};
|
||||
use crate::builtins::bytearray::PyByteArray;
|
||||
use crate::builtins::bytes::{PyBytes, PyBytesRef};
|
||||
use crate::builtins::int::{PyInt, PyIntRef};
|
||||
use crate::builtins::pystr::{self, PyStr, PyStrRef};
|
||||
use crate::builtins::singletons::PyNoneRef;
|
||||
use crate::builtins::PyTypeRef;
|
||||
use crate::byteslike::try_bytes_like;
|
||||
use crate::cformat::CFormatBytes;
|
||||
@@ -21,6 +15,10 @@ use crate::{
|
||||
IdProtocol, PyComparisonValue, PyIterable, PyObjectRef, PyResult, PyValue,
|
||||
TryFromBorrowedObject,
|
||||
};
|
||||
use bstr::ByteSlice;
|
||||
use itertools::Itertools;
|
||||
use num_bigint::BigInt;
|
||||
use num_traits::ToPrimitive;
|
||||
use rustpython_common::hash;
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
@@ -209,23 +207,24 @@ impl ByteInnerPaddingOptions {
|
||||
#[derive(FromArgs)]
|
||||
pub struct ByteInnerTranslateOptions {
|
||||
#[pyarg(positional)]
|
||||
table: Either<PyBytesInner, PyNoneRef>,
|
||||
table: Option<PyBytesInner>,
|
||||
#[pyarg(any, optional)]
|
||||
delete: OptionalArg<PyBytesInner>,
|
||||
}
|
||||
|
||||
impl ByteInnerTranslateOptions {
|
||||
pub fn get_value(self, vm: &VirtualMachine) -> PyResult<(Vec<u8>, Vec<u8>)> {
|
||||
let table = match self.table {
|
||||
Either::A(v) => v.elements.to_vec(),
|
||||
Either::B(_) => (0..=255).collect::<Vec<u8>>(),
|
||||
};
|
||||
|
||||
if table.len() != 256 {
|
||||
return Err(
|
||||
vm.new_value_error("translation table must be 256 characters long".to_owned())
|
||||
);
|
||||
}
|
||||
let table = self.table.map_or_else(
|
||||
|| Ok((0..=255).collect::<Vec<u8>>()),
|
||||
|v| {
|
||||
if v.elements.len() != 256 {
|
||||
return Err(vm.new_value_error(
|
||||
"translation table must be 256 characters long".to_owned(),
|
||||
));
|
||||
}
|
||||
Ok(v.elements.to_vec())
|
||||
},
|
||||
)?;
|
||||
|
||||
let delete = match self.delete {
|
||||
OptionalArg::Present(byte) => byte.elements,
|
||||
|
||||
Reference in New Issue
Block a user