Skip to content

Commit

Permalink
chore(lint): cargo clippy fix
Browse files Browse the repository at this point in the history
Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>
  • Loading branch information
chawyehsu committed Jul 24, 2023
1 parent 0134373 commit a544d1a
Show file tree
Hide file tree
Showing 20 changed files with 154 additions and 183 deletions.
30 changes: 11 additions & 19 deletions crates/libscoop/src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Bucket {
.map(|n| n.to_str().unwrap().to_string())
.unwrap();
if !path.exists() {
return Err(Error::BucketNotFound(name.clone()));
return Err(Error::BucketNotFound(name));
}

let mut remote_url = None;
Expand All @@ -58,12 +58,10 @@ impl Bucket {
let mut dtype = BucketDirectoryType::V2;
let entries = nested_dir.read_dir()?;

for entry in entries {
if let Ok(entry) = entry {
if entry.path().is_dir() {
dtype = BucketDirectoryType::V3;
break;
}
for entry in entries.flatten() {
if entry.path().is_dir() {
dtype = BucketDirectoryType::V3;
break;
}
}
dtype
Expand Down Expand Up @@ -129,8 +127,7 @@ impl Bucket {
let json_files = match self.dtype {
BucketDirectoryType::V1 => {
let path = self.path.as_path();
let entries = path
.read_dir()?
path.read_dir()?
.par_bridge()
.filter_map(std::io::Result::ok)
.filter(|de| {
Expand All @@ -140,13 +137,11 @@ impl Bucket {
path.is_file() && name.ends_with(".json") && name != "package.json"
})
.map(|de| de.path())
.collect::<Vec<_>>();
entries
.collect::<Vec<_>>()
}
BucketDirectoryType::V2 => {
let path = self.path.join("bucket");
let entries = path
.read_dir()?
path.read_dir()?
.par_bridge()
.filter_map(std::io::Result::ok)
.filter(|de| {
Expand All @@ -155,13 +150,11 @@ impl Bucket {
path.is_file() && name.ends_with(".json")
})
.map(|de| de.path())
.collect::<Vec<_>>();
entries
.collect::<Vec<_>>()
}
BucketDirectoryType::V3 => {
let path = self.path.join("bucket");
let entries = path
.read_dir()?
path.read_dir()?
.par_bridge()
.filter_map(std::io::Result::ok)
.filter(|de| {
Expand All @@ -185,8 +178,7 @@ impl Bucket {
Ok(entries)
})
.flatten()
.collect::<Vec<_>>();
entries
.collect::<Vec<_>>()
}
};
Ok(json_files)
Expand Down
8 changes: 4 additions & 4 deletions crates/libscoop/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl CacheFile {
pub fn from(path: PathBuf) -> Fallible<CacheFile> {
let text = path.file_name().unwrap().to_str().unwrap();
match REGEX_CACHE_FILE.is_match(text) {
false => Err(Error::InvalidCacheFile { path }.into()),
false => Err(Error::InvalidCacheFile { path }),
true => Ok(CacheFile { path }),
}
}
Expand All @@ -27,19 +27,19 @@ impl CacheFile {
/// Get file name of this cache file
#[inline]
pub fn file_name(&self) -> &str {
&self.path.file_name().unwrap().to_str().unwrap()
self.path.file_name().unwrap().to_str().unwrap()
}

/// Get package name of this cache file
#[inline]
pub fn package_name(&self) -> &str {
self.file_name().split_once("#").map(|s| s.0).unwrap()
self.file_name().split_once('#').map(|s| s.0).unwrap()
}

/// Get version of this cache file
#[inline]
pub fn version(&self) -> &str {
self.file_name().splitn(3, "#").collect::<Vec<_>>()[1]
self.file_name().splitn(3, '#').collect::<Vec<_>>()[1]
}

/// Get the tmp path of this cache file
Expand Down
10 changes: 5 additions & 5 deletions crates/libscoop/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ impl Config {
true => self.use_external_7zip = None,
false => match value.parse::<bool>() {
Ok(value) => self.use_external_7zip = Some(value),
Err(_) => return Err(Error::InvalidConfigValue(value.to_owned()).into()),
Err(_) => return Err(Error::InvalidConfigValue(value.to_owned())),
},
},
"aria2_enabled" | "aria2-enabled" => match is_unset {
true => self.aria2_enabled = None,
false => match value.parse::<bool>() {
Ok(value) => self.aria2_enabled = Some(value),
Err(_) => return Err(Error::InvalidConfigValue(value.to_owned()).into()),
Err(_) => return Err(Error::InvalidConfigValue(value.to_owned())),
},
},
"cat_style" => {
Expand All @@ -178,17 +178,17 @@ impl Config {
true => self.use_lessmsi = None,
false => match value.parse::<bool>() {
Ok(value) => self.use_lessmsi = Some(value),
Err(_) => return Err(Error::InvalidConfigValue(value.to_owned()).into()),
Err(_) => return Err(Error::InvalidConfigValue(value.to_owned())),
},
},
"proxy" => match value {
"" | "none" => self.proxy = None,
_ => self.proxy = Some(value.to_string()),
},
key => return Err(Error::InvalidConfigKey(key.to_owned()).into()),
key => return Err(Error::InvalidConfigKey(key.to_owned())),
}

Ok(self.commit()?)
self.commit()
}

/// Commit config changes and save to the config file
Expand Down
2 changes: 1 addition & 1 deletion crates/libscoop/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static REGEX_ARCHIVE_ZSTD: Lazy<Regex> = Lazy::new(|| {

pub static SPDX_LIST: Lazy<HashSet<&str>> = Lazy::new(|| {
// Reference: https://github.com/spdx/license-list-data
const ID_LIST: [&'static str; 458] = [
const ID_LIST: [&str; 458] = [
"0BSD",
"AAL",
"ADSL",
Expand Down
6 changes: 6 additions & 0 deletions crates/libscoop/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ impl EventBus {
}
}

impl Default for EventBus {
fn default() -> Self {
Self::new()
}
}

/// Event that may be emitted during the execution of operations.
#[derive(Debug, Clone)]
#[non_exhaustive]
Expand Down
24 changes: 12 additions & 12 deletions crates/libscoop/src/internal/dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ where
.filter(|(_, deps)| deps.len() == 0)
.map(|(node, _)| node)
.next()
.map(|node| node.clone());
.cloned();
if node.is_some() {
Self::__unregister(&mut self.nodes, node.as_ref().unwrap());
}
Expand All @@ -169,9 +169,9 @@ where
/// graph.
pub fn check(&self) -> Result<()> {
let mut nodes = self.nodes.clone();
while nodes.len() > 0 {
while !nodes.is_empty() {
let step = Self::__step(&mut nodes);
if step.len() == 0 {
if step.is_empty() {
return Err(CyclicError::from(&nodes));
}
}
Expand All @@ -182,9 +182,9 @@ where
/// be returned when cyclic dependency is detected.
pub fn walk(&mut self) -> Result<Vec<Vec<T>>> {
let mut res = vec![];
while self.nodes.len() > 0 {
while !self.nodes.is_empty() {
let step = self.step();
if step.len() == 0 {
if step.is_empty() {
return Err(CyclicError::from(&self.nodes));
}
res.push(step);
Expand Down Expand Up @@ -216,25 +216,25 @@ where
#[inline]
fn __register(nodes: &mut Nodes<T>, node: T, dep_node: Option<T>) {
// register node
match nodes.entry(node.clone()) {
match nodes.entry(node) {
Entry::Vacant(e) => {
let mut dep = Dependencies::new();
if let Some(dep_node) = &dep_node {
drop(dep.insert(dep_node.clone()));
dep.insert(dep_node.clone());
}
drop(e.insert(dep));
e.insert(dep);
}
Entry::Occupied(e) => {
if let Some(dep_node) = &dep_node {
drop(e.into_mut().insert(dep_node.clone()));
e.into_mut().insert(dep_node.clone());
}
}
}
// register dep_node
if let Some(dep_node) = dep_node {
match nodes.entry(dep_node) {
Entry::Vacant(e) => {
drop(e.insert(Dependencies::new()));
e.insert(Dependencies::new());
}
Entry::Occupied(_) => {
// no-op
Expand All @@ -245,9 +245,9 @@ where

#[inline]
fn __unregister(nodes: &mut Nodes<T>, node: &T) {
drop(nodes.remove(node));
nodes.remove(node);
nodes.iter_mut().for_each(|(_, deps)| {
drop(deps.remove(node));
deps.remove(node);
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/libscoop/src/internal/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn fetch_options(proxy: Option<&str>) -> FetchOptions<'static> {
cb.credentials(
move |url, username, cred| -> Result<git2::Cred, git2::Error> {
let user = username.unwrap_or("git");
let ref cfg = git2::Config::open_default()?;
let cfg = &(git2::Config::open_default()?);

if cred.contains(CredentialType::USERNAME) {
git2::Cred::username(user)
Expand All @@ -26,8 +26,8 @@ fn fetch_options(proxy: Option<&str>) -> FetchOptions<'static> {

fo.remote_callbacks(cb);

if proxy.is_some() {
let mut proxy = proxy.unwrap().to_owned();
if let Some(proxy) = proxy {
let mut proxy = proxy.to_owned();

if !(proxy.starts_with("http") || proxy.starts_with("socks")) {
proxy.insert_str(0, "http://");
Expand Down
74 changes: 37 additions & 37 deletions crates/libscoop/src/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn os_is_arch64() -> bool {
/// Check if a given executable is available on the system.
pub fn is_program_available(exe: &str) -> bool {
if let Ok(path) = std::env::var("PATH") {
for p in path.split(";") {
for p in path.split(';') {
let path = Path::new(p).join(exe);
if std::fs::metadata(path).is_ok() {
return true;
Expand All @@ -51,46 +51,41 @@ pub fn compare_versions<S: AsRef<str>>(ver_a: S, ver_b: S) -> std::cmp::Ordering
// ver_a_parsed, ver_b_parsed
// );

loop {
match ver_a_parsed.next() {
Some(a_part) => {
match ver_b_parsed.next() {
Some(b_part) => {
if a_part.parse::<u32>().is_ok() {
if b_part.parse::<u32>().is_ok() {
let a_part_num = a_part.parse::<u32>().unwrap();
let b_part_num = b_part.parse::<u32>().unwrap();
for a_part in ver_a_parsed {
match ver_b_parsed.next() {
Some(b_part) => {
if a_part.parse::<u32>().is_ok() {
if b_part.parse::<u32>().is_ok() {
let a_part_num = a_part.parse::<u32>().unwrap();
let b_part_num = b_part.parse::<u32>().unwrap();

match a_part_num {
n if n < b_part_num => return std::cmp::Ordering::Less,
n if n > b_part_num => return std::cmp::Ordering::Greater,
_ => continue,
}
}

// I guess this should be ok for cases like: 1.2.0 vs. 1.2-rc4
// num to text comparsion is an interesting branch.
return std::cmp::Ordering::Greater;
match a_part_num {
n if n < b_part_num => return std::cmp::Ordering::Less,
n if n > b_part_num => return std::cmp::Ordering::Greater,
_ => continue,
}

// FIXME: text to text comparsion is the hardest part,
// I just return `Ordering::Equal` currently...
}
None => {
if a_part.parse::<u32>().is_ok() {
let a_part_num = a_part.parse::<u32>().unwrap();
if 0 == a_part_num {
continue;
}
} else {
return std::cmp::Ordering::Less;
}

return std::cmp::Ordering::Greater;
// I guess this should be ok for cases like: 1.2.0 vs. 1.2-rc4
// num to text comparsion is an interesting branch.
return std::cmp::Ordering::Greater;
}

// FIXME: text to text comparsion is the hardest part,
// I just return `Ordering::Equal` currently...
}
None => {
if a_part.parse::<u32>().is_ok() {
let a_part_num = a_part.parse::<u32>().unwrap();
if 0 == a_part_num {
continue;
}
} else {
return std::cmp::Ordering::Less;
}

return std::cmp::Ordering::Greater;
}
None => break,
}
}

Expand All @@ -108,13 +103,18 @@ pub fn extract_name_and_bucket(path: &Path) -> Fallible<(String, String)> {
Some(caps) => {
let name = caps.name("name").map(|m| m.as_str().to_string());
let bucket = caps.name("bucket").map(|m| m.as_str().to_string());
if name.is_some() && bucket.is_some() {
return Ok((name.unwrap(), bucket.unwrap()));
if let Some(name) = name {
if let Some(bucket) = bucket {
return Ok((name, bucket));
}
}
}
}

Err(Error::Custom(format!("unsupported manifest path {}", path.display())).into())
Err(Error::Custom(format!(
"unsupported manifest path {}",
path.display()
)))
}

/// Normalize a path, removing things like `.` and `..`.
Expand Down
4 changes: 2 additions & 2 deletions crates/libscoop/src/internal/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub fn get_content_length(url: &str, proxy: Option<&str>) -> Option<f64> {
let mut easy = Easy::new();
easy.get(true).unwrap();
easy.url(url).unwrap();
if proxy.is_some() {
easy.proxy(proxy.unwrap()).unwrap();
if let Some(proxy) = proxy {
easy.proxy(proxy).unwrap();
}
easy.nobody(true).unwrap();
easy.follow_location(true).unwrap();
Expand Down
Loading

0 comments on commit a544d1a

Please sign in to comment.