1 /*
2 * Copyright (C) 2025 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 aconfig_protos::{ParsedFlagExt, ProtoFlagPermission, ProtoFlagState};
18 use anyhow::{anyhow, Result};
19 use std::{collections::HashSet, io::Read};
20
21 use crate::FlagId;
22
23 /// Parse a ProtoParsedFlags binary protobuf blob and return the fully qualified names of flags
24 /// that are slated for API finalization (i.e. are both ENABLED and READ_ONLY).
get_relevant_flags_from_binary_proto<R: Read>( mut reader: R, ) -> Result<HashSet<FlagId>>25 pub(crate) fn get_relevant_flags_from_binary_proto<R: Read>(
26 mut reader: R,
27 ) -> Result<HashSet<FlagId>> {
28 let mut buffer = Vec::new();
29 reader.read_to_end(&mut buffer)?;
30 let parsed_flags = aconfig_protos::parsed_flags::try_from_binary_proto(&buffer)
31 .map_err(|_| anyhow!("failed to parse binary proto"))?;
32 let iter = parsed_flags
33 .parsed_flag
34 .into_iter()
35 .filter(|flag| {
36 flag.state() == ProtoFlagState::ENABLED
37 && flag.permission() == ProtoFlagPermission::READ_ONLY
38 })
39 .map(|flag| flag.fully_qualified_name());
40 Ok(HashSet::from_iter(iter))
41 }
42
43 #[cfg(test)]
44 mod tests {
45 use super::*;
46
47 #[test]
test_disabled_or_read_write_flags_are_ignored()48 fn test_disabled_or_read_write_flags_are_ignored() {
49 let bytes = include_bytes!("../tests/flags.protobuf");
50 let flags = get_relevant_flags_from_binary_proto(&bytes[..]).unwrap();
51 assert_eq!(flags, HashSet::from_iter(vec!["record_finalized_flags.test.foo".to_string()]));
52 }
53 }
54