fixing clippy issues

Signed-off-by: Till Wegmueller <toasterson@gmail.com>
This commit is contained in:
Till Wegmueller 2023-03-25 12:47:54 +01:00
parent 563175bbca
commit 5291bb304f
No known key found for this signature in database

View file

@ -5,36 +5,36 @@
// Source https://docs.oracle.com/cd/E23824_01/html/E21796/pkg-5.html // Source https://docs.oracle.com/cd/E23824_01/html/E21796/pkg-5.html
use std::collections::{HashMap};
use std::fs::{read_to_string};
use crate::payload::{Payload, PayloadError};
use std::clone::Clone;
use crate::digest::Digest; use crate::digest::Digest;
use std::str::FromStr; use crate::payload::{Payload, PayloadError};
use std::path::{Path};
use pest::Parser; use pest::Parser;
use pest_derive::Parser; use pest_derive::Parser;
use thiserror::Error; use std::clone::Clone;
use std::collections::HashMap;
use std::fs::read_to_string;
use std::path::Path;
use std::result::Result as StdResult; use std::result::Result as StdResult;
use std::str::FromStr;
use thiserror::Error;
type Result<T> = StdResult<T, ActionError>; type Result<T> = StdResult<T, ActionError>;
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum ActionError { pub enum ActionError {
#[error("payload error: {0}")] #[error(transparent)]
PayloadError(#[from] PayloadError), PayloadError(#[from] PayloadError),
#[error("file action error: {0}")] #[error(transparent)]
FileError(#[from] FileError), FileError(#[from] FileError),
#[error("value {0} is not a boolean")] #[error("value {0} is not a boolean")]
NotBooleanValue(String), NotBooleanValue(String),
#[error("io error: {0}")] #[error(transparent)]
IOError(#[from] std::io::Error), IOError(#[from] std::io::Error),
#[error("parser error: {0}")] #[error(transparent)]
ParserError(#[from] pest::error::Error<Rule>) ParserError(#[from] pest::error::Error<Rule>),
} }
pub trait FacetedAction { pub trait FacetedAction {
@ -55,8 +55,8 @@ pub struct Action {
} }
impl Action { impl Action {
pub fn new(kind: ActionKind) -> Action{ pub fn new(kind: ActionKind) -> Action {
Action{ Action {
kind, kind,
payload: Payload::default(), payload: Payload::default(),
payload_string: String::new(), payload_string: String::new(),
@ -68,11 +68,11 @@ impl Action {
impl FacetedAction for Action { impl FacetedAction for Action {
fn add_facet(&mut self, facet: Facet) -> bool { fn add_facet(&mut self, facet: Facet) -> bool {
return self.facets.insert(facet.name.clone(), facet.clone()) == None self.facets.insert(facet.name.clone(), facet).is_none()
} }
fn remove_facet(&mut self, facet: Facet) -> bool { fn remove_facet(&mut self, facet: Facet) -> bool {
return self.facets.remove(&facet.name) == Some(facet) self.facets.remove(&facet.name) == Some(facet)
} }
} }
@ -93,7 +93,7 @@ impl From<Action> for Dir {
let mut props = act.properties; let mut props = act.properties;
if !act.payload_string.is_empty() { if !act.payload_string.is_empty() {
let p_str = split_property(act.payload_string); let p_str = split_property(act.payload_string);
props.push(Property{ props.push(Property {
key: p_str.0, key: p_str.0,
value: p_str.1, value: p_str.1,
}) })
@ -119,11 +119,11 @@ impl From<Action> for Dir {
impl FacetedAction for Dir { impl FacetedAction for Dir {
fn add_facet(&mut self, facet: Facet) -> bool { fn add_facet(&mut self, facet: Facet) -> bool {
return self.facets.insert(facet.name.clone(), facet.clone()) == None self.facets.insert(facet.name.clone(), facet).is_none()
} }
fn remove_facet(&mut self, facet: Facet) -> bool { fn remove_facet(&mut self, facet: Facet) -> bool {
return self.facets.remove(&facet.name) == Some(facet) self.facets.remove(&facet.name) == Some(facet)
} }
} }
@ -150,7 +150,7 @@ impl File {
Some(str) => { Some(str) => {
f.path = str.to_string(); f.path = str.to_string();
f.payload = Some(Payload::compute_payload(p)?); f.payload = Some(Payload::compute_payload(p)?);
}, }
None => return Err(FileError::FilePathIsNoStringError)?, None => return Err(FileError::FilePathIsNoStringError)?,
} }
@ -159,7 +159,7 @@ impl File {
Ok(f) Ok(f)
} }
pub fn get_original_path(&self) ->Option<String> { pub fn get_original_path(&self) -> Option<String> {
for p in &self.properties { for p in &self.properties {
if p.key.as_str() == "original-path" { if p.key.as_str() == "original-path" {
return Some(p.value.clone()); return Some(p.value.clone());
@ -175,17 +175,17 @@ impl From<Action> for File {
let mut p = act.payload.clone(); let mut p = act.payload.clone();
let mut props = act.properties; let mut props = act.properties;
if !act.payload_string.is_empty() { if !act.payload_string.is_empty() {
if act.payload_string.contains("/") { if act.payload_string.contains('/') {
if act.payload_string.contains("=") { if act.payload_string.contains('=') {
let p_str = split_property(act.payload_string); let p_str = split_property(act.payload_string);
props.push(Property{ props.push(Property {
key: p_str.0, key: p_str.0,
value: p_str.1, value: p_str.1,
}) })
} else { } else {
file.properties.push(Property{ file.properties.push(Property {
key: "original-path".to_string(), key: "original-path".to_string(),
value: act.payload_string.replace("\"", "").replace("\\", "") value: act.payload_string.replace(['\"', '\\'], ""),
}); });
} }
} else { } else {
@ -201,20 +201,26 @@ impl From<Action> for File {
"revert-tag" => file.revert_tag = prop.value, "revert-tag" => file.revert_tag = prop.value,
"original_name" => file.original_name = prop.value, "original_name" => file.original_name = prop.value,
"sysattr" => file.sys_attr = prop.value, "sysattr" => file.sys_attr = prop.value,
"overlay" => file.overlay = match string_to_bool(&prop.value) { "overlay" => {
file.overlay = match string_to_bool(&prop.value) {
Ok(b) => b, Ok(b) => b,
_ => false, _ => false,
}, }
"preserve" => file.preserve = match string_to_bool(&prop.value) { }
"preserve" => {
file.preserve = match string_to_bool(&prop.value) {
Ok(b) => b, Ok(b) => b,
_ => false, _ => false,
}, }
"chash" | "pkg.content-hash" => p.additional_identifiers.push(Digest::from_str(&prop.value).unwrap()), }
"chash" | "pkg.content-hash" => p
.additional_identifiers
.push(Digest::from_str(&prop.value).unwrap()),
_ => { _ => {
if is_facet(prop.key.clone()) { if is_facet(prop.key.clone()) {
file.add_facet(Facet::from_key_value(prop.key, prop.value)); file.add_facet(Facet::from_key_value(prop.key, prop.value));
} else { } else {
file.properties.push(Property{ file.properties.push(Property {
key: prop.key, key: prop.key,
value: prop.value, value: prop.value,
}); });
@ -233,11 +239,11 @@ impl From<Action> for File {
impl FacetedAction for File { impl FacetedAction for File {
fn add_facet(&mut self, facet: Facet) -> bool { fn add_facet(&mut self, facet: Facet) -> bool {
return self.facets.insert(facet.name.clone(), facet.clone()) == None self.facets.insert(facet.name.clone(), facet).is_none()
} }
fn remove_facet(&mut self, facet: Facet) -> bool { fn remove_facet(&mut self, facet: Facet) -> bool {
return self.facets.remove(&facet.name) == Some(facet) self.facets.remove(&facet.name) == Some(facet)
} }
} }
@ -264,7 +270,7 @@ impl From<Action> for Dependency {
let mut props = act.properties; let mut props = act.properties;
if !act.payload_string.is_empty() { if !act.payload_string.is_empty() {
let p_str = split_property(act.payload_string); let p_str = split_property(act.payload_string);
props.push(Property{ props.push(Property {
key: p_str.0, key: p_str.0,
value: p_str.1, value: p_str.1,
}) })
@ -290,11 +296,11 @@ impl From<Action> for Dependency {
impl FacetedAction for Dependency { impl FacetedAction for Dependency {
fn add_facet(&mut self, facet: Facet) -> bool { fn add_facet(&mut self, facet: Facet) -> bool {
return self.facets.insert(facet.name.clone(), facet.clone()) == None self.facets.insert(facet.name.clone(), facet).is_none()
} }
fn remove_facet(&mut self, facet: Facet) -> bool { fn remove_facet(&mut self, facet: Facet) -> bool {
return self.facets.remove(&facet.name) == Some(facet) self.facets.remove(&facet.name) == Some(facet)
} }
} }
@ -306,7 +312,7 @@ pub struct Facet {
impl Facet { impl Facet {
fn from_key_value(key: String, value: String) -> Facet { fn from_key_value(key: String, value: String) -> Facet {
Facet{ Facet {
name: get_facet_key(key), name: get_facet_key(key),
value, value,
} }
@ -326,7 +332,7 @@ impl From<Action> for Attr {
let mut props = act.properties; let mut props = act.properties;
if !act.payload_string.is_empty() { if !act.payload_string.is_empty() {
let p_str = split_property(act.payload_string); let p_str = split_property(act.payload_string);
props.push(Property{ props.push(Property {
key: p_str.0, key: p_str.0,
value: p_str.1, value: p_str.1,
}) })
@ -336,10 +342,13 @@ impl From<Action> for Attr {
"name" => attr.key = prop.value, "name" => attr.key = prop.value,
"value" => attr.values.push(prop.value), "value" => attr.values.push(prop.value),
_ => { _ => {
attr.properties.insert(prop.key.clone(), Property{ attr.properties.insert(
prop.key.clone(),
Property {
key: prop.key, key: prop.key,
value: prop.value, value: prop.value,
}); },
);
} }
} }
} }
@ -360,13 +369,15 @@ impl From<Action> for License {
license.payload = act.payload_string; license.payload = act.payload_string;
} }
for prop in act.properties { for prop in act.properties {
match prop.key.as_str() { let key = prop.key.as_str();
_ => { {
license.properties.insert(prop.key.clone(), Property{ license.properties.insert(
key.to_owned(),
Property {
key: prop.key, key: prop.key,
value: prop.value, value: prop.value,
}); },
} );
} }
} }
license license
@ -386,7 +397,7 @@ impl From<Action> for Link {
let mut props = act.properties; let mut props = act.properties;
if !act.payload_string.is_empty() { if !act.payload_string.is_empty() {
let p_str = split_property(act.payload_string); let p_str = split_property(act.payload_string);
props.push(Property{ props.push(Property {
key: p_str.0, key: p_str.0,
value: p_str.1, value: p_str.1,
}) })
@ -396,10 +407,13 @@ impl From<Action> for Link {
"path" => link.path = prop.value, "path" => link.path = prop.value,
"target" => link.target = prop.value, "target" => link.target = prop.value,
_ => { _ => {
link.properties.insert(prop.key.clone(), Property{ link.properties.insert(
prop.key.clone(),
Property {
key: prop.key, key: prop.key,
value: prop.value, value: prop.value,
}); },
);
} }
} }
} }
@ -420,19 +434,19 @@ pub struct Manifest {
pub files: Vec<File>, pub files: Vec<File>,
pub dependencies: Vec<Dependency>, pub dependencies: Vec<Dependency>,
pub licenses: Vec<License>, pub licenses: Vec<License>,
pub links: Vec<Link> pub links: Vec<Link>,
} }
impl Manifest { impl Manifest {
pub fn new() -> Manifest { pub fn new() -> Manifest {
return Manifest { Manifest {
attributes: Vec::new(), attributes: Vec::new(),
directories: Vec::new(), directories: Vec::new(),
files: Vec::new(), files: Vec::new(),
dependencies: Vec::new(), dependencies: Vec::new(),
licenses: Vec::new(), licenses: Vec::new(),
links: Vec::new(), links: Vec::new(),
}; }
} }
pub fn add_file(&mut self, f: File) { pub fn add_file(&mut self, f: File) {
@ -474,9 +488,9 @@ impl Manifest {
ActionKind::Transform => { ActionKind::Transform => {
todo!() todo!()
} }
ActionKind::Unknown{action} => { ActionKind::Unknown { action } => {
panic!("action {:?} not known", action) panic!("action {:?} not known", action)
}, }
} }
} }
@ -503,20 +517,19 @@ impl Manifest {
act.kind = get_action_kind(action.as_str()); act.kind = get_action_kind(action.as_str());
} }
Rule::payload => { Rule::payload => {
act.payload_string = action.as_str().clone().into(); act.payload_string = action.as_str().to_owned();
} }
Rule::property => { Rule::property => {
let mut property = Property::default(); let mut property = Property::default();
for prop in action.clone().into_inner() { for prop in action.clone().into_inner() {
match prop.as_rule() { match prop.as_rule() {
Rule::property_name => { Rule::property_name => {
property.key = prop.as_str().clone().into(); property.key = prop.as_str().to_owned();
} }
Rule::property_value => { Rule::property_value => {
let str_val: String = prop.as_str().clone().into(); let str_val: String = prop.as_str().to_owned();
property.value = str_val property.value = str_val
.replace("\"", "") .replace(['\"', '\\'], "");
.replace("\\", "");
} }
_ => panic!("unexpected rule {:?} inside action expected property_name or property_value", prop.as_rule()) _ => panic!("unexpected rule {:?} inside action expected property_name or property_value", prop.as_rule())
} }
@ -531,12 +544,18 @@ impl Manifest {
} }
Rule::EOI => (), Rule::EOI => (),
Rule::transform => (), Rule::transform => (),
_ => panic!("unexpected rule {:?} inside manifest expected action", manifest.as_rule()), _ => panic!(
"unexpected rule {:?} inside manifest expected action",
manifest.as_rule()
),
} }
} }
} }
Rule::WHITESPACE => (), Rule::WHITESPACE => (),
_ => panic!("unexpected rule {:?} inside pair expected manifest", p.as_rule()), _ => panic!(
"unexpected rule {:?} inside pair expected manifest",
p.as_rule()
),
} }
} }
@ -556,22 +575,23 @@ pub enum ActionKind {
License, License,
Link, Link,
Legacy, Legacy,
Unknown{action: String}, Unknown { action: String },
Transform, Transform,
} }
impl Default for ActionKind { impl Default for ActionKind {
fn default() -> Self { ActionKind::Unknown {action: String::new()} } fn default() -> Self {
ActionKind::Unknown {
action: String::new(),
}
}
} }
//TODO Multierror and no failure for these cases //TODO Multierror and no failure for these cases
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum ManifestError { pub enum ManifestError {
#[error("unknown action {action:?} at line {line:?}")] #[error("unknown action {action:?} at line {line:?}")]
UnknownAction { UnknownAction { line: usize, action: String },
line: usize,
action: String,
},
#[error("action string \"{action:?}\" at line {line:?} is invalid: {message:?}")] #[error("action string \"{action:?}\" at line {line:?} is invalid: {message:?}")]
InvalidAction { InvalidAction {
line: usize, line: usize,
@ -585,7 +605,7 @@ pub enum ManifestError {
struct ManifestParser; struct ManifestParser;
fn get_action_kind(act: &str) -> ActionKind { fn get_action_kind(act: &str) -> ActionKind {
return match act { match act {
"set" => ActionKind::Attr, "set" => ActionKind::Attr,
"depend" => ActionKind::Dependency, "depend" => ActionKind::Dependency,
"dir" => ActionKind::Dir, "dir" => ActionKind::Dir,
@ -598,7 +618,7 @@ fn get_action_kind(act: &str) -> ActionKind {
"user" => ActionKind::User, "user" => ActionKind::User,
"legacy" => ActionKind::Legacy, "legacy" => ActionKind::Legacy,
"<transform" => ActionKind::Transform, "<transform" => ActionKind::Transform,
_ => ActionKind::Unknown{action: act.into()}, _ => ActionKind::Unknown { action: act.into() },
} }
} }
@ -607,26 +627,22 @@ fn is_facet(s: String) -> bool {
} }
fn get_facet_key(facet_string: String) -> String { fn get_facet_key(facet_string: String) -> String {
match facet_string.find(".") { match facet_string.find('.') {
Some(idx) => { Some(idx) => facet_string.clone().split_off(idx + 1),
facet_string.clone().split_off(idx+1) None => facet_string.clone(),
},
None => facet_string.clone()
} }
} }
fn split_property(property_string: String) -> (String, String) { fn split_property(property_string: String) -> (String, String) {
match property_string.find("=") { match property_string.find('=') {
Some(_) => { Some(_) => {
let v :Vec <_> = property_string.split("=").collect(); let v: Vec<_> = property_string.split('=').collect();
( (
String::from(v[0]), String::from(v[0]),
String::from(v[1]) String::from(v[1]).replace(['\"', '\\'], ""),
.replace("\"", "")
.replace("\\", "")
) )
}, }
None => (property_string.clone(), String::new()) None => (property_string.clone(), String::new()),
} }
} }
@ -636,6 +652,6 @@ fn string_to_bool(orig: &str) -> Result<bool> {
"false" => Ok(false), "false" => Ok(false),
"t" => Ok(true), "t" => Ok(true),
"f" => Ok(false), "f" => Ok(false),
_ => Err(ActionError::NotBooleanValue(orig.clone().to_owned())) _ => Err(ActionError::NotBooleanValue(orig.to_owned())),
} }
} }