mirror of
https://github.com/cubixle/radius-rs.git
synced 2026-04-30 18:08:46 +01:00
Accept multiple dict files at code-generator
This commit is contained in:
@@ -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,11 +112,25 @@ 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();
|
||||
|
||||
@@ -118,12 +138,14 @@ fn main() {
|
||||
.keys()
|
||||
.collect::<HashSet<&String>>();
|
||||
|
||||
let mut w = BufWriter::new(File::create(&matches.free[1]).unwrap());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_header(w: &mut BufWriter<File>) {
|
||||
let code = b"// Code generated by machine generator; DO NOT EDIT.
|
||||
@@ -879,6 +901,8 @@ fn parse_dict_file(dict_file_path: &Path) -> Result<DictParsed, String> {
|
||||
}
|
||||
};
|
||||
|
||||
// TODO
|
||||
|
||||
radius_attributes.push(RadiusAttribute {
|
||||
name: items[1].to_string(),
|
||||
typ: items[2].parse().unwrap(),
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user