Skip to content

Commit 1c42792

Browse files
committed
Impl FromStr for DeprecationStrategy
Removes duplicated code.
1 parent e910fa4 commit 1c42792

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

graphql_client_cli/src/generate.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use failure;
2-
use graphql_client_codegen::{
3-
deprecation, generate_module_token_stream, GraphQLClientCodegenOptions,
4-
};
2+
use graphql_client_codegen::{generate_module_token_stream, GraphQLClientCodegenOptions};
53
use std::fs::File;
6-
use std::io::Write as IoWrite;
4+
use std::io::Write as _;
75
use std::path::PathBuf;
86
use syn;
97

@@ -19,12 +17,10 @@ pub(crate) struct CliCodegenParams {
1917
}
2018

2119
pub(crate) fn generate_code(params: CliCodegenParams) -> Result<(), failure::Error> {
22-
let deprecation_strategy = match params.deprecation_strategy.as_ref().map(|s| s.as_str()) {
23-
Some("allow") => Some(deprecation::DeprecationStrategy::Allow),
24-
Some("deny") => Some(deprecation::DeprecationStrategy::Deny),
25-
Some("warn") => Some(deprecation::DeprecationStrategy::Warn),
26-
_ => None,
27-
};
20+
let deprecation_strategy = params
21+
.deprecation_strategy
22+
.as_ref()
23+
.and_then(|s| s.parse().ok());
2824

2925
let mut options = GraphQLClientCodegenOptions::new_default();
3026

graphql_client_codegen/src/deprecation.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,16 @@ impl Default for DeprecationStrategy {
2323
DeprecationStrategy::Warn
2424
}
2525
}
26+
27+
impl std::str::FromStr for DeprecationStrategy {
28+
type Err = ();
29+
30+
fn from_str(s: &str) -> Result<Self, ()> {
31+
match s.trim() {
32+
"allow" => Ok(DeprecationStrategy::Allow),
33+
"deny" => Ok(DeprecationStrategy::Deny),
34+
"warn" => Ok(DeprecationStrategy::Warn),
35+
_ => Err(()),
36+
}
37+
}
38+
}

graphql_query_derive/src/attributes.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ pub fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result<String, failur
4040
pub fn extract_deprecation_strategy(
4141
ast: &syn::DeriveInput,
4242
) -> Result<DeprecationStrategy, failure::Error> {
43-
match extract_attr(&ast, "deprecated")?.to_lowercase().as_str() {
44-
"allow" => Ok(DeprecationStrategy::Allow),
45-
"deny" => Ok(DeprecationStrategy::Deny),
46-
"warn" => Ok(DeprecationStrategy::Warn),
47-
_ => Err(format_err!("{}", DEPRECATION_ERROR))?,
48-
}
43+
extract_attr(&ast, "deprecated")?
44+
.to_lowercase()
45+
.as_str()
46+
.parse()
47+
.map_err(|_| format_err!("{}", DEPRECATION_ERROR))
4948
}
5049

5150
#[cfg(test)]

0 commit comments

Comments
 (0)