aconfig: define Aconfig proto

Fill in aconfig.proto. Define Aconfig definitions (for introducing flags
and setting their values) and Overrides (for overriding flags regardless
of what their definitions say). More changes to the proto schema are
expected when more of the aconfig project is outlined.

Use proto2 instead of proto3: this will cause the protobuf text parser
to error on missing fields instead of returning a default value which is
ambiguous, especially for booleans. (Also, the text protobuf parser
doesn't provide good error messages: if the input is missing a field,
the error is always "1:1: Message not initialized").

Unfortunately the generated Rust wrappers around the proto structs land
in an external crate, which prevents aconfig from adding new impl
blocks. Circumvent this by converting the data read from proto into
structs defined in the aconfig crate.

Change main.rs to parse (static) text proto.

Also add dependency on anyhow.

Bug: 279485059
Test: atest aconfig.test
Change-Id: I512e3b61ef20e2f55b7699a178d466d2a9a89eac
diff --git a/tools/aconfig/Android.bp b/tools/aconfig/Android.bp
index b3813bf..e708b8a 100644
--- a/tools/aconfig/Android.bp
+++ b/tools/aconfig/Android.bp
@@ -18,6 +18,7 @@
     srcs: ["src/main.rs"],
     rustlibs: [
         "libaconfig_protos",
+        "libanyhow",
         "libprotobuf",
     ],
 }
diff --git a/tools/aconfig/Cargo.toml b/tools/aconfig/Cargo.toml
index 1e86f89..10cd26b 100644
--- a/tools/aconfig/Cargo.toml
+++ b/tools/aconfig/Cargo.toml
@@ -9,6 +9,7 @@
 cargo = []
 
 [dependencies]
+anyhow = "1.0.69"
 protobuf = "3.2.0"
 
 [build-dependencies]
diff --git a/tools/aconfig/protos/aconfig.proto b/tools/aconfig/protos/aconfig.proto
index 989c398..7d0e8ad 100644
--- a/tools/aconfig/protos/aconfig.proto
+++ b/tools/aconfig/protos/aconfig.proto
@@ -12,12 +12,29 @@
 // See the License for the specific language governing permissions and
 // limitations under the License
 
-// Placeholder proto file. Will be replaced by actual contents.
+// This is the schema definition for of Aconfig files. Modifications need to be
+// either backwards compatible, or include updates to all Aconfig files in the
+// Android tree.
 
-syntax = "proto3";
+syntax = "proto2";
 
 package android.aconfig;
 
-message Placeholder {
-  string name = 1;
-}
+message flag {
+  required string id = 1;
+  required string description = 2;
+  required bool value = 3;
+};
+
+message android_config {
+  repeated flag flag = 1;
+};
+
+message override {
+  required string id = 1;
+  required bool value = 2;
+};
+
+message override_config {
+  repeated override override = 1;
+};
diff --git a/tools/aconfig/src/aconfig.rs b/tools/aconfig/src/aconfig.rs
new file mode 100644
index 0000000..a6e1d1b
--- /dev/null
+++ b/tools/aconfig/src/aconfig.rs
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+use anyhow::{anyhow, Context, Error, Result};
+
+use crate::protos::{ProtoAndroidConfig, ProtoFlag, ProtoOverride, ProtoOverrideConfig};
+
+#[derive(Debug, PartialEq, Eq)]
+pub struct Flag {
+    pub id: String,
+    pub description: String,
+    pub value: bool,
+}
+
+impl Flag {
+    pub fn try_from_text_proto(text_proto: &str) -> Result<Flag> {
+        let proto: ProtoFlag = crate::protos::try_from_text_proto(text_proto)
+            .with_context(|| text_proto.to_owned())?;
+        proto.try_into()
+    }
+
+    pub fn try_from_text_proto_list(text_proto: &str) -> Result<Vec<Flag>> {
+        let proto: ProtoAndroidConfig = crate::protos::try_from_text_proto(text_proto)
+            .with_context(|| text_proto.to_owned())?;
+        proto.flag.into_iter().map(|proto_flag| proto_flag.try_into()).collect()
+    }
+}
+
+impl TryFrom<ProtoFlag> for Flag {
+    type Error = Error;
+
+    fn try_from(proto: ProtoFlag) -> Result<Self, Self::Error> {
+        let Some(id) = proto.id else {
+            return Err(anyhow!("missing 'id' field"));
+        };
+        let Some(description) = proto.description else {
+            return Err(anyhow!("missing 'description' field"));
+        };
+        let Some(value) = proto.value else {
+            return Err(anyhow!("missing 'value' field"));
+        };
+        Ok(Flag { id, description, value })
+    }
+}
+
+#[derive(Debug, PartialEq, Eq)]
+pub struct Override {
+    pub id: String,
+    pub value: bool,
+}
+
+impl Override {
+    pub fn try_from_text_proto(text_proto: &str) -> Result<Override> {
+        let proto: ProtoOverride = crate::protos::try_from_text_proto(text_proto)?;
+        proto.try_into()
+    }
+
+    pub fn try_from_text_proto_list(text_proto: &str) -> Result<Vec<Override>> {
+        let proto: ProtoOverrideConfig = crate::protos::try_from_text_proto(text_proto)?;
+        proto.override_.into_iter().map(|proto_flag| proto_flag.try_into()).collect()
+    }
+}
+
+impl TryFrom<ProtoOverride> for Override {
+    type Error = Error;
+
+    fn try_from(proto: ProtoOverride) -> Result<Self, Self::Error> {
+        let Some(id) = proto.id else {
+            return Err(anyhow!("missing 'id' field"));
+        };
+        let Some(value) = proto.value else {
+            return Err(anyhow!("missing 'value' field"));
+        };
+        Ok(Override { id, value })
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_flag_try_from_text_proto() {
+        let expected = Flag {
+            id: "1234".to_owned(),
+            description: "Description of the flag".to_owned(),
+            value: true,
+        };
+
+        let s = r#"
+        id: "1234"
+        description: "Description of the flag"
+        value: true
+        "#;
+        let actual = Flag::try_from_text_proto(s).unwrap();
+
+        assert_eq!(expected, actual);
+    }
+
+    #[test]
+    fn test_flag_try_from_text_proto_missing_field() {
+        let s = r#"
+        description: "Description of the flag"
+        value: true
+        "#;
+        let error = Flag::try_from_text_proto(s).unwrap_err();
+        assert!(format!("{:?}", error).contains("Message not initialized"));
+    }
+
+    #[test]
+    fn test_flag_try_from_text_proto_list() {
+        let expected = vec![
+            Flag { id: "a".to_owned(), description: "A".to_owned(), value: true },
+            Flag { id: "b".to_owned(), description: "B".to_owned(), value: false },
+        ];
+
+        let s = r#"
+        flag {
+            id: "a"
+            description: "A"
+            value: true
+        }
+        flag {
+            id: "b"
+            description: "B"
+            value: false
+        }
+        "#;
+        let actual = Flag::try_from_text_proto_list(s).unwrap();
+
+        assert_eq!(expected, actual);
+    }
+
+    #[test]
+    fn test_override_try_from_text_proto_list() {
+        let expected = Override { id: "1234".to_owned(), value: true };
+
+        let s = r#"
+        id: "1234"
+        value: true
+        "#;
+        let actual = Override::try_from_text_proto(s).unwrap();
+
+        assert_eq!(expected, actual);
+    }
+}
diff --git a/tools/aconfig/src/main.rs b/tools/aconfig/src/main.rs
index 5414f0e..9d87ca4 100644
--- a/tools/aconfig/src/main.rs
+++ b/tools/aconfig/src/main.rs
@@ -16,40 +16,23 @@
 
 //! `aconfig` is a build time tool to manage build time configurations, such as feature flags.
 
-use protobuf::text_format::{parse_from_str, ParseError};
+use anyhow::Result;
 
+mod aconfig;
 mod protos;
-use protos::Placeholder;
 
-fn foo() -> Result<String, ParseError> {
-    let placeholder = parse_from_str::<Placeholder>(r#"name: "aconfig""#)?;
-    Ok(placeholder.name)
-}
+use aconfig::{Flag, Override};
 
-fn main() {
-    println!("{:?}", foo());
-}
-
-#[cfg(test)]
-mod tests {
-    use super::*;
-
-    #[test]
-    fn test_foo() {
-        assert_eq!("aconfig", foo().unwrap());
-    }
-
-    #[test]
-    fn test_binary_protobuf() {
-        use protobuf::Message;
-        let mut buffer = Vec::new();
-
-        let mut original = Placeholder::new();
-        original.name = "test".to_owned();
-        original.write_to_writer(&mut buffer).unwrap();
-
-        let copy = Placeholder::parse_from_reader(&mut buffer.as_slice()).unwrap();
-
-        assert_eq!(original, copy);
-    }
+fn main() -> Result<()> {
+    let flag = Flag::try_from_text_proto(r#"id: "a" description: "description of a" value: true"#)?;
+    println!("{:?}", flag);
+    let flags = Flag::try_from_text_proto_list(
+        r#"flag { id: "a" description: "description of a" value: true }"#,
+    )?;
+    println!("{:?}", flags);
+    let override_ = Override::try_from_text_proto(r#"id: "foo" value: true"#)?;
+    println!("{:?}", override_);
+    let overrides = Override::try_from_text_proto_list(r#"override { id: "foo" value: true }"#)?;
+    println!("{:?}", overrides);
+    Ok(())
 }
diff --git a/tools/aconfig/src/protos.rs b/tools/aconfig/src/protos.rs
index 99d584a..42a7c1a 100644
--- a/tools/aconfig/src/protos.rs
+++ b/tools/aconfig/src/protos.rs
@@ -26,13 +26,42 @@
 //
 // This module hides these differences from the rest of aconfig.
 
-// When building with the Android tool-chain:
+// ---- When building with the Android tool-chain ----
 #[cfg(not(feature = "cargo"))]
-pub use aconfig_protos::aconfig::Placeholder;
+pub use aconfig_protos::aconfig::Android_config as ProtoAndroidConfig;
 
-// When building with cargo:
+#[cfg(not(feature = "cargo"))]
+pub use aconfig_protos::aconfig::Flag as ProtoFlag;
+
+#[cfg(not(feature = "cargo"))]
+pub use aconfig_protos::aconfig::Override_config as ProtoOverrideConfig;
+
+#[cfg(not(feature = "cargo"))]
+pub use aconfig_protos::aconfig::Override as ProtoOverride;
+
+// ---- When building with cargo ----
 #[cfg(feature = "cargo")]
 include!(concat!(env!("OUT_DIR"), "/aconfig_proto/mod.rs"));
 
 #[cfg(feature = "cargo")]
-pub use aconfig::Placeholder;
+pub use aconfig::Android_config as ProtoAndroidConfig;
+
+#[cfg(feature = "cargo")]
+pub use aconfig::Flag as ProtoFlag;
+
+#[cfg(feature = "cargo")]
+pub use aconfig::Override_config as ProtoOverrideConfig;
+
+#[cfg(feature = "cargo")]
+pub use aconfig::Override as ProtoOverride;
+
+// ---- Common for both the Android tool-chain and cargo ----
+use anyhow::Result;
+
+pub fn try_from_text_proto<T>(s: &str) -> Result<T>
+where
+    T: protobuf::MessageFull,
+{
+    // warning: parse_from_str does not check if required fields are set
+    protobuf::text_format::parse_from_str(s).map_err(|e| e.into())
+}