refactor(sysctl): update the from implementations of section

This commit is contained in:
Orhun Parmaksız 2022-01-24 19:27:34 +03:00
parent 70530961c2
commit de08836906
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
2 changed files with 21 additions and 4 deletions

View file

@ -39,7 +39,7 @@ impl<'a> TryFrom<&'a Ctl> for Parameter {
.description()
.ok()
.and_then(|v| (v == "[N/A]").then(|| None)?),
section: Section::from(ctl.name()?),
section: Section::from_name(ctl.name()?),
docs_path: PathBuf::new(),
docs_title: String::new(),
})

View file

@ -23,10 +23,22 @@ pub enum Section {
Unknown,
}
impl Section {
/// Returns the section of the given parameter name.
pub fn from_name(name: String) -> Self {
for section in Self::variants() {
if name.starts_with(&format!("{}.", section)) {
return *section;
}
}
Self::Unknown
}
}
impl From<String> for Section {
fn from(value: String) -> Self {
for section in Self::variants() {
if value.starts_with(&format!("{}.", section)) {
if value.to_lowercase() == section.to_string() {
return *section;
}
}
@ -77,8 +89,13 @@ mod tests {
#[test]
fn test_sysctl_section() {
assert_eq!(Section::Net, Section::from(String::from("net.xyz")));
assert_eq!(Section::User, Section::from(String::from("user.aaa.bbb")));
assert_eq!(Section::Net, Section::from_name(String::from("net.xyz")));
assert_eq!(
Section::User,
Section::from_name(String::from("user.aaa.bbb"))
);
assert_eq!(Section::Unknown, Section::from_name(String::from("test")));
assert_eq!(Section::Sunrpc, Section::from(String::from("sunrpc")));
assert_eq!(Section::Unknown, Section::from(String::from("test")));
assert_eq!(Section::Vm, Section::from(Path::new("/etc/vm.txt")));
assert_eq!(