Skip to content

Refactor module parsing function. #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: rust
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions rust/macros/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,17 @@ impl ModuleInfo {
fn parse(it: &mut token_stream::IntoIter) -> Self {
let mut info = ModuleInfo::default();

const EXPECTED_KEYS: &[&str] = &[
"type",
"name",
"author",
"description",
"license",
"alias",
"alias_rtnl_link",
"params",
const ALL_KEYS: &[(bool, &str)] = &[
//required: bool, key: &str
(true, "type"),
(true, "name"),
(false, "author"),
(false, "description"),
(true, "license"),
(false, "alias"),
(false, "alias_rtnl_link"),
(false, "params"),
];
const REQUIRED_KEYS: &[&str] = &["type", "name", "license"];
let mut seen_keys = Vec::new();

loop {
Expand Down Expand Up @@ -354,8 +354,9 @@ impl ModuleInfo {
}
"params" => info.params = Some(expect_group(it)),
_ => panic!(
"Unknown key \"{}\". Valid keys are: {:?}.",
key, EXPECTED_KEYS
"Unknown key \"{}\". Valid keys are: \"{}\".",
key,
ALL_KEYS.iter().map(|e| e.1).collect::<Vec<_>>().join(", ")
),
}

Expand All @@ -366,16 +367,15 @@ impl ModuleInfo {

expect_end(it);

for key in REQUIRED_KEYS {
if !seen_keys.iter().any(|e| e == key) {
panic!("Missing required key \"{}\".", key);
let mut ordered_keys: Vec<&str> = Vec::new();
for key in ALL_KEYS {
let has_key = seen_keys.iter().any(|e| e == key.1);
if key.0 && !has_key {
panic!("Missing required key \"{}\".", key.1);
}
}

let mut ordered_keys: Vec<&str> = Vec::new();
for key in EXPECTED_KEYS {
if seen_keys.iter().any(|e| e == key) {
ordered_keys.push(key);
if has_key {
ordered_keys.push(&key.1);
}
}

Expand Down