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 anyhow::Result; 18 use regex::Regex; 19 use std::{collections::HashSet, io::Read}; 20 21 use crate::FlagId; 22 23 /// Grep for all flags used with @FlaggedApi annotations in an API signature file (*current.txt 24 /// file). extract_flagged_api_flags<R: Read>(mut reader: R) -> Result<HashSet<FlagId>>25pub(crate) fn extract_flagged_api_flags<R: Read>(mut reader: R) -> Result<HashSet<FlagId>> { 26 let mut haystack = String::new(); 27 reader.read_to_string(&mut haystack)?; 28 let regex = Regex::new(r#"(?ms)@FlaggedApi\("(.*?)"\)"#).unwrap(); 29 let iter = regex.captures_iter(&haystack).map(|cap| cap[1].to_owned()); 30 Ok(HashSet::from_iter(iter)) 31 } 32 33 #[cfg(test)] 34 mod tests { 35 use super::*; 36 37 #[test] test()38 fn test() { 39 let api_signature_file = include_bytes!("../tests/api-signature-file.txt"); 40 let flags = extract_flagged_api_flags(&api_signature_file[..]).unwrap(); 41 assert_eq!( 42 flags, 43 HashSet::from_iter(vec![ 44 "record_finalized_flags.test.foo".to_string(), 45 "this.flag.is.not.used".to_string(), 46 ]) 47 ); 48 } 49 } 50