Skip to content

Commit 32cf018

Browse files
committed
Fix functions
1 parent ba427c8 commit 32cf018

File tree

4 files changed

+9
-15
lines changed

4 files changed

+9
-15
lines changed

common/src/format.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn parse_precision(text: &str) -> Result<(Option<usize>, &str), &'static str> {
271271
}
272272

273273
impl FormatSpec {
274-
pub fn parse(text: &str) -> Result<Self, &'static str> {
274+
pub fn parse(text: &str) -> Result<Self, String> {
275275
// get_integer in CPython
276276
let (preconversor, text) = FormatPreconversor::parse(text);
277277
let (mut fill, mut align, text) = parse_fill_and_align(text);
@@ -283,7 +283,7 @@ impl FormatSpec {
283283
let (precision, text) = parse_precision(text)?;
284284
let (format_type, text) = FormatType::parse(text);
285285
if !text.is_empty() {
286-
return Err("Invalid format specifier");
286+
return Err("Invalid format specifier".to_owned());
287287
}
288288

289289
if zero && fill.is_none() {
@@ -685,7 +685,7 @@ pub enum FormatParseError {
685685
}
686686

687687
impl FromStr for FormatSpec {
688-
type Err = &'static str;
688+
type Err = String;
689689
fn from_str(s: &str) -> Result<Self, Self::Err> {
690690
FormatSpec::parse(s)
691691
}

vm/src/builtins/float.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,8 @@ fn float_from_string(val: PyObjectRef, vm: &VirtualMachine) -> PyResult<f64> {
189189
impl PyFloat {
190190
#[pymethod(magic)]
191191
fn format(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
192-
let format_spec =
193-
FormatSpec::parse(spec.as_str()).map_err(|msg| vm.new_value_error(msg.to_owned()))?;
194-
format_spec
195-
.format_float(self.value)
192+
FormatSpec::parse(spec.as_str())
193+
.and_then(|format_spec| format_spec.format_float(self.value))
196194
.map_err(|msg| vm.new_value_error(msg))
197195
}
198196

vm/src/builtins/int.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,8 @@ impl PyInt {
568568

569569
#[pymethod(magic)]
570570
fn format(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
571-
let format_spec =
572-
FormatSpec::parse(spec.as_str()).map_err(|msg| vm.new_value_error(msg.to_owned()))?;
573-
format_spec
574-
.format_int(&self.value)
571+
FormatSpec::parse(spec.as_str())
572+
.and_then(|format_spec| format_spec.format_int(&self.value))
575573
.map_err(|msg| vm.new_value_error(msg))
576574
}
577575

vm/src/builtins/str.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -730,10 +730,8 @@ impl PyStr {
730730

731731
#[pymethod(name = "__format__")]
732732
fn format_str(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
733-
let format_spec =
734-
FormatSpec::parse(spec.as_str()).map_err(|err| vm.new_value_error(err.to_string()))?;
735-
format_spec
736-
.format_string(self.borrow())
733+
FormatSpec::parse(spec.as_str())
734+
.and_then(|format_spec| format_spec.format_string(self.borrow()))
737735
.map_err(|msg| vm.new_value_error(msg))
738736
}
739737

0 commit comments

Comments
 (0)