libgir/
env.rs

1use std::cell::RefCell;
2
3use crate::{
4    analysis::{self, namespaces::NsId},
5    config::{gobjects::GStatus, Config},
6    library::*,
7    nameutil::use_glib_type,
8    version::Version,
9};
10
11#[derive(Debug)]
12pub struct Env {
13    pub library: Library,
14    pub config: Config,
15    pub namespaces: analysis::namespaces::Info,
16    pub symbols: RefCell<analysis::symbols::Info>,
17    pub class_hierarchy: analysis::class_hierarchy::Info,
18    pub analysis: analysis::Analysis,
19}
20
21impl Env {
22    #[inline]
23    pub fn type_(&self, tid: TypeId) -> &Type {
24        self.library.type_(tid)
25    }
26    pub fn type_status(&self, name: &str) -> GStatus {
27        self.config
28            .objects
29            .get(name)
30            .map(|o| o.status)
31            .unwrap_or_default()
32    }
33    pub fn type_status_sys(&self, name: &str) -> GStatus {
34        self.config
35            .objects
36            .get(name)
37            .map_or(GStatus::Generate, |o| o.status)
38    }
39
40    pub fn is_totally_deprecated(
41        &self,
42        ns_id: Option<NsId>,
43        deprecated_version: Option<Version>,
44    ) -> bool {
45        let to_compare_with = self.config.min_required_version(self, ns_id);
46        match (deprecated_version, to_compare_with) {
47            (Some(v), Some(to_compare_v)) => {
48                if v <= to_compare_v {
49                    self.config.deprecate_by_min_version
50                } else {
51                    false
52                }
53            }
54            (Some(v), _) => {
55                if v <= self.config.min_cfg_version {
56                    self.config.deprecate_by_min_version
57                } else {
58                    false
59                }
60            }
61            _ => false,
62        }
63    }
64
65    pub fn is_too_low_version(&self, ns_id: Option<NsId>, version: Option<Version>) -> bool {
66        let to_compare_with = self.config.min_required_version(self, ns_id);
67        if let (Some(v), Some(to_compare_v)) = (version, to_compare_with) {
68            return v <= to_compare_v;
69        }
70        false
71    }
72
73    pub fn main_sys_crate_name(&self) -> &str {
74        &self.namespaces[MAIN_NAMESPACE].sys_crate_name
75    }
76
77    /// Helper to get the ffi crate import
78    pub fn sys_crate_import(&self, type_id: TypeId) -> String {
79        let crate_name = &self.namespaces[type_id.ns_id].sys_crate_name;
80        if crate_name == "gobject_ffi" {
81            use_glib_type(self, crate_name)
82        } else {
83            crate_name.clone()
84        }
85    }
86}