diff --git a/code-generator/src/main.rs b/code-generator/src/main.rs index cd0b326..ea553ff 100644 --- a/code-generator/src/main.rs +++ b/code-generator/src/main.rs @@ -98,6 +98,12 @@ fn main() { let mut opts = Options::new(); 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 .parse(&args[1..]) .unwrap_or_else(|f| panic!(f.to_string())); @@ -106,23 +112,39 @@ fn main() { print_usage(&program, &opts); } - let dict_file_path = Path::new(&matches.free[0]); - if !dict_file_path.exists() { - panic!("no such dictionary file => {}", &matches.free[0]); + let out_dir_str = match matches.opt_str("o") { + Some(o) => o, + 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::>(); + + 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::>(); - - 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) { @@ -879,6 +901,8 @@ fn parse_dict_file(dict_file_path: &Path) -> Result { } }; + // TODO + radius_attributes.push(RadiusAttribute { name: items[1].to_string(), typ: items[2].parse().unwrap(), diff --git a/scripts/generate-code.sh b/scripts/generate-code.sh index 4b372d7..f8a72ca 100755 --- a/scripts/generate-code.sh +++ b/scripts/generate-code.sh @@ -7,6 +7,7 @@ DICTS_DIR="${REPO_ROOT}/dicts" SRC_DIR="${REPO_ROOT}/radius/src" DICTS=$(ls "$DICTS_DIR") +DICT_FILES=() # shellcheck disable=SC2068 for DICT in ${DICTS[@]}; do @@ -14,17 +15,12 @@ for DICT in ${DICTS[@]}; do DICT_FILE="${DICTS_DIR}/dictionary.${DICT_NAME}" if [ -f "$DICT_FILE" ]; then cat /dev/null > "${SRC_DIR}/${DICT_NAME}.rs" + DICT_FILES+=("$DICT_FILE") fi done # shellcheck disable=SC2068 -for DICT in ${DICTS[@]}; do - 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 run --bin code-generator -- --out-dir="${SRC_DIR}/" ${DICT_FILES[@]} cargo fix --allow-dirty --allow-staged cargo fmt