Skip to content
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

Support deriving Parser on struct with lifetime and automatically parse Cow<'_, T> into Cow::Owned #5773

Open
2 tasks done
jeertmans opened this issue Oct 10, 2024 · 0 comments
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations E-hard Call for participation: Experience needed to fix: Hard / a lot

Comments

@jeertmans
Copy link

jeertmans commented Oct 10, 2024

Please complete the following tasks

Clap Version

v4.5.19

Describe your use case

Currently, as investigated and discussed in #5772, it seems impossible to derive Parser on a struct that has fields with Cow<'_, T> where the lifetime isn't 'static.

This happens, e.g., when one wants to reuse the same code to both create CLI and also structure available in the public API of the crate. E.g., see code below (that does not compile):

use clap::Parser;
use std::borrow::Cow;

// Error type is not important
fn parse_cow_str(string: &str) -> Result<Cow<'static, str>, std::io::Error>{
    Ok(Cow::Owned(string.to_owned()))
}

#[derive(Parser)]
pub struct Cli<'source> {
    #[arg(long, value_parser = clap::builder::ValueParser::new(parse_cow_str))]
    pub text: Cow<'source, str>,
}

fn main() {
    let manual_cli = Cli {
        text: "here is some text".into()
    };

    assert!(matches!(manual_cli.text, Cow::Borrowed(_)));

    let cli = Cli::parse();

    assert!(matches!(cli.text, Cow::Owned(_)));
}

Describe the solution you'd like

When parsing from args, Clap should be capable to choose the Cow::Owned variant, and not care about the lifetime duration of the fields.

Moreover, it would be nice that Clap provides a default parser for Cow<'_, T> if <T as ToOwned>::Owned is a type that could be parsed automatically be Clap.

Alternatives, if applicable

The only alternative I think of is currently to create a new structure, e.g., CliOwned, derive Parser on it, and implement From<CliOwned> for Cli, but this possibly means a lot of duplicate code, especially documentation strings that I prefer to reuse to avoid errors when copy/pasting.

Additional Context

No response

@jeertmans jeertmans added the C-enhancement Category: Raise on the bar on expectations label Oct 10, 2024
@epage epage added E-hard Call for participation: Experience needed to fix: Hard / a lot A-derive Area: #[derive]` macro API labels Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations E-hard Call for participation: Experience needed to fix: Hard / a lot
Projects
None yet
Development

No branches or pull requests

2 participants