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 pub mod cpp;
18 pub mod java;
19 pub mod rust;
20
21 use aconfig_protos::{is_valid_name_ident, is_valid_package_ident};
22 use anyhow::{ensure, Result};
23 use clap::ValueEnum;
24
create_device_config_ident(package: &str, flag_name: &str) -> Result<String>25 pub fn create_device_config_ident(package: &str, flag_name: &str) -> Result<String> {
26 ensure!(is_valid_package_ident(package), "bad package");
27 ensure!(is_valid_name_ident(flag_name), "bad flag name");
28 Ok(format!("{}.{}", package, flag_name))
29 }
30
31 #[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)]
32 pub enum CodegenMode {
33 Exported,
34 ForceReadOnly,
35 Production,
36 Test,
37 }
38
39 impl std::fmt::Display for CodegenMode {
fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result40 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
41 match self {
42 CodegenMode::Exported => write!(f, "exported"),
43 CodegenMode::ForceReadOnly => write!(f, "force-read-only"),
44 CodegenMode::Production => write!(f, "production"),
45 CodegenMode::Test => write!(f, "test"),
46 }
47 }
48 }
49
50 #[cfg(test)]
51 mod tests {
52 use super::*;
53 use aconfig_protos::is_valid_container_ident;
54
55 #[test]
test_is_valid_name_ident()56 fn test_is_valid_name_ident() {
57 assert!(is_valid_name_ident("foo"));
58 assert!(is_valid_name_ident("foo_bar_123"));
59 assert!(is_valid_name_ident("foo_"));
60
61 assert!(!is_valid_name_ident(""));
62 assert!(!is_valid_name_ident("123_foo"));
63 assert!(!is_valid_name_ident("foo-bar"));
64 assert!(!is_valid_name_ident("foo-b\u{00e5}r"));
65 assert!(!is_valid_name_ident("foo__bar"));
66 assert!(!is_valid_name_ident("_foo"));
67 }
68
69 #[test]
test_is_valid_package_ident()70 fn test_is_valid_package_ident() {
71 assert!(is_valid_package_ident("foo.bar"));
72 assert!(is_valid_package_ident("foo.bar_baz"));
73 assert!(is_valid_package_ident("foo.bar.a123"));
74
75 assert!(!is_valid_package_ident("foo_bar_123"));
76 assert!(!is_valid_package_ident("foo"));
77 assert!(!is_valid_package_ident("foo._bar"));
78 assert!(!is_valid_package_ident(""));
79 assert!(!is_valid_package_ident("123_foo"));
80 assert!(!is_valid_package_ident("foo-bar"));
81 assert!(!is_valid_package_ident("foo-b\u{00e5}r"));
82 assert!(!is_valid_package_ident("foo.bar.123"));
83 assert!(!is_valid_package_ident(".foo.bar"));
84 assert!(!is_valid_package_ident("foo.bar."));
85 assert!(!is_valid_package_ident("."));
86 assert!(!is_valid_package_ident(".."));
87 assert!(!is_valid_package_ident("foo..bar"));
88 assert!(!is_valid_package_ident("foo.__bar"));
89 }
90
91 #[test]
test_is_valid_container_ident()92 fn test_is_valid_container_ident() {
93 assert!(is_valid_container_ident("foo.bar"));
94 assert!(is_valid_container_ident("foo.bar_baz"));
95 assert!(is_valid_container_ident("foo.bar.a123"));
96 assert!(is_valid_container_ident("foo"));
97 assert!(is_valid_container_ident("foo_bar_123"));
98
99 assert!(!is_valid_container_ident(""));
100 assert!(!is_valid_container_ident("foo._bar"));
101 assert!(!is_valid_container_ident("_foo"));
102 assert!(!is_valid_container_ident("123_foo"));
103 assert!(!is_valid_container_ident("foo-bar"));
104 assert!(!is_valid_container_ident("foo-b\u{00e5}r"));
105 assert!(!is_valid_container_ident("foo.bar.123"));
106 assert!(!is_valid_container_ident(".foo.bar"));
107 assert!(!is_valid_container_ident("foo.bar."));
108 assert!(!is_valid_container_ident("."));
109 assert!(!is_valid_container_ident(".."));
110 assert!(!is_valid_container_ident("foo..bar"));
111 assert!(!is_valid_container_ident("foo.__bar"));
112 }
113
114 #[test]
test_create_device_config_ident()115 fn test_create_device_config_ident() {
116 assert_eq!(
117 "com.foo.bar.some_flag",
118 create_device_config_ident("com.foo.bar", "some_flag").unwrap()
119 );
120 }
121 }
122