Accept multiple dict files at code-generator

This commit is contained in:
moznion
2020-12-09 23:43:24 +09:00
parent cda352e87f
commit 0064a28520
2 changed files with 43 additions and 23 deletions
+40 -16
View File
@@ -98,6 +98,12 @@ fn main() {
let mut opts = Options::new(); let mut opts = Options::new();
opts.optflag("h", "help", "print this help menu"); opts.optflag("h", "help", "print this help menu");
opts.optopt(
"o",
"out-dir",
"[mandatory] a directory to out the generated code",
"/path/to/out/",
);
let matches = opts let matches = opts
.parse(&args[1..]) .parse(&args[1..])
.unwrap_or_else(|f| panic!(f.to_string())); .unwrap_or_else(|f| panic!(f.to_string()));
@@ -106,23 +112,39 @@ fn main() {
print_usage(&program, &opts); print_usage(&program, &opts);
} }
let dict_file_path = Path::new(&matches.free[0]); let out_dir_str = match matches.opt_str("o") {
if !dict_file_path.exists() { Some(o) => o,
panic!("no such dictionary file => {}", &matches.free[0]); None => panic!("mandatory parameter `-o` (`--out-dir`) is missing"),
};
let out_dir = Path::new(&out_dir_str);
let dict_file_paths: Vec<&Path> = matches
.free
.iter()
.map(|file_path_str| Path::new(file_path_str))
.filter(|path| {
if !path.exists() || !path.is_file() {
panic!("no such dictionary file => {}", path.to_str().unwrap());
}
true
})
.collect();
for dict_file_path in dict_file_paths {
let (radius_attributes, radius_attribute_to_values_map) =
parse_dict_file(dict_file_path).unwrap();
let value_defined_attributes_set = radius_attribute_to_values_map
.keys()
.collect::<HashSet<&String>>();
let rfc_name = dict_file_path.extension().unwrap().to_str().unwrap();
let mut w = BufWriter::new(File::create(out_dir.join(format!("{}.rs", rfc_name))).unwrap());
generate_header(&mut w);
generate_values_code(&mut w, &radius_attribute_to_values_map);
generate_attributes_code(&mut w, &radius_attributes, &value_defined_attributes_set);
} }
let (radius_attributes, radius_attribute_to_values_map) =
parse_dict_file(dict_file_path).unwrap();
let value_defined_attributes_set = radius_attribute_to_values_map
.keys()
.collect::<HashSet<&String>>();
let mut w = BufWriter::new(File::create(&matches.free[1]).unwrap());
generate_header(&mut w);
generate_values_code(&mut w, &radius_attribute_to_values_map);
generate_attributes_code(&mut w, &radius_attributes, &value_defined_attributes_set);
} }
fn generate_header(w: &mut BufWriter<File>) { fn generate_header(w: &mut BufWriter<File>) {
@@ -879,6 +901,8 @@ fn parse_dict_file(dict_file_path: &Path) -> Result<DictParsed, String> {
} }
}; };
// TODO
radius_attributes.push(RadiusAttribute { radius_attributes.push(RadiusAttribute {
name: items[1].to_string(), name: items[1].to_string(),
typ: items[2].parse().unwrap(), typ: items[2].parse().unwrap(),
+3 -7
View File
@@ -7,6 +7,7 @@ DICTS_DIR="${REPO_ROOT}/dicts"
SRC_DIR="${REPO_ROOT}/radius/src" SRC_DIR="${REPO_ROOT}/radius/src"
DICTS=$(ls "$DICTS_DIR") DICTS=$(ls "$DICTS_DIR")
DICT_FILES=()
# shellcheck disable=SC2068 # shellcheck disable=SC2068
for DICT in ${DICTS[@]}; do for DICT in ${DICTS[@]}; do
@@ -14,17 +15,12 @@ for DICT in ${DICTS[@]}; do
DICT_FILE="${DICTS_DIR}/dictionary.${DICT_NAME}" DICT_FILE="${DICTS_DIR}/dictionary.${DICT_NAME}"
if [ -f "$DICT_FILE" ]; then if [ -f "$DICT_FILE" ]; then
cat /dev/null > "${SRC_DIR}/${DICT_NAME}.rs" cat /dev/null > "${SRC_DIR}/${DICT_NAME}.rs"
DICT_FILES+=("$DICT_FILE")
fi fi
done done
# shellcheck disable=SC2068 # shellcheck disable=SC2068
for DICT in ${DICTS[@]}; do cargo run --bin code-generator -- --out-dir="${SRC_DIR}/" ${DICT_FILES[@]}
DICT_NAME="${DICT##*.}"
DICT_FILE="${DICTS_DIR}/dictionary.${DICT_NAME}"
if [ -f "$DICT_FILE" ]; then
cargo run --bin code-generator "$DICT_FILE" "${SRC_DIR}/${DICT_NAME}.rs"
fi
done
cargo fix --allow-dirty --allow-staged cargo fix --allow-dirty --allow-staged
cargo fmt cargo fmt