ferment_sys/
config.rs

1use std::fmt::{Display, Formatter};
2use std::path::PathBuf;
3use proc_macro2::Ident;
4use crate::{Crate, Lang};
5
6#[derive(Debug, Clone)]
7pub struct Config {
8    pub mod_name: String,
9    pub cbindgen_config: cbindgen::Config,
10    pub cbindgen_config_from_file: Option<String>,
11    pub current_crate: Crate,
12    pub external_crates: Vec<Crate>,
13    pub languages: Vec<Lang>,
14}
15
16impl Display for Config {
17    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
18        f.write_fmt(format_args!("[Config]\n\tcrate: {:?}\n\texternal: {:?}", self.current_crate, self.external_crates))
19    }
20}
21
22
23impl Config {
24    pub fn new(mod_name: &'static str, current_crate: Crate, cbindgen_config: cbindgen::Config) -> Self {
25        Self { mod_name: String::from(mod_name), cbindgen_config, current_crate, cbindgen_config_from_file: None, external_crates: vec![], languages: vec![] }
26    }
27    pub fn expansion_path(&self) -> PathBuf {
28        self.current_crate.root_path.join(format!("{}.rs", self.mod_name))
29    }
30    pub(crate) fn contains_fermented_crate(&self, ident: &Ident) -> bool {
31        self.external_crates.iter()
32            .find(|c| c.ident().eq(ident))
33            .is_some()
34    }
35
36    pub(crate) fn is_current_crate(&self, crate_name: &Ident) -> bool {
37        self.current_crate.ident().eq(crate_name)
38    }
39
40    #[allow(unused)]
41    pub fn new_cbindgen_config(&self) -> cbindgen::Config {
42        let Self { external_crates, current_crate: Crate { name, .. }, .. } = self;
43        let mut crates = vec!["ferment".to_string()];
44        crates.extend(external_crates.iter().map(|c| c.name.clone()));
45        cbindgen::Config {
46            language: cbindgen::Language::C,
47            cpp_compat: true,
48            parse: cbindgen::ParseConfig {
49                parse_deps: true,
50                include: Some(crates.clone()),
51                extra_bindings: crates.clone(),
52                expand: cbindgen::ParseExpandConfig { crates, ..Default::default() },
53                ..Default::default()
54            },
55            enumeration: cbindgen::EnumConfig {
56                prefix_with_name: true,
57                ..Default::default()
58            },
59            braces: cbindgen::Braces::SameLine,
60            line_length: 80,
61            tab_width: 4,
62            documentation_style: cbindgen::DocumentationStyle::C,
63            include_guard: Some(format!("{name}_h")),
64            ..Default::default()
65        }
66    }
67
68
69
70}
71
72#[cfg(feature = "objc")]
73impl Config {
74    pub fn maybe_objc_config(&self) -> Option<&crate::lang::objc::Config> {
75        self.languages.iter().find_map(|lang| match lang {
76            Lang::ObjC(config) => Some(config),
77            #[cfg(feature = "java")]
78            _ => None
79        })
80    }
81}