Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | use anyhow::{Context, Result}; |
| 18 | use clap::ValueEnum; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 19 | use std::fmt; |
| 20 | use std::io::Read; |
| 21 | |
| 22 | use crate::aconfig::{Flag, Override}; |
| 23 | use crate::cache::Cache; |
| 24 | |
Mårten Kongstad | 2937566 | 2023-05-05 10:57:19 +0200 | [diff] [blame] | 25 | #[derive(Clone)] |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 26 | pub enum Source { |
| 27 | #[allow(dead_code)] // only used in unit tests |
| 28 | Memory, |
| 29 | File(String), |
| 30 | } |
| 31 | |
| 32 | impl 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 | |
| 41 | pub struct Input { |
| 42 | pub source: Source, |
| 43 | pub reader: Box<dyn Read>, |
| 44 | } |
| 45 | |
Mårten Kongstad | 09c28d1 | 2023-05-04 13:29:26 +0200 | [diff] [blame] | 46 | pub fn create_cache(build_id: u32, aconfigs: Vec<Input>, overrides: Vec<Input>) -> Result<Cache> { |
| 47 | let mut cache = Cache::new(build_id); |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 48 | |
| 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)] |
| 73 | pub enum Format { |
| 74 | Text, |
| 75 | Debug, |
| 76 | } |
| 77 | |
| 78 | pub fn dump_cache(cache: Cache, format: Format) -> Result<()> { |
| 79 | match format { |
| 80 | Format::Text => { |
| 81 | for item in cache.iter() { |
Mårten Kongstad | 09c28d1 | 2023-05-04 13:29:26 +0200 | [diff] [blame] | 82 | println!("{}: {}", item.id, item.value); |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | Format::Debug => { |
| 86 | for item in cache.iter() { |
Mårten Kongstad | 09c28d1 | 2023-05-04 13:29:26 +0200 | [diff] [blame] | 87 | println!("{}: {} ({:?})", item.id, item.value, item.debug); |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
| 91 | Ok(()) |
| 92 | } |
| 93 | |
| 94 | #[cfg(test)] |
| 95 | mod tests { |
| 96 | use super::*; |
| 97 | |
| 98 | #[test] |
| 99 | fn test_create_cache() { |
| 100 | let s = r#" |
| 101 | flag { |
| 102 | id: "a" |
| 103 | description: "Description of a" |
Mårten Kongstad | 09c28d1 | 2023-05-04 13:29:26 +0200 | [diff] [blame] | 104 | value { |
| 105 | value: true |
Mårten Kongstad | 416330b | 2023-05-05 11:10:01 +0200 | [diff] [blame^] | 106 | permission: READ_ONLY |
Mårten Kongstad | 09c28d1 | 2023-05-04 13:29:26 +0200 | [diff] [blame] | 107 | } |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 108 | } |
| 109 | "#; |
| 110 | let aconfigs = vec![Input { source: Source::Memory, reader: Box::new(s.as_bytes()) }]; |
| 111 | let o = r#" |
| 112 | override { |
| 113 | id: "a" |
| 114 | value: false |
Mårten Kongstad | 416330b | 2023-05-05 11:10:01 +0200 | [diff] [blame^] | 115 | permission: READ_ONLY |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 116 | } |
| 117 | "#; |
| 118 | let overrides = vec![Input { source: Source::Memory, reader: Box::new(o.as_bytes()) }]; |
Mårten Kongstad | 09c28d1 | 2023-05-04 13:29:26 +0200 | [diff] [blame] | 119 | let cache = create_cache(1, aconfigs, overrides).unwrap(); |
| 120 | let value = cache.iter().find(|&item| item.id == "a").unwrap().value; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 121 | assert!(!value); |
| 122 | } |
| 123 | } |