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 | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 19 | use protobuf::Message; |
Mårten Kongstad | 76adff2 | 2023-05-08 10:57:24 +0200 | [diff] [blame] | 20 | use serde::{Deserialize, Serialize}; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 21 | use std::fmt; |
| 22 | use std::io::Read; |
| 23 | |
| 24 | use crate::aconfig::{Flag, Override}; |
| 25 | use crate::cache::Cache; |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 26 | use crate::protos::ProtoDump; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 27 | |
Mårten Kongstad | 76adff2 | 2023-05-08 10:57:24 +0200 | [diff] [blame] | 28 | #[derive(Serialize, Deserialize, Clone, Debug)] |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 29 | pub enum Source { |
| 30 | #[allow(dead_code)] // only used in unit tests |
| 31 | Memory, |
| 32 | File(String), |
| 33 | } |
| 34 | |
| 35 | impl fmt::Display for Source { |
| 36 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 37 | match self { |
| 38 | Self::Memory => write!(f, "<memory>"), |
| 39 | Self::File(path) => write!(f, "{}", path), |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | pub struct Input { |
| 45 | pub source: Source, |
| 46 | pub reader: Box<dyn Read>, |
| 47 | } |
| 48 | |
Mårten Kongstad | 09c28d1 | 2023-05-04 13:29:26 +0200 | [diff] [blame] | 49 | pub fn create_cache(build_id: u32, aconfigs: Vec<Input>, overrides: Vec<Input>) -> Result<Cache> { |
| 50 | let mut cache = Cache::new(build_id); |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 51 | |
| 52 | for mut input in aconfigs { |
| 53 | let mut contents = String::new(); |
| 54 | input.reader.read_to_string(&mut contents)?; |
| 55 | let flags = Flag::try_from_text_proto_list(&contents) |
| 56 | .with_context(|| format!("Failed to parse {}", input.source))?; |
| 57 | for flag in flags { |
| 58 | cache.add_flag(input.source.clone(), flag)?; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | for mut input in overrides { |
| 63 | let mut contents = String::new(); |
| 64 | input.reader.read_to_string(&mut contents)?; |
| 65 | let overrides = Override::try_from_text_proto_list(&contents) |
| 66 | .with_context(|| format!("Failed to parse {}", input.source))?; |
| 67 | for override_ in overrides { |
| 68 | cache.add_override(input.source.clone(), override_)?; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | Ok(cache) |
| 73 | } |
| 74 | |
| 75 | #[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)] |
| 76 | pub enum Format { |
| 77 | Text, |
| 78 | Debug, |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 79 | Protobuf, |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 82 | pub fn dump_cache(cache: Cache, format: Format) -> Result<Vec<u8>> { |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 83 | match format { |
| 84 | Format::Text => { |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 85 | let mut lines = vec![]; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 86 | for item in cache.iter() { |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 87 | lines.push(format!("{}: {:?}\n", item.id, item.state)); |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 88 | } |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 89 | Ok(lines.concat().into()) |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 90 | } |
| 91 | Format::Debug => { |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 92 | let mut lines = vec![]; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 93 | for item in cache.iter() { |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 94 | lines.push(format!("{:?}\n", item)); |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 95 | } |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 96 | Ok(lines.concat().into()) |
| 97 | } |
| 98 | Format::Protobuf => { |
| 99 | let dump: ProtoDump = cache.into(); |
| 100 | let mut output = vec![]; |
| 101 | dump.write_to_vec(&mut output)?; |
| 102 | Ok(output) |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 103 | } |
| 104 | } |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | #[cfg(test)] |
| 108 | mod tests { |
| 109 | use super::*; |
Mårten Kongstad | c68c4ea | 2023-05-05 16:20:09 +0200 | [diff] [blame] | 110 | use crate::aconfig::{FlagState, Permission}; |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 111 | |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 112 | fn create_test_cache() -> Cache { |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 113 | let s = r#" |
| 114 | flag { |
| 115 | id: "a" |
| 116 | description: "Description of a" |
Mårten Kongstad | 09c28d1 | 2023-05-04 13:29:26 +0200 | [diff] [blame] | 117 | value { |
Mårten Kongstad | c68c4ea | 2023-05-05 16:20:09 +0200 | [diff] [blame] | 118 | state: ENABLED |
| 119 | permission: READ_WRITE |
Mårten Kongstad | 09c28d1 | 2023-05-04 13:29:26 +0200 | [diff] [blame] | 120 | } |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 121 | } |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 122 | flag { |
| 123 | id: "b" |
| 124 | description: "Description of b" |
| 125 | value { |
| 126 | state: ENABLED |
| 127 | permission: READ_ONLY |
| 128 | } |
| 129 | } |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 130 | "#; |
| 131 | let aconfigs = vec![Input { source: Source::Memory, reader: Box::new(s.as_bytes()) }]; |
| 132 | let o = r#" |
| 133 | override { |
| 134 | id: "a" |
Mårten Kongstad | c68c4ea | 2023-05-05 16:20:09 +0200 | [diff] [blame] | 135 | state: DISABLED |
Mårten Kongstad | 416330b | 2023-05-05 11:10:01 +0200 | [diff] [blame] | 136 | permission: READ_ONLY |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 137 | } |
| 138 | "#; |
| 139 | let overrides = vec![Input { source: Source::Memory, reader: Box::new(o.as_bytes()) }]; |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 140 | create_cache(1, aconfigs, overrides).unwrap() |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn test_create_cache() { |
| 145 | let cache = create_test_cache(); // calls create_cache |
Mårten Kongstad | c68c4ea | 2023-05-05 16:20:09 +0200 | [diff] [blame] | 146 | let item = cache.iter().find(|&item| item.id == "a").unwrap(); |
| 147 | assert_eq!(FlagState::Disabled, item.state); |
| 148 | assert_eq!(Permission::ReadOnly, item.permission); |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 149 | } |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame^] | 150 | |
| 151 | #[test] |
| 152 | fn test_dump_text_format() { |
| 153 | let cache = create_test_cache(); |
| 154 | let bytes = dump_cache(cache, Format::Text).unwrap(); |
| 155 | let text = std::str::from_utf8(&bytes).unwrap(); |
| 156 | assert!(text.contains("a: Disabled")); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn test_dump_protobuf_format() { |
| 161 | use protobuf::Message; |
| 162 | |
| 163 | let cache = create_test_cache(); |
| 164 | let bytes = dump_cache(cache, Format::Protobuf).unwrap(); |
| 165 | let actual = ProtoDump::parse_from_bytes(&bytes).unwrap(); |
| 166 | |
| 167 | assert_eq!( |
| 168 | vec!["a".to_string(), "b".to_string()], |
| 169 | actual.item.iter().map(|item| item.id.clone().unwrap()).collect::<Vec<_>>() |
| 170 | ); |
| 171 | } |
Mårten Kongstad | 4d2b4b0 | 2023-04-27 16:05:58 +0200 | [diff] [blame] | 172 | } |