1use proc_macro2::TokenStream;
2use proc_macro_crate::{crate_name, FoundCrate};
3use quote::{format_ident, quote};
4use syn::{Attribute, FnArg, Ident, Pat, PatIdent, PatType};
5
6pub fn zbus_path() -> TokenStream {
7 if let Ok(FoundCrate::Name(name: String)) = crate_name(orig_name:"zbus") {
8 let ident: Ident = format_ident!("{}", name);
9 quote! { ::#ident }
10 } else {
11 quote! { ::zbus }
12 }
13}
14
15pub fn typed_arg(arg: &FnArg) -> Option<&PatType> {
16 match arg {
17 FnArg::Typed(t: &PatType) => Some(t),
18 _ => None,
19 }
20}
21
22pub fn pat_ident(pat: &PatType) -> Option<&Ident> {
23 match &*pat.pat {
24 Pat::Ident(PatIdent { ident: &Ident, .. }) => Some(ident),
25 _ => None,
26 }
27}
28
29pub fn get_doc_attrs(attrs: &[Attribute]) -> Vec<&Attribute> {
30 attrs.iter().filter(|x: &&Attribute| x.path.is_ident("doc")).collect()
31}
32
33// Convert to pascal case, assuming snake case.
34// If `s` is already in pascal case, should yield the same result.
35pub fn pascal_case(s: &str) -> String {
36 let mut pascal: String = String::new();
37 let mut capitalize: bool = true;
38 for ch: char in s.chars() {
39 if ch == '_' {
40 capitalize = true;
41 } else if capitalize {
42 pascal.push(ch:ch.to_ascii_uppercase());
43 capitalize = false;
44 } else {
45 pascal.push(ch);
46 }
47 }
48 pascal
49}
50
51pub fn is_blank(s: &str) -> bool {
52 s.trim().is_empty()
53}
54