| 1 |
use std::path::Path; |
| 2 |
|
| 3 |
use crate::{ |
| 4 |
analysis::imports::Imports, |
| 5 |
codegen::general::{ |
| 6 |
self, cfg_condition, cfg_deprecated, doc_alias, version_condition, version_condition_string, |
| 7 |
}, |
| 8 |
env::Env, |
| 9 |
file_saver, library, |
| 10 |
}; |
| 11 |
|
| 12 |
pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) { |
| 13 |
let path = root_path.join("constants.rs"); |
| 14 |
let mut imports = Imports::new(&env.library); |
| 15 |
|
| 16 |
if env.analysis.constants.is_empty() { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
let sys_crate_name = env.main_sys_crate_name(); |
| 21 |
imports.add("glib::GStr"); |
| 22 |
imports.add("crate::ffi"); |
| 23 |
|
| 24 |
file_saver::save_to_file(path, env.config.make_backup, |w| { |
| 25 |
general::start_comments(w, &env.config)?; |
| 26 |
general::uses(w, env, &imports, None)?; |
| 27 |
writeln!(w)?; |
| 28 |
|
| 29 |
mod_rs.push("\nmod constants;".into()); |
| 30 |
|
| 31 |
for constant in &env.analysis.constants { |
| 32 |
let type_ = env.type_(constant.typ); |
| 33 |
if let library::Type::Basic(library::Basic::Utf8) = type_ { |
| 34 |
cfg_deprecated(w, env, None, constant.deprecated_version, false, 0)?; |
| 35 |
cfg_condition(w, constant.cfg_condition.as_ref(), false, 0)?; |
| 36 |
version_condition(w, env, None, constant.version, false, 0)?; |
| 37 |
doc_alias(w, &constant.glib_name, "", 0)?; |
| 38 |
writeln!( |
| 39 |
w, |
| 40 |
"pub static {name}: &GStr = unsafe{{GStr::from_utf8_with_nul_unchecked({sys_crate_name}::{c_id})}};", |
| 41 |
sys_crate_name = sys_crate_name, |
| 42 |
name = constant.name, |
| 43 |
c_id = constant.glib_name |
| 44 |
)?; |
| 45 |
if let Some(cfg) = version_condition_string(env, None, constant.version, false, 0) { |
| 46 |
mod_rs.push(cfg); |
| 47 |
} |
| 48 |
mod_rs.push(format!( |
| 49 |
"{}pub use self::constants::{};", |
| 50 |
constant |
| 51 |
.deprecated_version |
| 52 |
.map(|_| "#[allow(deprecated)]\n") |
| 53 |
.unwrap_or(""), |
| 54 |
constant.name |
| 55 |
)); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
Ok(()) |
| 60 |
}); |
| 61 |
} |