From 32cf0185bdb7fc158da6a44009b8a26fced0ad06 Mon Sep 17 00:00:00 2001 From: yt2b Date: Fri, 6 Jan 2023 22:56:30 +0900 Subject: [PATCH] Fix functions --- common/src/format.rs | 6 +++--- vm/src/builtins/float.rs | 6 ++---- vm/src/builtins/int.rs | 6 ++---- vm/src/builtins/str.rs | 6 ++---- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/common/src/format.rs b/common/src/format.rs index 901566741..ca9df8939 100644 --- a/common/src/format.rs +++ b/common/src/format.rs @@ -271,7 +271,7 @@ fn parse_precision(text: &str) -> Result<(Option, &str), &'static str> { } impl FormatSpec { - pub fn parse(text: &str) -> Result { + pub fn parse(text: &str) -> Result { // 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 { FormatSpec::parse(s) } diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index 54b257908..91f0221ce 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -189,10 +189,8 @@ fn float_from_string(val: PyObjectRef, vm: &VirtualMachine) -> PyResult { impl PyFloat { #[pymethod(magic)] fn format(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult { - 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)) } diff --git a/vm/src/builtins/int.rs b/vm/src/builtins/int.rs index 98108ca1f..224f8c5b7 100644 --- a/vm/src/builtins/int.rs +++ b/vm/src/builtins/int.rs @@ -568,10 +568,8 @@ impl PyInt { #[pymethod(magic)] fn format(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult { - 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)) } diff --git a/vm/src/builtins/str.rs b/vm/src/builtins/str.rs index 83e2581d5..b57d9590f 100644 --- a/vm/src/builtins/str.rs +++ b/vm/src/builtins/str.rs @@ -730,10 +730,8 @@ impl PyStr { #[pymethod(name = "__format__")] fn format_str(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult { - 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)) }