Skip to content

stty: detach logic for parsing u8 and processing error - #16129

Open
oech3 wants to merge 1 commit into
uutils:mainfrom
oech3:stty-parse-u8
Open

oech3 wants to merge 1 commit into
uutils:mainfrom
oech3:stty-parse-u8

Conversation

@oech3

@oech3 oech3 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

fn parse_u8_or_err is mixing logic for parsing u8 and processing the error message.
We don't need to test logic of .parse::<u8> if they are separated.

@xtqqczze

xtqqczze commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

What is purpose of this refactoring; is it intended to support further changes?

@oech3

oech3 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

No. This PR just drops tests we don't need to verify.

@xtqqczze

xtqqczze commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

In that case would you drop the refactoring and just remove the test.

@oech3

oech3 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

No. If fn parse_u8_or_err contains parsing, we need to test parsing logic again.

@xtqqczze

xtqqczze commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

No. If fn parse_u8_or_err contains parsing, we need to test parsing logic again.

In that case, I don't think this change is justified. The refactoring makes the code more difficult to understand without providing any meaningful benefit.

@oech3 oech3 closed this Nov 2, 2026
@oech3
oech3 deleted the stty-parse-u8 branch November 2, 2026 13:22
Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
@cakebaker

Copy link
Copy Markdown
Contributor

An alternative idea is to modify the proposed helper function a bit to also create the error, something like:

fn create_err(e: &std::num::ParseIntError, arg: &str) -> Box<dyn UError> {
    let msg = if *e.kind() == IntErrorKind::PosOverflow {
        translate!("stty-error-invalid-integer-argument-value-too-large", "value" => format!("'{arg}'"))
    } else {
        translate!("stty-error-invalid-integer-argument", "value" => format!("'{arg}'"))
    };
    USimpleError::new(1, msg)
}

And then use it like:

let n = line.parse::<u8>().map_err(|e| create_err(&e, line))?;

@oech3

oech3 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Including USimpleError is fine. But we still repeat line variable.

@oech3
oech3 restored the stty-parse-u8 branch November 2, 2026 14:23
@oech3 oech3 reopened this Nov 2, 2026
@oech3
oech3 force-pushed the stty-parse-u8 branch 2 times, most recently from 6565ce3 to cc471b2 Compare November 2, 2026 14:49
@xtqqczze

xtqqczze commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

I don't think this refactoring solely to remove a test is worthwhile. Could we keep the parsing logic returning a Result instead, something like:

fn try_parse_u8(arg: &str) -> Result<u8, T>

@oech3

oech3 commented Nov 2, 2026 via email

Copy link
Copy Markdown
Contributor Author

@cakebaker

Copy link
Copy Markdown
Contributor

I think the reduction of tests is "only" a side effect of the refactoring. For me it is about readability. The first thought when I see a call of parse_u8_or_err is: why not use parse::<u8>() from the std?

@oech3

oech3 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

An another thought about this: we accepts different stderr with GNU. Then someone can try to patch GnuTests to accept raw parse::<u8>() at different PR based on this.

@xtqqczze

xtqqczze commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

The first thought when I see a call of parse_u8_or_err is: why not use parse::<u8>() from the std?

Right, the existing function is badly named, it could be something like: try_parse_integer_argument.

Comment thread src/uu/stty/src/stty.rs
#[test]
fn test_parse_u8_or_err_overflow() {
// Test that overflow values return an error
fn test_create_parse_err_overflow() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests/by-util/test_stty.rs already checks both messages for line/min/time, do we still need these unit tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine to drop. But some tests are platform specific at there.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants