aconfig: remove calls to unwrap (outside tests)
Do not call unwrap outside tests: replace existing uses with Result
return values or infallible alternatives.
Bug: 279485059
Test: atest aconfig.test
Change-Id: Ie5919b704b23a0f96bbef84ffbe9270d667cecd8
diff --git a/tools/aconfig/src/cache.rs b/tools/aconfig/src/cache.rs
index 7b6edc5..5ef1829 100644
--- a/tools/aconfig/src/cache.rs
+++ b/tools/aconfig/src/cache.rs
@@ -119,6 +119,10 @@
pub fn into_iter(self) -> impl Iterator<Item = Item> {
self.items.into_iter()
}
+
+ pub fn namespace(&self) -> &str {
+ &self.namespace
+ }
}
#[cfg(test)]
diff --git a/tools/aconfig/src/codegen_java.rs b/tools/aconfig/src/codegen_java.rs
index bbf1272..26f7fe8 100644
--- a/tools/aconfig/src/codegen_java.rs
+++ b/tools/aconfig/src/codegen_java.rs
@@ -25,9 +25,7 @@
pub fn generate_java_code(cache: &Cache) -> Result<OutputFile> {
let class_elements: Vec<ClassElement> = cache.iter().map(create_class_element).collect();
let readwrite = class_elements.iter().any(|item| item.readwrite);
- let namespace = uppercase_first_letter(
- cache.iter().find(|item| !item.namespace.is_empty()).unwrap().namespace.as_str(),
- );
+ let namespace = uppercase_first_letter(cache.namespace());
let context = Context { namespace: namespace.clone(), readwrite, class_elements };
let mut template = TinyTemplate::new();
template.add_template("java_code_gen", include_str!("../templates/java.template"))?;
diff --git a/tools/aconfig/src/main.rs b/tools/aconfig/src/main.rs
index b9ce259..e1e9166 100644
--- a/tools/aconfig/src/main.rs
+++ b/tools/aconfig/src/main.rs
@@ -18,6 +18,7 @@
use anyhow::{anyhow, ensure, Result};
use clap::{builder::ArgAction, builder::EnumValueParser, Arg, ArgMatches, Command};
+use core::any::Any;
use std::fs;
use std::io;
use std::io::Write;
@@ -30,7 +31,7 @@
mod protos;
use crate::cache::Cache;
-use commands::{Input, OutputFile, Source};
+use commands::{DumpFormat, Input, OutputFile, Source};
fn cli() -> Command {
Command::new("aconfig")
@@ -60,6 +61,15 @@
)
}
+fn get_required_arg<'a, T>(matches: &'a ArgMatches, arg_name: &str) -> Result<&'a T>
+where
+ T: Any + Clone + Send + Sync + 'static,
+{
+ matches
+ .get_one::<T>(arg_name)
+ .ok_or(anyhow!("internal error: required argument '{}' not found", arg_name))
+}
+
fn open_zero_or_more_files(matches: &ArgMatches, arg_name: &str) -> Result<Vec<Input>> {
let mut opened_files = vec![];
for path in matches.get_many::<String>(arg_name).unwrap_or_default() {
@@ -89,30 +99,30 @@
let matches = cli().get_matches();
match matches.subcommand() {
Some(("create-cache", sub_matches)) => {
- let namespace = sub_matches.get_one::<String>("namespace").unwrap();
+ let namespace = get_required_arg::<String>(sub_matches, "namespace")?;
let declarations = open_zero_or_more_files(sub_matches, "declarations")?;
let values = open_zero_or_more_files(sub_matches, "values")?;
let cache = commands::create_cache(namespace, declarations, values)?;
- let path = sub_matches.get_one::<String>("cache").unwrap();
+ let path = get_required_arg::<String>(sub_matches, "cache")?;
let file = fs::File::create(path)?;
cache.write_to_writer(file)?;
}
Some(("create-java-lib", sub_matches)) => {
- let path = sub_matches.get_one::<String>("cache").unwrap();
+ let path = get_required_arg::<String>(sub_matches, "cache")?;
let file = fs::File::open(path)?;
let cache = Cache::read_from_reader(file)?;
- let dir = PathBuf::from(sub_matches.get_one::<String>("out").unwrap());
- let generated_file = commands::generate_code(&cache).unwrap();
+ let dir = PathBuf::from(get_required_arg::<String>(sub_matches, "out")?);
+ let generated_file = commands::generate_code(&cache)?;
write_output_file_realtive_to_dir(&dir, &generated_file)?;
}
Some(("dump", sub_matches)) => {
- let path = sub_matches.get_one::<String>("cache").unwrap();
+ let path = get_required_arg::<String>(sub_matches, "cache")?;
let file = fs::File::open(path)?;
let cache = Cache::read_from_reader(file)?;
- let format = sub_matches.get_one("format").unwrap();
+ let format = get_required_arg::<DumpFormat>(sub_matches, "format")?;
let output = commands::dump_cache(cache, *format)?;
- let path = sub_matches.get_one::<String>("out").unwrap();
- let mut file: Box<dyn Write> = if path == "-" {
+ let path = get_required_arg::<String>(sub_matches, "out")?;
+ let mut file: Box<dyn Write> = if *path == "-" {
Box::new(io::stdout())
} else {
Box::new(fs::File::create(path)?)