Fix functions

This commit is contained in:
yt2b committed 2023-01-06 22:56:30 +09:00
1 parent ba427c8690
commit 32cf0185bd
4 files changed
+9 -15

No files matched your search

+3 -3
View File
@@ -271,7 +271,7 @@ fn parse_precision(text: &str) -> Result<(Option<usize>, &str), &'static str> {
}
impl FormatSpec {
pub fn parse(text: &str) -> Result<Self, &'static str> {
pub fn parse(text: &str) -> Result<Self, String> {
// get_integer in CPython
let (preconversor, text) = FormatPreconversor::parse(text);
let (mut fill, mut align, text) = parse_fill_and_align(text);
@@ -283,7 +283,7 @@ impl FormatSpec {
let (precision, text) = parse_precision(text)?;
let (format_type, text) = FormatType::parse(text);
if !text.is_empty() {
return Err("Invalid format specifier");
return Err("Invalid format specifier".to_owned());
}
if zero && fill.is_none() {
@@ -685,7 +685,7 @@ pub enum FormatParseError {
}
impl FromStr for FormatSpec {
type Err = &'static str;
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
FormatSpec::parse(s)
}
+2 -4
View File
@@ -189,10 +189,8 @@ fn float_from_string(val: PyObjectRef, vm: &VirtualMachine) -> PyResult<f64> {
impl PyFloat {
#[pymethod(magic)]
fn format(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
let format_spec =
FormatSpec::parse(spec.as_str()).map_err(|msg| vm.new_value_error(msg.to_owned()))?;
format_spec
.format_float(self.value)
FormatSpec::parse(spec.as_str())
.and_then(|format_spec| format_spec.format_float(self.value))
.map_err(|msg| vm.new_value_error(msg))
}
+2 -4
View File
@@ -568,10 +568,8 @@ impl PyInt {
#[pymethod(magic)]
fn format(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
let format_spec =
FormatSpec::parse(spec.as_str()).map_err(|msg| vm.new_value_error(msg.to_owned()))?;
format_spec
.format_int(&self.value)
FormatSpec::parse(spec.as_str())
.and_then(|format_spec| format_spec.format_int(&self.value))
.map_err(|msg| vm.new_value_error(msg))
}
+2 -4
View File
@@ -730,10 +730,8 @@ impl PyStr {
#[pymethod(name = "__format__")]
fn format_str(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
let format_spec =
FormatSpec::parse(spec.as_str()).map_err(|err| vm.new_value_error(err.to_string()))?;
format_spec
.format_string(self.borrow())
FormatSpec::parse(spec.as_str())
.and_then(|format_spec| format_spec.format_string(self.borrow()))
.map_err(|msg| vm.new_value_error(msg))
}