From f33df3d0abe3bf0198f3b07ac4f4c4ea7173d47f Mon Sep 17 00:00:00 2001 From: Till Wegmueller Date: Mon, 18 May 2020 10:32:16 +0200 Subject: [PATCH] Linting. --- src/actions/mod.rs | 42 ++++++++++++++++++++---------------------- src/lib.rs | 19 +++++++------------ 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/actions/mod.rs b/src/actions/mod.rs index f033d6a..16cd064 100644 --- a/src/actions/mod.rs +++ b/src/actions/mod.rs @@ -3,14 +3,13 @@ // MPL was not distributed with this file, You can // obtain one at https://mozilla.org/MPL/2.0/. +use regex::Regex; +use std::collections::HashSet; +use std::error; +use std::fmt; use std::fs::File; use std::io::BufRead; use std::io::BufReader; -use std::error; -use std::fmt; -use regex::Regex; -use regex::RegexSet; -use std::collections::HashSet; pub struct Dir { pub path: String, @@ -37,7 +36,7 @@ pub struct Manifest { impl Manifest { pub fn new() -> Manifest { - return Manifest{ + return Manifest { attributes: Vec::new(), }; } @@ -67,8 +66,7 @@ pub enum ManifestError { impl fmt::Display for ManifestError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - ManifestError::EmptyVec => - write!(f, "please use a vector with at least one element"), + ManifestError::EmptyVec => write!(f, "please use a vector with at least one element"), // This is a wrapper, so defer to the underlying types' implementation of `fmt`. ManifestError::Read(ref e) => e.fmt(f), ManifestError::Regex(ref e) => e.fmt(f), @@ -96,18 +94,18 @@ pub fn parse_manifest_file(filename: String) -> Result Err(e) => return Err(ManifestError::Read(e)), }; let file = BufReader::new(&f); - for lineRead in file.lines() { - let line = match lineRead { + for line_read in file.lines() { + let line = match line_read { Ok(l) => l, Err(e) => return Err(ManifestError::Read(e)), }; if is_attr_action(&line) { match parse_attr_action(line) { Ok(attr) => manifest.attributes.push(attr), - Err(e) => return Err(e) + Err(e) => return Err(e), } } - } + } return Ok(manifest); } @@ -115,13 +113,13 @@ pub fn parse_manifest_string(manifest: String) -> Result m.attributes.push(attr), - Err(e) => return Err(e) + Err(e) => return Err(e), }; } } - return Ok(m) + return Ok(m); } fn is_attr_action(line: &String) -> bool { @@ -135,7 +133,7 @@ pub fn parse_attr_action(line: String) -> Result { //Todo move regex initialisation out of for loop into static area let name_regex = match Regex::new(r"name=([^ ]+) value=") { Ok(re) => re, - Err(e) => return Err(ManifestError::Regex(e)) + Err(e) => return Err(ManifestError::Regex(e)), }; let mut key = String::new(); for cap in name_regex.captures_iter(line.trim_start()) { @@ -156,12 +154,12 @@ pub fn parse_attr_action(line: String) -> Result { let mut properties = HashSet::new(); let optionals_regex_no_quotes = match Regex::new(r#"([^ ]+)=([^"][^ ]+[^"])"#) { Ok(re) => re, - Err(e) => return Err(ManifestError::Regex(e)) + Err(e) => return Err(ManifestError::Regex(e)), }; let optionals_regex_quotes = match Regex::new(r#"([^ ]+)=([^"][^ ]+[^"])"#) { Ok(re) => re, - Err(e) => return Err(ManifestError::Regex(e)) + Err(e) => return Err(ManifestError::Regex(e)), }; for cap in value_no_space_regex.captures_iter(line.trim_start()) { @@ -177,7 +175,7 @@ pub fn parse_attr_action(line: String) -> Result { continue; } - properties.insert(Property{ + properties.insert(Property { key: String::from(cap[1].trim()), value: String::from(cap[2].trim()), }); @@ -188,15 +186,15 @@ pub fn parse_attr_action(line: String) -> Result { continue; } - properties.insert(Property{ + properties.insert(Property { key: String::from(cap[1].trim()), value: String::from(cap[2].trim()), }); } - Ok(Attr{ + Ok(Attr { key, values, properties, }) -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index dbd2d73..345c6aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,11 +8,8 @@ mod actions; #[cfg(test)] mod tests { - use crate::actions::{parse_manifest_string, Attr}; use crate::actions::Manifest; - use crate::actions::ManifestError; - use std::error; - use std::fmt; + use crate::actions::{parse_manifest_string, Attr}; use std::collections::HashSet; #[test] @@ -29,7 +26,7 @@ mod tests { set name=org.opensolaris.consolidation value=userland set name=com.oracle.info.version value=1.18.0 set name=variant.arch value=i386"); - let testResults = vec![ + let test_results = vec![ Attr{ key: String::from("pkg.fmri"), values: vec![String::from("pkg://openindiana.org/web/server/nginx@1.18.0,5.11-2020.0.1.0:20200421T195136Z")], @@ -92,19 +89,17 @@ mod tests { } ]; - let mut manifest = Manifest::new(); - match parse_manifest_string(manifest_string){ + let mut manifest = Manifest::new(); + match parse_manifest_string(manifest_string) { Ok(m) => manifest = m, - Err(_) => assert!(false, "caught error") + Err(_) => assert!(false, "caught error"), }; assert_eq!(manifest.attributes.len(), 12); for (pos, attr) in manifest.attributes.iter().enumerate() { - assert_eq!(attr.key, testResults[pos].key); + assert_eq!(attr.key, test_results[pos].key); for (vpos, val) in attr.values.iter().enumerate() { - assert_eq!(val, &testResults[pos].values[vpos]); + assert_eq!(val, &test_results[pos].values[vpos]); } } } - } -