blob: fea9961ccd1e2210531f5bb384a9b9291e6d7ece [file] [log] [blame]
Mårten Kongstad00cf0452023-05-26 16:48:01 +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
Mårten Kongstad066575b2023-06-07 16:29:25 +020017use anyhow::{ensure, Result};
18
Mårten Kongstadfbd71e22023-05-31 13:29:35 +020019pub fn is_valid_name_ident(s: &str) -> bool {
Mårten Kongstad00cf0452023-05-26 16:48:01 +020020 // Identifiers must match [a-z][a-z0-9_]*
21 let mut chars = s.chars();
22 let Some(first) = chars.next() else {
23 return false;
24 };
25 if !first.is_ascii_lowercase() {
26 return false;
27 }
28 chars.all(|ch| ch.is_ascii_lowercase() || ch.is_ascii_digit() || ch == '_')
29}
30
Mårten Kongstadfbd71e22023-05-31 13:29:35 +020031pub fn is_valid_package_ident(s: &str) -> bool {
32 s.split('.').all(is_valid_name_ident)
33}
34
Mårten Kongstad066575b2023-06-07 16:29:25 +020035pub fn create_device_config_ident(package: &str, flag_name: &str) -> Result<String> {
36 ensure!(is_valid_package_ident(package), "bad package");
37 ensure!(is_valid_package_ident(flag_name), "bad flag name");
38 Ok(format!("{}.{}", package, flag_name))
39}
40
Mårten Kongstad00cf0452023-05-26 16:48:01 +020041#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
Mårten Kongstadfbd71e22023-05-31 13:29:35 +020046 fn test_is_valid_name_ident() {
47 assert!(is_valid_name_ident("foo"));
48 assert!(is_valid_name_ident("foo_bar_123"));
Mårten Kongstad00cf0452023-05-26 16:48:01 +020049
Mårten Kongstadfbd71e22023-05-31 13:29:35 +020050 assert!(!is_valid_name_ident(""));
51 assert!(!is_valid_name_ident("123_foo"));
52 assert!(!is_valid_name_ident("foo-bar"));
53 assert!(!is_valid_name_ident("foo-b\u{00e5}r"));
54 }
55
56 #[test]
57 fn test_is_valid_package_ident() {
58 assert!(is_valid_package_ident("foo"));
59 assert!(is_valid_package_ident("foo_bar_123"));
60 assert!(is_valid_package_ident("foo.bar"));
61 assert!(is_valid_package_ident("foo.bar.a123"));
62
63 assert!(!is_valid_package_ident(""));
64 assert!(!is_valid_package_ident("123_foo"));
65 assert!(!is_valid_package_ident("foo-bar"));
66 assert!(!is_valid_package_ident("foo-b\u{00e5}r"));
67 assert!(!is_valid_package_ident("foo.bar.123"));
68 assert!(!is_valid_package_ident(".foo.bar"));
69 assert!(!is_valid_package_ident("foo.bar."));
70 assert!(!is_valid_package_ident("."));
71 assert!(!is_valid_package_ident("foo..bar"));
Mårten Kongstad00cf0452023-05-26 16:48:01 +020072 }
Mårten Kongstad066575b2023-06-07 16:29:25 +020073
74 #[test]
75 fn test_create_device_config_ident() {
76 assert_eq!(
77 "com.foo.bar.some_flag",
78 create_device_config_ident("com.foo.bar", "some_flag").unwrap()
79 );
80 }
Mårten Kongstad00cf0452023-05-26 16:48:01 +020081}