-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6271ee7
commit d1a50bf
Showing
4 changed files
with
188 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
//! Special types handling | ||
|
||
use crate::spanned::Sp; | ||
|
||
use syn::{ | ||
spanned::Spanned, GenericArgument, Path, PathArguments, PathArguments::AngleBracketed, | ||
PathSegment, Type, TypePath, | ||
}; | ||
|
||
#[derive(Copy, Clone, PartialEq, Debug)] | ||
pub enum Ty { | ||
Bool, | ||
Vec, | ||
Option, | ||
OptionOption, | ||
OptionVec, | ||
Other, | ||
} | ||
|
||
impl Ty { | ||
pub fn from_syn_ty(ty: &syn::Type) -> Sp<Self> { | ||
use Ty::*; | ||
let t = |kind| Sp::new(kind, ty.span()); | ||
|
||
if is_simple_ty(ty, "bool") { | ||
t(Bool) | ||
} else if is_generic_ty(ty, "Vec") { | ||
t(Vec) | ||
} else if let Some(subty) = subty_if_name(ty, "Option") { | ||
if is_generic_ty(subty, "Option") { | ||
t(OptionOption) | ||
} else if is_generic_ty(subty, "Vec") { | ||
t(OptionVec) | ||
} else { | ||
t(Option) | ||
} | ||
} else { | ||
t(Other) | ||
} | ||
} | ||
} | ||
|
||
pub fn sub_type(ty: &syn::Type) -> Option<&syn::Type> { | ||
subty_if(ty, |_| true) | ||
} | ||
|
||
fn only_last_segment(ty: &syn::Type) -> Option<&PathSegment> { | ||
match ty { | ||
Type::Path(TypePath { | ||
qself: None, | ||
path: | ||
Path { | ||
leading_colon: None, | ||
segments, | ||
}, | ||
}) => only_one(segments.iter()), | ||
|
||
_ => None, | ||
} | ||
} | ||
|
||
fn subty_if<F>(ty: &syn::Type, f: F) -> Option<&syn::Type> | ||
where | ||
F: FnOnce(&PathSegment) -> bool, | ||
{ | ||
only_last_segment(ty) | ||
.filter(|segment| f(segment)) | ||
.and_then(|segment| { | ||
if let AngleBracketed(args) = &segment.arguments { | ||
only_one(args.args.iter()).and_then(|genneric| { | ||
if let GenericArgument::Type(ty) = genneric { | ||
Some(ty) | ||
} else { | ||
None | ||
} | ||
}) | ||
} else { | ||
None | ||
} | ||
}) | ||
} | ||
|
||
fn subty_if_name<'a>(ty: &'a syn::Type, name: &str) -> Option<&'a syn::Type> { | ||
subty_if(ty, |seg| seg.ident == name) | ||
} | ||
|
||
fn is_simple_ty(ty: &syn::Type, name: &str) -> bool { | ||
only_last_segment(ty) | ||
.map(|segment| { | ||
if let PathArguments::None = segment.arguments { | ||
segment.ident == name | ||
} else { | ||
false | ||
} | ||
}) | ||
.unwrap_or(false) | ||
} | ||
|
||
fn is_generic_ty(ty: &syn::Type, name: &str) -> bool { | ||
subty_if_name(ty, name).is_some() | ||
} | ||
|
||
fn only_one<I, T>(mut iter: I) -> Option<T> | ||
where | ||
I: Iterator<Item = T>, | ||
{ | ||
iter.next().filter(|_| iter.next().is_none()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//! Checks that types like `::std::option::Option` are not special | ||
|
||
use structopt::StructOpt; | ||
|
||
#[rustversion::since(1.37)] | ||
#[test] | ||
fn special_types_bool() { | ||
mod inner { | ||
#[allow(non_camel_case_types)] | ||
#[derive(PartialEq, Debug)] | ||
pub struct bool(pub String); | ||
|
||
impl std::str::FromStr for self::bool { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Ok(self::bool(s.into())) | ||
} | ||
} | ||
}; | ||
|
||
#[derive(StructOpt, PartialEq, Debug)] | ||
struct Opt { | ||
arg: inner::bool, | ||
} | ||
|
||
assert_eq!( | ||
Opt { | ||
arg: inner::bool("success".into()) | ||
}, | ||
Opt::from_iter(&["test", "success"]) | ||
); | ||
} | ||
|
||
#[test] | ||
fn special_types_option() { | ||
fn parser(s: &str) -> Option<String> { | ||
Some(s.to_string()) | ||
} | ||
|
||
#[derive(StructOpt, PartialEq, Debug)] | ||
struct Opt { | ||
#[structopt(parse(from_str = parser))] | ||
arg: ::std::option::Option<String>, | ||
} | ||
|
||
assert_eq!( | ||
Opt { | ||
arg: Some("success".into()) | ||
}, | ||
Opt::from_iter(&["test", "success"]) | ||
); | ||
} | ||
|
||
#[test] | ||
fn special_types_vec() { | ||
fn parser(s: &str) -> Vec<String> { | ||
vec![s.to_string()] | ||
} | ||
|
||
#[derive(StructOpt, PartialEq, Debug)] | ||
struct Opt { | ||
#[structopt(parse(from_str = parser))] | ||
arg: ::std::vec::Vec<String>, | ||
} | ||
|
||
assert_eq!( | ||
Opt { | ||
arg: vec!["success".into()] | ||
}, | ||
Opt::from_iter(&["test", "success"]) | ||
); | ||
} |