8000 internal: quote in CommandBuilder by Young-Flash · Pull Request #807 · moonbitlang/moon · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

internal: quote in CommandBuilder #807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2025
Merged

internal: quote in CommandBuilder #807

merged 1 commit into from
May 12, 2025

Conversation

Young-Flash
Copy link
Collaborator

No description provided.

Copy link
peter-jerry-ye-code-review bot commented May 12, 2025
Potential failure handling for shlex::try_quote

Category
Correctness
Code Snippet
shlex::try_quote(command).unwrap()
Recommendation
Handle the potential error case explicitly instead of using unwrap():

let command = if command.contains(|c: char| c.is_whitespace()) {
    shlex::try_quote(command).map_err(|e| format!("Failed to quote command: {}", e))?
} else {
    command.into()
};```
**Reasoning**
Using unwrap() could cause a panic if try_quote fails. While this is unlikely, proper error handling would make the code more robust and provide better error messages.

</details>
<details>

<summary> Missing documentation for whitespace handling logic </summary>

**Category**
Maintainability
**Code Snippet**
// don't always quote the `command` since moon in windows will be quoted into 'moon'
**Recommendation**
Add more detailed documentation explaining the Windows-specific behavior:
```rust
/// Creates a new CommandBuilder with proper command quoting.
/// 
/// On Windows, commands containing whitespace need special handling to prevent
/// double-quoting issues. Simple commands (without whitespace) are left unquoted
/// to avoid problems with commands like 'moon' being incorrectly quoted.```
**Reasoning**
The current comment is terse and doesn't fully explain the rationale behind the conditional quoting behavior. Better documentation would help future maintainers understand the Windows-specific considerations.

</details>
<details>

<summary> Consider extracting whitespace check into a named function </summary>

**Category**
Maintainability
**Code Snippet**
command.contains(|c: char| c.is_whitespace())
**Recommendation**
Extract the check into a descriptive function:
```rust
fn needs_quoting(s: &str) -> bool {
    s.contains(|c: char| c.is_whitespace())
}

let command = if needs_quoting(command) {
    shlex::try_quote(command).unwrap()
} else {
    command.into()
};```
**Reasoning**
The whitespace check could be reused elsewhere and its purpose would be clearer with a descriptive function name. This would also make the code more maintainable if the quoting rules need to change in the future.

</details>

@Young-Flash Young-Flash merged commit b27a098 into main May 12, 2025
10 of 13 checks passed
@Young-Flash Young-Flash deleted the quote_exe branch May 12, 2025 06:14
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.

1 participant
0