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
Issue when inferring lifetimes when used with serde. #4349
Comments
|
|
Please next time, include a full reproduction case so I don't have to work backwards to create all of the scaffolding that was elided. Below is what I"m using for this issue. It goes a step further and uses #!/usr/bin/env -S rust-script
//! ```cargo
//! [dependencies]
//! clap = { version = "4.0.8", features = ["derive", "wrap_help", "env"] }
//! serde_json = "1.0"
//! serde = { version = "1.0.137", features = ["derive"] }
//! ```
use clap::Parser;
use clap::Subcommand;
use serde::Deserialize;
use serde::Serialize;
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: QueryMsg,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Subcommand)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
GetPrice {
#[arg(value_parser = serde_json::from_str::<AssetInfo>)]
asset: AssetInfo,
},
GetPrices {
#[arg(value_parser = serde_json::from_str::<Vec<AssetInfo>>)]
assets: Vec<AssetInfo>,
},
GetAllPrices {
#[arg(value_parser = serde_json::from_str::<Option<AssetInfo>>)]
start_after: Option<AssetInfo>,
limit: Option<u32>,
},
}
#[derive(Serialize, Deserialize, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "snake_case")]
#[repr(u8)]
pub enum AssetInfo {
Token { contract_addr: Addr },
NativeToken { denom: String },
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Addr(String);
fn main() {
let args: Cli = Cli::parse();
println!("{:?}", args);
} |
Still double checking some things but the following works around the problem: #!/usr/bin/env -S rust-script
//! ```cargo
//! [dependencies]
//! clap = { version = "4.0.8", features = ["derive", "wrap_help", "env"] }
//! serde_json = "1.0"
//! serde = { version = "1.0.137", features = ["derive"] }
//! ```
use clap::Parser;
use clap::Subcommand;
use serde::Deserialize;
use serde::Serialize;
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: QueryMsg,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Subcommand)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
GetPrice {
#[arg(value_parser = asset_parser)]
asset: AssetInfo,
},
GetPrices {
#[arg(value_parser = asset_parser)]
assets: Vec<AssetInfo>,
},
GetAllPrices {
#[arg(value_parser = asset_parser)]
start_after: Option<AssetInfo>,
limit: Option<u32>,
},
}
fn asset_parser(s: &str) -> Result<AssetInfo, serde_json::Error> {
serde_json::from_str::<AssetInfo>(s)
}
#[derive(Serialize, Deserialize, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "snake_case")]
#[repr(u8)]
pub enum AssetInfo {
Token { contract_addr: Addr },
NativeToken { denom: String },
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Addr(String);
fn main() {
let args: Cli = Cli::parse();
println!("{:?}", args);
} Besides breaking out |
I think this error is key:
pub fn from_str<'a, T>(s: &'a str) -> Result<T> where
T: Deserialize<'a>, and I suspect the I am not aware of a way to loosen things so we can work with |
Thanks for your hard work! Really appreciate it. If you have any ideas, let me know! |
Please complete the following tasks
Rust Version
rustc 1.66.0-nightly (01af5040f 2022-10-04)
Clap Version
clap = { version = "4.0.8", features = ["derive"] }
Minimal reproducible code
Steps to reproduce the bug with the above code
This won't compile.
Actual Behaviour
Expected Behaviour
This is significantly above my head. Not sure what the solution is.
Additional Context
No response
Debug Output
nothing changed.
The text was updated successfully, but these errors were encountered: