aconfig: allow dots in package fields
Allow package fields to include dots. Update the generated code based on the package name: if the package name is com.android.example: - java: package com.android.example; ... - C++: namespace com::android::example { ... } - Rust: mod com { mod android { mod example { ... } } } Also, update examples to use dots in the package fields. Also, remove unnecessary #include from the auto-generated C++ code: the header should not include itself. Bug: 285000854 Test: atest aconfig.test Change-Id: I8a5352e25c64c34dee0725202a1b7c9957819de8
This commit is contained in:
@@ -24,9 +24,12 @@ use crate::commands::OutputFile;
|
||||
|
||||
pub fn generate_rust_code(cache: &Cache) -> Result<OutputFile> {
|
||||
let package = cache.package();
|
||||
let parsed_flags: Vec<TemplateParsedFlag> =
|
||||
cache.iter().map(|item| create_template_parsed_flag(package, item)).collect();
|
||||
let context = TemplateContext { package: package.to_string(), parsed_flags };
|
||||
let parsed_flags: Vec<TemplateParsedFlag> = cache.iter().map(|item| item.into()).collect();
|
||||
let context = TemplateContext {
|
||||
package: package.to_string(),
|
||||
parsed_flags,
|
||||
modules: package.split('.').map(|s| s.to_string()).collect::<Vec<_>>(),
|
||||
};
|
||||
let mut template = TinyTemplate::new();
|
||||
template.add_template("rust_code_gen", include_str!("../templates/rust.template"))?;
|
||||
let contents = template.render("rust_code_gen", &context)?;
|
||||
@@ -38,12 +41,12 @@ pub fn generate_rust_code(cache: &Cache) -> Result<OutputFile> {
|
||||
struct TemplateContext {
|
||||
pub package: String,
|
||||
pub parsed_flags: Vec<TemplateParsedFlag>,
|
||||
pub modules: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct TemplateParsedFlag {
|
||||
pub name: String,
|
||||
pub fn_name: String,
|
||||
|
||||
// TinyTemplate's conditionals are limited to single <bool> expressions; list all options here
|
||||
// Invariant: exactly one of these fields will be true
|
||||
@@ -52,28 +55,29 @@ struct TemplateParsedFlag {
|
||||
pub is_read_write: bool,
|
||||
}
|
||||
|
||||
#[allow(clippy::nonminimal_bool)]
|
||||
fn create_template_parsed_flag(package: &str, item: &Item) -> TemplateParsedFlag {
|
||||
let template = TemplateParsedFlag {
|
||||
name: item.name.clone(),
|
||||
fn_name: format!("{}_{}", package, &item.name),
|
||||
is_read_only_enabled: item.permission == Permission::ReadOnly
|
||||
&& item.state == FlagState::Enabled,
|
||||
is_read_only_disabled: item.permission == Permission::ReadOnly
|
||||
&& item.state == FlagState::Disabled,
|
||||
is_read_write: item.permission == Permission::ReadWrite,
|
||||
};
|
||||
#[rustfmt::skip]
|
||||
debug_assert!(
|
||||
(template.is_read_only_enabled && !template.is_read_only_disabled && !template.is_read_write) ||
|
||||
(!template.is_read_only_enabled && template.is_read_only_disabled && !template.is_read_write) ||
|
||||
(!template.is_read_only_enabled && !template.is_read_only_disabled && template.is_read_write),
|
||||
"TemplateParsedFlag invariant failed: {} {} {}",
|
||||
template.is_read_only_enabled,
|
||||
template.is_read_only_disabled,
|
||||
template.is_read_write,
|
||||
);
|
||||
template
|
||||
impl From<&Item> for TemplateParsedFlag {
|
||||
#[allow(clippy::nonminimal_bool)]
|
||||
fn from(item: &Item) -> Self {
|
||||
let template = TemplateParsedFlag {
|
||||
name: item.name.clone(),
|
||||
is_read_only_enabled: item.permission == Permission::ReadOnly
|
||||
&& item.state == FlagState::Enabled,
|
||||
is_read_only_disabled: item.permission == Permission::ReadOnly
|
||||
&& item.state == FlagState::Disabled,
|
||||
is_read_write: item.permission == Permission::ReadWrite,
|
||||
};
|
||||
#[rustfmt::skip]
|
||||
debug_assert!(
|
||||
(template.is_read_only_enabled && !template.is_read_only_disabled && !template.is_read_write) ||
|
||||
(!template.is_read_only_enabled && template.is_read_only_disabled && !template.is_read_write) ||
|
||||
(!template.is_read_only_enabled && !template.is_read_only_disabled && template.is_read_write),
|
||||
"TemplateParsedFlag invariant failed: {} {} {}",
|
||||
template.is_read_only_enabled,
|
||||
template.is_read_only_disabled,
|
||||
template.is_read_write,
|
||||
);
|
||||
template
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -86,24 +90,33 @@ mod tests {
|
||||
let generated = generate_rust_code(&cache).unwrap();
|
||||
assert_eq!("src/lib.rs", format!("{}", generated.path.display()));
|
||||
let expected = r#"
|
||||
pub mod com {
|
||||
pub mod android {
|
||||
pub mod aconfig {
|
||||
pub mod test {
|
||||
#[inline(always)]
|
||||
pub const fn r#test_disabled_ro() -> bool {
|
||||
pub const fn r#disabled_ro() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn r#test_disabled_rw() -> bool {
|
||||
flags_rust::GetServerConfigurableFlag("test", "disabled_rw", "false") == "true"
|
||||
pub fn r#disabled_rw() -> bool {
|
||||
flags_rust::GetServerConfigurableFlag("com.android.aconfig.test", "disabled_rw", "false") == "true"
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub const fn r#test_enabled_ro() -> bool {
|
||||
pub const fn r#enabled_ro() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn r#test_enabled_rw() -> bool {
|
||||
flags_rust::GetServerConfigurableFlag("test", "enabled_rw", "false") == "true"
|
||||
pub fn r#enabled_rw() -> bool {
|
||||
flags_rust::GetServerConfigurableFlag("com.android.aconfig.test", "enabled_rw", "false") == "true"
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"#;
|
||||
assert_eq!(expected.trim(), String::from_utf8(generated.contents).unwrap().trim());
|
||||
|
Reference in New Issue
Block a user