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/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);
+    }
+}