Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 49e4d6e7 authored by Mårten Kongstad's avatar Mårten Kongstad
Browse files

aconfig: dump --filter: hook up command line args to dump.rs

This is the first step towards teaching dump to (optionally) filter
which flags to print.

A follow-up CL will implement dump::create_filter_predicate.

Bug: 315487153
Test: atest aconfig.test
Change-Id: Ibe0d4ce6563d3b5718fedd3ebfd45fbf5d935b92
parent ee58f98c
Loading
Loading
Loading
Loading
+14 −3
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ use std::path::PathBuf;
use crate::codegen::cpp::generate_cpp_code;
use crate::codegen::java::generate_java_code;
use crate::codegen::rust::generate_rust_code;
use crate::dump::DumpFormat;
use crate::dump::{DumpFormat, DumpPredicate};
use crate::protos::{
    ParsedFlagExt, ProtoFlagMetadata, ProtoFlagPermission, ProtoFlagState, ProtoParsedFlag,
    ProtoParsedFlags, ProtoTracepoint,
@@ -282,13 +282,22 @@ pub fn create_device_config_sysprops(mut input: Input) -> Result<Vec<u8>> {
pub fn dump_parsed_flags(
    mut input: Vec<Input>,
    format: DumpFormat,
    filters: &[&str],
    dedup: bool,
) -> Result<Vec<u8>> {
    let individually_parsed_flags: Result<Vec<ProtoParsedFlags>> =
        input.iter_mut().map(|i| i.try_parse_flags()).collect();
    let parsed_flags: ProtoParsedFlags =
        crate::protos::parsed_flags::merge(individually_parsed_flags?, dedup)?;
    crate::dump::dump_parsed_flags(parsed_flags.parsed_flag.into_iter(), format)
    let filters: Vec<Box<DumpPredicate>> = if filters.is_empty() {
        vec![Box::new(|_| true)]
    } else {
        filters.iter().map(|f| crate::dump::create_filter_predicate(f)).collect::<Result<Vec<_>>>()?
    };
    crate::dump::dump_parsed_flags(
        parsed_flags.parsed_flag.into_iter().filter(|flag| filters.iter().any(|p| p(flag))),
        format,
    )
}

fn find_unique_package(parsed_flags: &[ProtoParsedFlag]) -> Option<&str> {
@@ -570,6 +579,7 @@ mod tests {
        let bytes = dump_parsed_flags(
            vec![input],
            DumpFormat::Custom("{fully_qualified_name}".to_string()),
            &[],
            false,
        )
        .unwrap();
@@ -581,7 +591,8 @@ mod tests {
    fn test_dump_textproto_format_dedup() {
        let input = parse_test_flags_as_input();
        let input2 = parse_test_flags_as_input();
        let bytes = dump_parsed_flags(vec![input, input2], DumpFormat::Textproto, true).unwrap();
        let bytes =
            dump_parsed_flags(vec![input, input2], DumpFormat::Textproto, &[], true).unwrap();
        let text = std::str::from_utf8(&bytes).unwrap();
        assert_eq!(crate::test::TEST_FLAGS_TEXTPROTO.trim(), text.trim());
    }
+7 −0
Original line number Diff line number Diff line
@@ -123,6 +123,13 @@ fn dump_custom_format(flag: &ProtoParsedFlag, format: &str, output: &mut Vec<u8>
    output.extend_from_slice(str.as_bytes());
}

pub type DumpPredicate = dyn Fn(&ProtoParsedFlag) -> bool;

#[allow(unused)]
pub fn create_filter_predicate(filter: &str) -> Result<Box<DumpPredicate>> {
    todo!();
}

#[cfg(test)]
mod tests {
    use super::*;
+12 −1
Original line number Diff line number Diff line
@@ -37,6 +37,11 @@ mod test;

use commands::{CodegenMode, Input, OutputFile};

const HELP_DUMP_FILTER: &str = r#"
Limit which flags to output. If multiple --filter arguments are provided, the output will be
limited to flags that match any of the filters.
"#;

fn cli() -> Command {
    Command::new("aconfig")
        .subcommand_required(true)
@@ -109,6 +114,7 @@ fn cli() -> Command {
                        .value_parser(|s: &str| DumpFormat::try_from(s))
                        .default_value("text"),
                )
                .arg(Arg::new("filter").long("filter").action(ArgAction::Append).help(HELP_DUMP_FILTER.trim()))
                .arg(Arg::new("dedup").long("dedup").num_args(0).action(ArgAction::SetTrue))
                .arg(Arg::new("out").long("out").default_value("-")),
        )
@@ -252,8 +258,13 @@ fn main() -> Result<()> {
            let input = open_zero_or_more_files(sub_matches, "cache")?;
            let format = get_required_arg::<DumpFormat>(sub_matches, "format")
                .context("failed to dump previously parsed flags")?;
            let filters = sub_matches
                .get_many::<String>("filter")
                .unwrap_or_default()
                .map(String::as_ref)
                .collect::<Vec<_>>();
            let dedup = get_required_arg::<bool>(sub_matches, "dedup")?;
            let output = commands::dump_parsed_flags(input, format.clone(), *dedup)?;
            let output = commands::dump_parsed_flags(input, format.clone(), &filters, *dedup)?;
            let path = get_required_arg::<String>(sub_matches, "out")?;
            write_output_to_file_or_stdout(path, &output)?;
        }