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 .any(|c| c.ident().eq(ident))
33 }
34
35 pub(crate) fn is_current_crate(&self, crate_name: &Ident) -> bool {
36 self.current_crate.ident().eq(crate_name)
37 }
38
39 #[allow(unused)]
40 pub fn new_cbindgen_config(&self) -> cbindgen::Config {
41 let Self { external_crates, current_crate: Crate { name, .. }, .. } = self;
42 let mut crates = vec!["ferment".to_string()];
43 crates.extend(external_crates.iter().map(|c| c.name.clone()));
44 cbindgen::Config {
45 language: cbindgen::Language::C,
46 cpp_compat: true,
47 parse: cbindgen::ParseConfig {
48 parse_deps: true,
49 include: Some(crates.clone()),
50 extra_bindings: crates.clone(),
51 expand: cbindgen::ParseExpandConfig { crates, ..Default::default() },
52 ..Default::default()
53 },
54 enumeration: cbindgen::EnumConfig {
55 prefix_with_name: true,
56 ..Default::default()
57 },
58 braces: cbindgen::Braces::SameLine,
59 line_length: 80,
60 tab_width: 4,
61 documentation_style: cbindgen::DocumentationStyle::C,
62 include_guard: Some(format!("{name}_h")),
63 ..Default::default()
64 }
65 }
66}
67