aconfig: use proto struct directly

Remove the hand-crafted wrappers around the structures auto-generated
from protos/aconfig.proto, and use the auto-generated structs directly
intead. This gets rid of a lot of manual repetition, and its inherent
risk.

Also unify how individual fields read from text proto are verified (e.g.
is the flag.name field a valid identifier).

Also change the intermediate cache format from JSON to binary protobuf.

The concept of a 'cache' as an intermediate internal format to represent
parsed input stays. The command line interface still refers to caches.
At the moment a cache file is identical to a parsed_file protbuf, and
the code exploits this internally.

A couple of points regarding the auto-generated structs:

  - Vectors are named in the singular (e.g. parsed_flags.parsed_flag is
    a Vec<ProtoParsedFlag>) because this improves ergonomics for all
    devs working with aconfig input files

  - The auto-generated structs have fields that are of type Option<T>
    and convenience methods (named the same as the fields) to access T

Test: atest aconfig.test aconfig.test.java
Bug: 283910447
Change-Id: I512820cc4bc6c543dea9f6a4356f863120a10be3
This commit is contained in:
Mårten Kongstad
2023-06-14 09:51:56 +02:00
parent a99ac90eb5
commit 403658f9cb
10 changed files with 934 additions and 1125 deletions

View File

@@ -18,18 +18,19 @@ use anyhow::Result;
use serde::Serialize;
use tinytemplate::TinyTemplate;
use crate::aconfig::{FlagState, Permission};
use crate::cache::{Cache, Item};
use crate::codegen;
use crate::commands::OutputFile;
use crate::protos::{ProtoFlagPermission, ProtoFlagState, ProtoParsedFlag};
pub fn generate_rust_code(cache: &Cache) -> Result<OutputFile> {
let package = cache.package();
let parsed_flags: Vec<TemplateParsedFlag> =
cache.iter().map(|item| TemplateParsedFlag::new(package, item)).collect();
pub fn generate_rust_code<'a, I>(package: &str, parsed_flags_iter: I) -> Result<OutputFile>
where
I: Iterator<Item = &'a ProtoParsedFlag>,
{
let template_flags: Vec<TemplateParsedFlag> =
parsed_flags_iter.map(|pf| TemplateParsedFlag::new(package, pf)).collect();
let context = TemplateContext {
package: package.to_string(),
parsed_flags,
template_flags,
modules: package.split('.').map(|s| s.to_string()).collect::<Vec<_>>(),
};
let mut template = TinyTemplate::new();
@@ -42,7 +43,7 @@ pub fn generate_rust_code(cache: &Cache) -> Result<OutputFile> {
#[derive(Serialize)]
struct TemplateContext {
pub package: String,
pub parsed_flags: Vec<TemplateParsedFlag>,
pub template_flags: Vec<TemplateParsedFlag>,
pub modules: Vec<String>,
}
@@ -61,17 +62,17 @@ struct TemplateParsedFlag {
impl TemplateParsedFlag {
#[allow(clippy::nonminimal_bool)]
fn new(package: &str, item: &Item) -> Self {
fn new(package: &str, pf: &ProtoParsedFlag) -> Self {
let template = TemplateParsedFlag {
name: item.name.clone(),
device_config_namespace: item.namespace.to_string(),
device_config_flag: codegen::create_device_config_ident(package, &item.name)
.expect("values checked at cache creation time"),
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,
name: pf.name().to_string(),
device_config_namespace: pf.namespace().to_string(),
device_config_flag: codegen::create_device_config_ident(package, pf.name())
.expect("values checked at flag parse time"),
is_read_only_enabled: pf.permission() == ProtoFlagPermission::READ_ONLY
&& pf.state() == ProtoFlagState::ENABLED,
is_read_only_disabled: pf.permission() == ProtoFlagPermission::READ_ONLY
&& pf.state() == ProtoFlagState::DISABLED,
is_read_write: pf.permission() == ProtoFlagPermission::READ_WRITE,
};
#[rustfmt::skip]
debug_assert!(
@@ -93,8 +94,9 @@ mod tests {
#[test]
fn test_generate_rust_code() {
let cache = crate::test::create_cache();
let generated = generate_rust_code(&cache).unwrap();
let parsed_flags = crate::test::parse_test_flags();
let generated =
generate_rust_code(crate::test::TEST_PACKAGE, parsed_flags.parsed_flag.iter()).unwrap();
assert_eq!("src/lib.rs", format!("{}", generated.path.display()));
let expected = r#"
pub mod com {