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

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

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
parent fe753f53
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ rust_defaults {
    srcs: ["src/main.rs"],
    rustlibs: [
        "libaconfig_protos",
        "libanyhow",
        "libprotobuf",
    ],
}
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ default = ["cargo"]
cargo = []

[dependencies]
anyhow = "1.0.69"
protobuf = "3.2.0"

[build-dependencies]
+22 −5
Original line number Diff line number Diff line
@@ -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;
};
+159 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
+16 −33
Original line number Diff line number Diff line
@@ -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)
}

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);
    }
use aconfig::{Flag, Override};

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(())
}
Loading