aconfig: add dump protobuf format

Introduce a new protobuf format to represent the all flags parsed by
aconfig. This data in this new format is similar to that of the internal
cache object, but the protobuf is a public API for other tools to
consume and any changes to the proto spec must be backwards compatible.

When aconfig has matured more, Cache can potentially be rewritten to
work with proto structs directly, removing some of the hand-written
wrapper structs and the logic to convert to and from the proto structs.
At this point, the intermediate json format can be replaced by the
protobuf dump.

Also, teach `aconfig dump` to accept an --out <file> argument (default:
stdout).

Also, teach `aconfig dump` to read more than once cache file.

Note: the new protobuf fields refer to existing fields. It would make
sense to split the .proto file in one for input and one for output
formats, and import the common messages, but the Android build system
and cargo will need different import paths. Keep the definitions in the
same file to circumvent this problem.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I55ee4a52c0fb3369d91d61406867ae03a15805c3
diff --git a/tools/aconfig/src/commands.rs b/tools/aconfig/src/commands.rs
index 73d3357..645d395 100644
--- a/tools/aconfig/src/commands.rs
+++ b/tools/aconfig/src/commands.rs
@@ -16,12 +16,14 @@
 
 use anyhow::{Context, Result};
 use clap::ValueEnum;
+use protobuf::Message;
 use serde::{Deserialize, Serialize};
 use std::fmt;
 use std::io::Read;
 
 use crate::aconfig::{Flag, Override};
 use crate::cache::Cache;
+use crate::protos::ProtoDump;
 
 #[derive(Serialize, Deserialize, Clone, Debug)]
 pub enum Source {
@@ -74,22 +76,32 @@
 pub enum Format {
     Text,
     Debug,
+    Protobuf,
 }
 
-pub fn dump_cache(cache: Cache, format: Format) -> Result<()> {
+pub fn dump_cache(cache: Cache, format: Format) -> Result<Vec<u8>> {
     match format {
         Format::Text => {
+            let mut lines = vec![];
             for item in cache.iter() {
-                println!("{}: {:?}", item.id, item.state);
+                lines.push(format!("{}: {:?}\n", item.id, item.state));
             }
+            Ok(lines.concat().into())
         }
         Format::Debug => {
+            let mut lines = vec![];
             for item in cache.iter() {
-                println!("{:?}", item);
+                lines.push(format!("{:?}\n", item));
             }
+            Ok(lines.concat().into())
+        }
+        Format::Protobuf => {
+            let dump: ProtoDump = cache.into();
+            let mut output = vec![];
+            dump.write_to_vec(&mut output)?;
+            Ok(output)
         }
     }
-    Ok(())
 }
 
 #[cfg(test)]
@@ -97,8 +109,7 @@
     use super::*;
     use crate::aconfig::{FlagState, Permission};
 
-    #[test]
-    fn test_create_cache() {
+    fn create_test_cache() -> Cache {
         let s = r#"
         flag {
             id: "a"
@@ -108,6 +119,14 @@
                 permission: READ_WRITE
             }
         }
+        flag {
+            id: "b"
+            description: "Description of b"
+            value {
+                state: ENABLED
+                permission: READ_ONLY
+            }
+        }
         "#;
         let aconfigs = vec![Input { source: Source::Memory, reader: Box::new(s.as_bytes()) }];
         let o = r#"
@@ -118,9 +137,36 @@
         }
         "#;
         let overrides = vec![Input { source: Source::Memory, reader: Box::new(o.as_bytes()) }];
-        let cache = create_cache(1, aconfigs, overrides).unwrap();
+        create_cache(1, aconfigs, overrides).unwrap()
+    }
+
+    #[test]
+    fn test_create_cache() {
+        let cache = create_test_cache(); // calls create_cache
         let item = cache.iter().find(|&item| item.id == "a").unwrap();
         assert_eq!(FlagState::Disabled, item.state);
         assert_eq!(Permission::ReadOnly, item.permission);
     }
+
+    #[test]
+    fn test_dump_text_format() {
+        let cache = create_test_cache();
+        let bytes = dump_cache(cache, Format::Text).unwrap();
+        let text = std::str::from_utf8(&bytes).unwrap();
+        assert!(text.contains("a: Disabled"));
+    }
+
+    #[test]
+    fn test_dump_protobuf_format() {
+        use protobuf::Message;
+
+        let cache = create_test_cache();
+        let bytes = dump_cache(cache, Format::Protobuf).unwrap();
+        let actual = ProtoDump::parse_from_bytes(&bytes).unwrap();
+
+        assert_eq!(
+            vec!["a".to_string(), "b".to_string()],
+            actual.item.iter().map(|item| item.id.clone().unwrap()).collect::<Vec<_>>()
+        );
+    }
 }