[derive-backend] Add utils::if_type_is_python

This commit is contained in:
kngwyu 2019-07-12 22:22:56 +09:00
parent 5d85ea7fdc
commit fc5cdc1031
3 changed files with 21 additions and 22 deletions

View file

@ -85,16 +85,7 @@ impl<'a> FnSpec<'a> {
}
};
let py = match ty {
syn::Type::Path(syn::TypePath { ref path, .. }) => {
if let Some(segment) = path.segments.last() {
segment.value().ident == "Python"
} else {
false
}
}
_ => false,
};
let py = crate::utils::if_type_is_python(ty);
let opt = check_arg_ty_and_optional(name, ty);
arguments.push(FnArg {
@ -209,6 +200,11 @@ impl<'a> FnSpec<'a> {
}
false
}
/// A FnSpec is valid as getter if it has no argument or has one argument of type `Python`
pub fn valid_as_getter(&self) -> bool {
false
}
}
pub fn is_ref(name: &syn::Ident, ty: &syn::Type) -> bool {

View file

@ -64,16 +64,7 @@ fn wrap_fn_argument<'a>(input: &'a syn::FnArg, name: &'a Ident) -> Option<method
_ => panic!("unsupported argument: {:?}", cap.pat),
};
let py = match cap.ty {
syn::Type::Path(ref typath) => typath
.path
.segments
.last()
.map(|seg| seg.value().ident == "Python")
.unwrap_or(false),
_ => false,
};
let py = crate::utils::if_type_is_python(&cap.ty);
let opt = method::check_arg_ty_and_optional(&name, &cap.ty);
Some(method::FnArg {
name: ident,
@ -111,7 +102,7 @@ fn extract_pyfn_attrs(
}
_ => panic!("The first parameter of pyfn must be a MetaItem"),
}
// read Python fonction name
// read Python function name
match meta[1] {
syn::NestedMeta::Literal(syn::Lit::Str(ref lits)) => {
fnname = Some(syn::Ident::new(&lits.value(), lits.span()));

View file

@ -2,12 +2,24 @@
use proc_macro2::Span;
use proc_macro2::TokenStream;
use syn;
pub fn print_err(msg: String, t: TokenStream) {
println!("Error: {} in '{}'", msg, t.to_string());
}
/// Check if the given type `ty` is `pyo3::Python`.
pub fn if_type_is_python(ty: &syn::Type) -> bool {
match ty {
syn::Type::Path(ref typath) => typath
.path
.segments
.last()
.map(|seg| seg.value().ident == "Python")
.unwrap_or(false),
_ => false,
}
}
// FIXME(althonos): not sure the docstring formatting is on par here.
pub fn get_doc(attrs: &[syn::Attribute], null_terminated: bool) -> syn::Lit {
let mut doc = Vec::new();