taitan_orm_parser/info_parser/
lifetime_parser.rs

1use syn::{GenericParam, Generics, Lifetime, Type};
2use syn::visit::Visit;
3
4
5
6pub struct LifetimeParser;
7
8#[derive(Default)]
9struct LifetimeFinder {
10    lifetime: Option<Lifetime>,
11}
12
13impl<'ast> Visit<'ast> for LifetimeFinder {
14    fn visit_lifetime(&mut self, lifetime: &'ast syn::Lifetime) {
15        self.lifetime = Some(lifetime.clone());
16    }
17}
18impl LifetimeParser {
19    pub fn has_lifetime(ty: &Type) -> bool {
20        Self::get_lifetime(ty).is_some()
21    }
22
23    pub fn get_lifetime(ty: &Type) -> Option<Lifetime> {
24        let mut finder = LifetimeFinder::default();
25        finder.visit_type(ty);
26        finder.lifetime
27    }
28
29    pub fn get_generic_lifetimes(generics: &Generics) -> Vec<Lifetime> {
30        let mut lifetimes: Vec<Lifetime> = Vec::new();
31        for param in generics.params.iter() {
32            if let GenericParam::Lifetime(lifetime_def) = param {
33                lifetimes.push(lifetime_def.lifetime.clone());
34            }
35        }
36        lifetimes
37    }
38
39    // fn has_lifetime_in_type_path(type_path: &TypePath) -> bool {
40    //     for segment in &type_path.path.segments {
41    //         if let PathArguments::AngleBracketed(ref args) = segment.arguments {
42    //             for arg in &args.args {
43    //                 match arg {
44    //                     GenericArgument::Lifetime(_) => return true,
45    //                     GenericArgument::Type(ty) => {
46    //                         if Self::has_lifetime(ty) {
47    //                             return true;
48    //                         }
49    //                     }
50    //                     _ => {}
51    //                 }
52    //             }
53    //         }
54    //     }
55    //     false
56    // }
57    //
58    // fn extract_lifetime_in_type_path(type_path: &TypePath) -> Vec<Lifetime> {
59    //     let mut lifetimes: Vec<Lifetime> = Vec::new();
60    //     for segment in &type_path.path.segments {
61    //         if let PathArguments::AngleBracketed(ref args) = segment.arguments {
62    //             for arg in &args.args {
63    //                 match arg {
64    //                     GenericArgument::Lifetime(life_time) => lifetimes.push(life_time.clone()),
65    //                     GenericArgument::Type(ty) => {
66    //                         let life_times = Self::extract_lifetime(ty);
67    //                         lifetimes.extend(life_times);
68    //                     }
69    //                     _ => {}
70    //                 }
71    //             }
72    //         }
73    //     }
74    //     lifetimes
75    // }
76}