blob: dc3e6bc995f316b06d06a10945b0f67f69676c66 [file] [log] [blame]
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +02001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use anyhow::{Context, Result};
18use clap::ValueEnum;
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020019use std::fmt;
20use std::io::Read;
21
22use crate::aconfig::{Flag, Override};
23use crate::cache::Cache;
24
Mårten Kongstad29375662023-05-05 10:57:19 +020025#[derive(Clone)]
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020026pub enum Source {
27 #[allow(dead_code)] // only used in unit tests
28 Memory,
29 File(String),
30}
31
32impl fmt::Display for Source {
33 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34 match self {
35 Self::Memory => write!(f, "<memory>"),
36 Self::File(path) => write!(f, "{}", path),
37 }
38 }
39}
40
41pub struct Input {
42 pub source: Source,
43 pub reader: Box<dyn Read>,
44}
45
Mårten Kongstad09c28d12023-05-04 13:29:26 +020046pub fn create_cache(build_id: u32, aconfigs: Vec<Input>, overrides: Vec<Input>) -> Result<Cache> {
47 let mut cache = Cache::new(build_id);
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020048
49 for mut input in aconfigs {
50 let mut contents = String::new();
51 input.reader.read_to_string(&mut contents)?;
52 let flags = Flag::try_from_text_proto_list(&contents)
53 .with_context(|| format!("Failed to parse {}", input.source))?;
54 for flag in flags {
55 cache.add_flag(input.source.clone(), flag)?;
56 }
57 }
58
59 for mut input in overrides {
60 let mut contents = String::new();
61 input.reader.read_to_string(&mut contents)?;
62 let overrides = Override::try_from_text_proto_list(&contents)
63 .with_context(|| format!("Failed to parse {}", input.source))?;
64 for override_ in overrides {
65 cache.add_override(input.source.clone(), override_)?;
66 }
67 }
68
69 Ok(cache)
70}
71
72#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)]
73pub enum Format {
74 Text,
75 Debug,
76}
77
78pub fn dump_cache(cache: Cache, format: Format) -> Result<()> {
79 match format {
80 Format::Text => {
81 for item in cache.iter() {
Mårten Kongstadc68c4ea2023-05-05 16:20:09 +020082 println!("{}: {:?}", item.id, item.state);
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020083 }
84 }
85 Format::Debug => {
86 for item in cache.iter() {
Mårten Kongstadc68c4ea2023-05-05 16:20:09 +020087 println!("{:?}", item);
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020088 }
89 }
90 }
91 Ok(())
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
Mårten Kongstadc68c4ea2023-05-05 16:20:09 +020097 use crate::aconfig::{FlagState, Permission};
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +020098
99 #[test]
100 fn test_create_cache() {
101 let s = r#"
102 flag {
103 id: "a"
104 description: "Description of a"
Mårten Kongstad09c28d12023-05-04 13:29:26 +0200105 value {
Mårten Kongstadc68c4ea2023-05-05 16:20:09 +0200106 state: ENABLED
107 permission: READ_WRITE
Mårten Kongstad09c28d12023-05-04 13:29:26 +0200108 }
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200109 }
110 "#;
111 let aconfigs = vec![Input { source: Source::Memory, reader: Box::new(s.as_bytes()) }];
112 let o = r#"
113 override {
114 id: "a"
Mårten Kongstadc68c4ea2023-05-05 16:20:09 +0200115 state: DISABLED
Mårten Kongstad416330b2023-05-05 11:10:01 +0200116 permission: READ_ONLY
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200117 }
118 "#;
119 let overrides = vec![Input { source: Source::Memory, reader: Box::new(o.as_bytes()) }];
Mårten Kongstad09c28d12023-05-04 13:29:26 +0200120 let cache = create_cache(1, aconfigs, overrides).unwrap();
Mårten Kongstadc68c4ea2023-05-05 16:20:09 +0200121 let item = cache.iter().find(|&item| item.id == "a").unwrap();
122 assert_eq!(FlagState::Disabled, item.state);
123 assert_eq!(Permission::ReadOnly, item.permission);
Mårten Kongstad4d2b4b02023-04-27 16:05:58 +0200124 }
125}