string: remove StringError::NotANumber

Use the more generic `StringError::InvalidArgs` instead

Closes #12556
This commit is contained in:
Nahor
2026-04-07 11:20:49 -07:00
committed by Johannes Altmanninger
parent 30e6aa85e2
commit d649c2aab4
6 changed files with 18 additions and 31 deletions

View File

@@ -1,4 +1,4 @@
use crate::{builtins::error::Error, err_fmt, err_raw, err_str, screen::escape_code_length};
use crate::{err_fmt, err_raw, err_str, screen::escape_code_length};
use fish_wcstringutil::fish_wcwidth_visible;
// Forward some imports to make subcmd implementations easier
use super::prelude::*;
@@ -69,7 +69,7 @@ fn parse_opts(
c => {
let retval = self.parse_opt(c, w.woptarg);
if let Err(e) = retval {
e.print_error(&args_read, streams, w.woptarg, w.wopt_index);
e.print_error(&args_read, streams, w.wopt_index);
return Err(STATUS_INVALID_ARGS);
}
}
@@ -79,6 +79,11 @@ fn parse_opts(
Ok(w.wopt_index)
}
fn parse_arg_number<'a, 'b>(arg: &'a wstr) -> Result<i64, StringError<'b>> {
let n = fish_wcstol(arg).map_err(|_| err_fmt!(Error::NOT_NUMBER, arg))?;
Ok(n)
}
/// Take any positional arguments after options have been parsed.
#[allow(unused_variables)]
fn take_args(
@@ -143,7 +148,6 @@ fn run_impl(
/// This covers failing argument/option parsing
enum StringError<'a> {
InvalidArgs(Error<'a>),
NotANumber,
UnknownOption,
}
@@ -190,26 +194,14 @@ fn print_error(&self, args: &[&wstr], streams: &mut IoStreams) {
}
}
impl<'a> From<crate::wutil::wcstoi::Error> for StringError<'a> {
fn from(_: crate::wutil::wcstoi::Error) -> Self {
StringError::NotANumber
}
}
impl<'a> From<crate::builtins::error::Error<'a>> for StringError<'a> {
fn from(error: crate::builtins::error::Error<'a>) -> Self {
impl<'a> From<error::Error<'a>> for StringError<'a> {
fn from(error: error::Error<'a>) -> Self {
StringError::InvalidArgs(error)
}
}
impl<'a> StringError<'a> {
fn print_error(
self,
args: &[&wstr],
streams: &mut IoStreams,
optarg: Option<&wstr>,
optind: usize,
) {
fn print_error(self, args: &[&wstr], streams: &mut IoStreams, optind: usize) {
let cmd = L!("string");
let subcmd = args[0];
use StringError::*;
@@ -217,11 +209,6 @@ fn print_error(
InvalidArgs(err) => {
err.subcmd(cmd, subcmd).finish(streams);
}
NotANumber => {
err_fmt!(Error::NOT_NUMBER, optarg.unwrap())
.subcmd(cmd, subcmd)
.finish(streams);
}
UnknownOption => {
// This would mean the subcmd's XXX_OPTIONS does not match
// the list in its `parse_opt()` implementation

View File

@@ -52,7 +52,7 @@ fn parse_opt(&mut self, c: char, arg: Option<&wstr>) -> Result<(), StringError<'
'r' => self.pad_from = Direction::Right,
'w' => {
let arg = arg.unwrap();
self.width = fish_wcstol(arg)?
self.width = Self::parse_arg_number(arg)?
.try_into()
.map_err(|_| err_fmt!("Invalid width value '%s'", arg))?;
}

View File

@@ -22,7 +22,7 @@ fn parse_opt(&mut self, c: char, arg: Option<&wstr>) -> Result<(), StringError<'
'n' => {
let arg = arg.unwrap();
self.count = Some(
fish_wcstol(arg)?
Self::parse_arg_number(arg)?
.try_into()
.map_err(|_| err_fmt!("Invalid count value '%s'", arg))?,
);
@@ -30,7 +30,7 @@ fn parse_opt(&mut self, c: char, arg: Option<&wstr>) -> Result<(), StringError<'
'm' => {
let arg = arg.unwrap();
self.max = Some(
fish_wcstol(arg)?
Self::parse_arg_number(arg)?
.try_into()
.map_err(|_| err_fmt!(Error::INVALID_MAX_VALUE, arg))?,
);

View File

@@ -46,7 +46,7 @@ fn parse_opt(&mut self, c: char, arg: Option<&'args wstr>) -> Result<(), StringE
'm' => {
let arg = arg.unwrap();
self.max = Some(
fish_wcstol(arg)?
Self::parse_arg_number(arg)?
.try_into()
.map_err(|_| err_fmt!(Error::INVALID_MAX_VALUE, arg))?,
);

View File

@@ -118,7 +118,7 @@ fn parse_opt(&mut self, c: char, arg: Option<&wstr>) -> Result<(), StringError<'
'r' => self.split_from = Direction::Right,
'm' => {
let arg = arg.unwrap();
self.max = fish_wcstol(arg)?
self.max = Self::parse_arg_number(arg)?
.try_into()
.map_err(|_| err_fmt!(Error::INVALID_MAX_VALUE, arg))?;
}

View File

@@ -24,7 +24,7 @@ fn parse_opt(&mut self, c: char, arg: Option<&wstr>) -> Result<(), StringError<'
'l' => {
let arg = arg.unwrap();
self.length = Some(
fish_wcstol(arg)?
Self::parse_arg_number(arg)?
.try_into()
.map_err(|_| err_fmt!("Invalid length value '%s'", arg))?,
);
@@ -32,7 +32,7 @@ fn parse_opt(&mut self, c: char, arg: Option<&wstr>) -> Result<(), StringError<'
's' => {
let arg = arg.unwrap();
self.start = Some(
fish_wcstol(arg)?
Self::parse_arg_number(arg)?
.try_into()
.map_err(|_| err_fmt!("Invalid start value '%s'", arg))?,
);
@@ -40,7 +40,7 @@ fn parse_opt(&mut self, c: char, arg: Option<&wstr>) -> Result<(), StringError<'
'e' => {
let arg = arg.unwrap();
self.end = Some(
fish_wcstol(arg)?
Self::parse_arg_number(arg)?
.try_into()
.map_err(|_| err_fmt!("Invalid end value '%s'", arg))?,
);