ferment_sys/builder.rs
1use crate::{Config, Crate, error, Lang};
2use crate::lang::rust::find_crates_paths;
3use crate::writer::Writer;
4
5extern crate env_logger;
6
7pub const DEFAULT_FERMENTATE_MOD: &str = "fermented";
8
9#[derive(Debug, Clone)]
10pub struct Builder {
11 config: Config,
12}
13
14impl Builder {
15 pub fn new(current_crate: Crate) -> Builder {
16 env_logger::init();
17 Builder { config: Config::new(DEFAULT_FERMENTATE_MOD, current_crate, Default::default()) }
18 }
19 #[allow(unused)]
20 pub fn with_crate_name(crate_name: &str) -> Builder {
21 Self::new(Crate::current_with_name(crate_name))
22 }
23
24 #[allow(unused)]
25 pub fn with_default_mod_name(mut self) -> Builder {
26 self.config.mod_name = String::from(DEFAULT_FERMENTATE_MOD);
27 self
28 }
29 #[allow(unused)]
30 pub fn with_cbindgen_config(mut self, config: cbindgen::Config) -> Builder {
31 self.config.cbindgen_config = config;
32 self
33 }
34 #[allow(unused)]
35 pub fn with_cbindgen_config_from_file(mut self, config: &'static str) -> Builder {
36 self.config.cbindgen_config_from_file = Some(config.to_string());
37 self
38 }
39 #[allow(unused)]
40 pub fn with_mod_name<S: AsRef<str>>(mut self, mod_name: S) -> Builder {
41 self.config.mod_name = String::from(mod_name.as_ref());
42 self
43 }
44
45 #[allow(unused)]
46 pub fn with_crates(mut self, crates: Vec<&str>) -> Builder {
47 self.config.external_crates = find_crates_paths(crates);
48 self
49 }
50 #[allow(unused)]
51 pub fn with_external_crates(mut self, crates: Vec<&str>) -> Builder {
52 self.config.external_crates = find_crates_paths(crates);
53 self
54 }
55
56 #[allow(unused)]
57 pub fn with_languages(mut self, languages: Vec<Lang>) -> Builder {
58 self.config.languages = languages;
59 self
60 }
61
62 /// Reads rust file and its nested dependencies
63 /// Creates syntax tree which we'll use later
64 /// to handle imports for FFI converted types
65 /// `mod_name`: mod with this name will be created in `src/{mod_name}.rs`
66 ///
67 /// Recursively reads a Rust project file tree and its nested dependencies to generate a syntax tree.
68 ///
69 /// This function will traverse the primary Rust file and its dependencies to generate
70 /// a syntax tree. This tree is later utilized to manage imports for types that are
71 /// converted for FFI.
72 ///
73 /// The resulting code will be written into a new module file in the `src/` directory.
74 ///
75 /// # Arguments
76 ///
77 /// * `mod_name`: The name of the module to be created. The resulting file will be
78 /// named `{mod_name}.rs` and will be located inside the `src/` directory.
79 ///
80 /// # Errors
81 ///
82 /// If the function encounters any errors while reading the file, processing the syntax,
83 /// or writing to the output file, it will return an `error::Error`.
84 ///
85 /// # Example
86 ///
87 /// ```no_run
88 /// # extern crate ferment_sys;
89 /// use ferment_sys::{Crate, Ferment, Lang};
90 /// #[cfg(feature = "objc")]
91 /// use ferment_sys::{ObjC, XCodeConfig};
92 /// #[cfg(feature = "java")]
93 /// use ferment_sys::Java;
94 /// let mut languages = vec![];
95 /// #[cfg(feature = "objc")]
96 /// languages.push(Lang::ObjC(ObjC::new(XCodeConfig::new("DS", "Fermented", "Fermented"))));
97 /// #[cfg(feature = "java")]
98 /// languages.push(Lang::Java(Java::new("Fermented")));
99 /// Ferment::with_crate_name("your_crate_name")
100 /// .with_default_mod_name()
101 /// .with_crates(vec![])
102 /// .with_languages(languages)
103 /// .generate()
104 /// .expect("Fermentation fault");
105 /// ```
106 ///
107 /// # Remarks
108 ///
109 /// This function expects the primary Rust file to be named `lib.rs` and located inside
110 /// the `src/` directory. Any deviation from this naming and structure might lead to errors.
111 ///
112 /// The resulting module will only contain the necessary imports and types suitable for FFI kind.
113 ///
114 pub fn generate(self) -> Result<(), error::Error> {
115 Writer::from(self.config)
116 .write_all()
117 }
118}