From 6a82fdf48c3b6ec9858b647d9d6190027f98047c Mon Sep 17 00:00:00 2001 From: Andrew Lilley Brinker Date: Tue, 7 May 2024 14:59:04 -0700 Subject: [PATCH 1/3] feat: Removed pathbuf module in favor of crate. A while back I'd made a published crate called `pathbuf` that does the same stuff we'd been doing in our own `pathbuf.rs` file, so let's just use that instead of having our own code for it. Signed-off-by: Andrew Lilley Brinker --- Cargo.lock | 1 + hipcheck/Cargo.toml | 3 +- hipcheck/src/analysis/session/mod.rs | 8 +-- hipcheck/src/config.rs | 2 +- hipcheck/src/data.rs | 13 ++--- hipcheck/src/data/modules.rs | 4 +- hipcheck/src/data/npm.rs | 2 +- hipcheck/src/data/query/module.rs | 8 ++- hipcheck/src/data/source/mod.rs | 2 +- hipcheck/src/data/source/query.rs | 8 +-- hipcheck/src/main.rs | 1 - hipcheck/src/pathbuf.rs | 78 ---------------------------- 12 files changed, 26 insertions(+), 104 deletions(-) delete mode 100644 hipcheck/src/pathbuf.rs diff --git a/Cargo.lock b/Cargo.lock index ca95898b..493eedfb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -623,6 +623,7 @@ dependencies = [ "once_cell", "ordered-float", "paste", + "pathbuf", "petgraph", "regex", "salsa", diff --git a/hipcheck/Cargo.toml b/hipcheck/Cargo.toml index 5a83e8fe..d0f482ab 100644 --- a/hipcheck/Cargo.toml +++ b/hipcheck/Cargo.toml @@ -29,10 +29,12 @@ lazy_static = "1.4.0" log = "0.4.16" libc = "0.2.154" maplit = "1.0.2" +native-tls = "0.2.11" nom = "7.1.3" once_cell = "1.10.0" ordered-float = { version = "4.2.0", features = ["serde"] } paste = "1.0.7" +pathbuf = "1.0.0" petgraph = { version = "0.6.0", features = ["serde-1"] } regex = "1.5.5" salsa = "0.16.1" @@ -59,7 +61,6 @@ url = "2.2.2" walkdir = "2.5.0" which = { version = "6.0.1", default-features = false } xml-rs = "0.8.20" -native-tls = "0.2.11" [target.'cfg(windows)'.dependencies.winapi] version = "0.3.9" diff --git a/hipcheck/src/analysis/session/mod.rs b/hipcheck/src/analysis/session/mod.rs index 78e496d9..ca1a1e52 100644 --- a/hipcheck/src/analysis/session/mod.rs +++ b/hipcheck/src/analysis/session/mod.rs @@ -39,7 +39,6 @@ use crate::error::Error; use crate::error::Result; use crate::filesystem::create_dir_all; use crate::hc_error; -use crate::pathbuf; use crate::report::Format; use crate::report::ReportParams; use crate::report::ReportParamsStorage; @@ -51,6 +50,7 @@ use crate::version::VersionQueryStorage; use crate::HIPCHECK_TOML_FILE; use chrono::prelude::*; use dotenv::var; +use pathbuf::pathbuf; use std::ffi::OsStr; use std::ffi::OsString; use std::fmt; @@ -372,7 +372,7 @@ pub fn resolve_config(config_flag: Option<&Path>) -> Result { // (See https://docs.rs/dirs/3.0.2/dirs/fn.cache_dir.html) if let Some(config_path) = config_flag { - let full_config_path = pathbuf![&config_path, HIPCHECK_TOML_FILE]; + let full_config_path = pathbuf![config_path, HIPCHECK_TOML_FILE]; if full_config_path.exists() { return Ok(full_config_path); } @@ -673,7 +673,7 @@ mod tests { ], || { let data_dir = None; - let data_path = pathbuf![dirs::data_dir().unwrap(), "hipcheck"]; + let data_path = pathbuf![&dirs::data_dir().unwrap(), "hipcheck"]; create_dir_all(data_path.as_path()).unwrap(); let result = resolve_data(data_dir).unwrap(); let path = result.to_str().unwrap(); @@ -748,7 +748,7 @@ mod tests { ("HC_DATA", None), ], || { - let data_path = pathbuf![dirs::data_dir().unwrap(), "hipcheck"]; + let data_path = pathbuf![&dirs::data_dir().unwrap(), "hipcheck"]; create_dir_all(data_path.as_path()).unwrap(); let result = resolve_data(None).unwrap(); let path = result.to_str().unwrap(); diff --git a/hipcheck/src/config.rs b/hipcheck/src/config.rs index 9951146e..12c2aba0 100644 --- a/hipcheck/src/config.rs +++ b/hipcheck/src/config.rs @@ -5,12 +5,12 @@ use crate::context::Context; use crate::error::Result; use crate::filesystem as file; -use crate::pathbuf; use crate::BINARY_CONFIG_FILE; use crate::F64; use crate::LANGS_FILE; use crate::ORGS_FILE; use crate::TYPO_FILE; +use pathbuf::pathbuf; use serde::Deserialize; use serde::Serialize; use smart_default::SmartDefault; diff --git a/hipcheck/src/data.rs b/hipcheck/src/data.rs index 62350a97..4b2e300d 100644 --- a/hipcheck/src/data.rs +++ b/hipcheck/src/data.rs @@ -2,25 +2,24 @@ //! Functions and types for data retrieval. -mod code_quality; -mod es_lint; pub mod git; pub mod git_command; +pub mod npm; +pub mod source; + +mod code_quality; +mod es_lint; mod github; mod hash; mod modules; -pub mod npm; mod query; -pub mod source; pub use query::*; -use std::collections::HashSet; use crate::context::Context; use crate::error::Error; use crate::error::Result; use crate::hc_error; -use crate::pathbuf; use git::get_commits_for_file; use git::Commit; use git::CommitContributor; @@ -28,9 +27,11 @@ use git::Contributor; use git::Diff; use github::*; use modules::RawModule; +use pathbuf::pathbuf; use petgraph::visit::Dfs; use petgraph::Graph; use serde::Serialize; +use std::collections::HashSet; use std::path::Path; use std::rc::Rc; diff --git a/hipcheck/src/data/modules.rs b/hipcheck/src/data/modules.rs index b476e699..8f920cc4 100644 --- a/hipcheck/src/data/modules.rs +++ b/hipcheck/src/data/modules.rs @@ -6,7 +6,7 @@ use crate::context::Context as _; use crate::error::Error; use crate::error::Result; use crate::hc_error; -use crate::pathbuf; +use pathbuf::pathbuf; use serde::Deserialize; use serde_json::Value as JsonValue; use std::collections::HashMap; @@ -21,7 +21,7 @@ use std::process::Command; pub fn generate_module_model(repo_dir: &Path, module_deps: &Path) -> Result> { let root = detect_npm_package_root(&pathbuf![repo_dir, "package.json"])?; - if !pathbuf![&repo_dir, &root].exists() { + if !pathbuf![repo_dir, &root].exists() { return Err(Error::msg( "Unable to identify module structure of repository code", )); diff --git a/hipcheck/src/data/npm.rs b/hipcheck/src/data/npm.rs index 04674e43..6a9b508a 100644 --- a/hipcheck/src/data/npm.rs +++ b/hipcheck/src/data/npm.rs @@ -6,7 +6,7 @@ use crate::context::Context; use crate::error::Result; use crate::filesystem as file; use crate::hc_error; -use crate::pathbuf; +use pathbuf::pathbuf; use serde::Deserialize; use std::collections::HashMap; use std::convert::AsRef; diff --git a/hipcheck/src/data/query/module.rs b/hipcheck/src/data/query/module.rs index 1d381baf..da0a2756 100644 --- a/hipcheck/src/data/query/module.rs +++ b/hipcheck/src/data/query/module.rs @@ -2,18 +2,16 @@ //! Query group for module information. -use std::path::PathBuf; -use std::rc::Rc; - use crate::data::associate_modules_and_commits; use crate::data::git::Commit; use crate::data::git::GitProvider; use crate::data::Module; use crate::data::ModuleGraph; - use crate::error::Error; use crate::error::Result; -use crate::pathbuf; +use pathbuf::pathbuf; +use std::path::PathBuf; +use std::rc::Rc; /// A module and an associated commit pub type ModuleCommitMap = Rc, Rc)>>; diff --git a/hipcheck/src/data/source/mod.rs b/hipcheck/src/data/source/mod.rs index 33dee983..c7080839 100644 --- a/hipcheck/src/data/source/mod.rs +++ b/hipcheck/src/data/source/mod.rs @@ -9,9 +9,9 @@ use crate::data::git_command::GitCommand; use crate::error::Error; use crate::error::Result; use crate::hc_error; -use crate::pathbuf; use crate::shell::Phase; use log::debug; +use pathbuf::pathbuf; use std::ffi::OsStr; use std::fmt; use std::fmt::Debug; diff --git a/hipcheck/src/data/source/query.rs b/hipcheck/src/data/source/query.rs index 6ae6c186..9d4680c0 100644 --- a/hipcheck/src/data/source/query.rs +++ b/hipcheck/src/data/source/query.rs @@ -5,7 +5,7 @@ use crate::data::source::Remote; use crate::data::source::Source; use crate::hash; -use crate::pathbuf; +use pathbuf::pathbuf; use std::path::PathBuf; use std::rc::Rc; @@ -80,14 +80,14 @@ fn storage_path(db: &dyn SourceQuery) -> Rc { repo, pull_request, .. - } => pathbuf!["remote", "github", owner, repo, pull_request.to_string()], + } => pathbuf!["remote", "github", owner, repo, &pull_request.to_string()], // This is an unknown remote source. - Unknown(url) => pathbuf!["remote", "unknown", hash!(url).to_string()], + Unknown(url) => pathbuf!["remote", "unknown", &hash!(url).to_string()], }, // This is a local source. None => match db.local().file_name() { Some(file_name) => pathbuf!["local", file_name], - None => pathbuf!["local", "unknown", hash!(db.local()).to_string()], + None => pathbuf!["local", "unknown", &hash!(db.local()).to_string()], }, }; diff --git a/hipcheck/src/main.rs b/hipcheck/src/main.rs index c4a4ed30..680a6c8a 100644 --- a/hipcheck/src/main.rs +++ b/hipcheck/src/main.rs @@ -7,7 +7,6 @@ mod context; mod data; mod error; mod filesystem; -mod pathbuf; mod report; mod shell; #[cfg(test)] diff --git a/hipcheck/src/pathbuf.rs b/hipcheck/src/pathbuf.rs deleted file mode 100644 index 537e4ced..00000000 --- a/hipcheck/src/pathbuf.rs +++ /dev/null @@ -1,78 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -//! `pathbuf` provides a single macro, [`pathbuf!`][pathbuf], which gives a [`vec!`][std_vec]-like syntax -//! for constructing [`PathBuf`][std_path_pathbuf]s. -//! -//! # Example -//! -//! ``` -//! # use hc_common::pathbuf; -//! # use std::path::Path; -//! # -//! fn do_something(dir: &Path) { -//! let file_name = pathbuf![dir, "filename.txt"]; -//! -//! if file_name.exists() { -//! // do something... -//! } -//! } -//! ``` -//! -//! [pathbuf]: macro.pathbuf.html -//! [std_vec]: https://doc.rust-lang.org/std/macro.vec.html "Documentation for std::vec (macro)" -//! [std_path_pathbuf]: https://doc.rust-lang.org/std/path/struct.PathBuf.html "Documentation for std::path::PathBuf (struct)" - -/// Creates a [`PathBuf`][std_path_pathbuf] containing the arguments. -/// -/// `pathbuf!` allows [`PathBuf`][std_path_pathbuf]s to be defined with the same syntax as array expressions, like so: -/// -/// ``` -/// # use hc_common::pathbuf; -/// # use std::path::Path; -/// # -/// fn do_something(dir: &Path) { -/// let file_name = pathbuf![dir, "filename.txt"]; -/// -/// if file_name.exists() { -/// // do something... -/// } -/// } -/// ``` -/// -/// [std_path_pathbuf]: https://doc.rust-lang.org/std/path/struct.PathBuf.html "Documentation for std::path::PathBuf (struct)" -#[macro_export] -macro_rules! pathbuf { - ( $( $part:expr ),* ) => {{ - use std::path::PathBuf; - - let mut temp = PathBuf::new(); - - $( - temp.push($part); - )* - - temp - }}; - - ($( $part:expr, )*) => ($crate::pathbuf![$($part),*]) -} - -#[cfg(test)] -mod tests { - use crate::pathbuf; - use std::path::PathBuf; - - #[test] - fn it_works() { - let p = pathbuf!["hello", "filename.txt"]; - - let expected = { - let mut temp = PathBuf::new(); - temp.push("hello"); - temp.push("filename.txt"); - temp - }; - - assert_eq!(p, expected); - } -} From d7efa53429308bc8189fef4f1f1b87669e646778 Mon Sep 17 00:00:00 2001 From: Andrew Lilley Brinker Date: Tue, 7 May 2024 15:18:46 -0700 Subject: [PATCH 2/3] feat: Organize helper modules under `util/` This is intended to help clarify the folder structure in Hipcheck. We have some helper logic that wraps `std` functionality with nicer error messages, or extends iterators with helpful new methods, and those can now be found under the `util/` directory. Signed-off-by: Andrew Lilley Brinker --- hipcheck/src/analysis/metric/affiliation.rs | 2 +- .../analysis/metric/binary_detector/mod.rs | 2 +- hipcheck/src/analysis/metric/linguist/mod.rs | 2 +- hipcheck/src/analysis/metric/typo.rs | 2 +- hipcheck/src/analysis/session/mod.rs | 2 +- hipcheck/src/config.rs | 2 +- hipcheck/src/data/npm.rs | 2 +- hipcheck/src/main.rs | 8 +- hipcheck/src/try_any.rs | 73 ------------------- hipcheck/src/util.rs | 6 ++ hipcheck/src/{filesystem.rs => util/fs.rs} | 33 --------- hipcheck/src/{try_filter.rs => util/iter.rs} | 72 +++++++++++++++++- 12 files changed, 84 insertions(+), 122 deletions(-) delete mode 100644 hipcheck/src/try_any.rs create mode 100644 hipcheck/src/util.rs rename hipcheck/src/{filesystem.rs => util/fs.rs} (68%) rename hipcheck/src/{try_filter.rs => util/iter.rs} (58%) diff --git a/hipcheck/src/analysis/metric/affiliation.rs b/hipcheck/src/analysis/metric/affiliation.rs index a77e1e73..25f0fd76 100644 --- a/hipcheck/src/analysis/metric/affiliation.rs +++ b/hipcheck/src/analysis/metric/affiliation.rs @@ -6,8 +6,8 @@ use crate::data::git::Commit; use crate::data::git::CommitContributorView; use crate::error::Error; use crate::error::Result; -use crate::filesystem as file; use crate::hc_error; +use crate::util::fs as file; use serde::de::Error as SerdeError; use serde::de::Visitor; use serde::Deserialize; diff --git a/hipcheck/src/analysis/metric/binary_detector/mod.rs b/hipcheck/src/analysis/metric/binary_detector/mod.rs index b6e57fa4..089afc33 100644 --- a/hipcheck/src/analysis/metric/binary_detector/mod.rs +++ b/hipcheck/src/analysis/metric/binary_detector/mod.rs @@ -6,8 +6,8 @@ pub use query::*; use crate::context::Context; use crate::error::Result; -use crate::filesystem::read_toml; use crate::hc_error; +use crate::util::fs::read_toml; use content_inspector::inspect; use content_inspector::ContentType; use serde::de::Visitor; diff --git a/hipcheck/src/analysis/metric/linguist/mod.rs b/hipcheck/src/analysis/metric/linguist/mod.rs index c080f87e..a30a6204 100644 --- a/hipcheck/src/analysis/metric/linguist/mod.rs +++ b/hipcheck/src/analysis/metric/linguist/mod.rs @@ -6,7 +6,7 @@ pub use query::*; use crate::context::Context as _; use crate::error::Result; -use crate::filesystem::read_toml; +use crate::util::fs::read_toml; use serde::de::Visitor; use serde::Deserialize; use serde::Deserializer; diff --git a/hipcheck/src/analysis/metric/typo.rs b/hipcheck/src/analysis/metric/typo.rs index 893a89a1..540047ee 100644 --- a/hipcheck/src/analysis/metric/typo.rs +++ b/hipcheck/src/analysis/metric/typo.rs @@ -5,7 +5,7 @@ use crate::context::Context as _; use crate::data::Dependencies; use crate::data::Lang; use crate::error::Result; -use crate::filesystem as file; +use crate::util::fs as file; use maplit::hashmap; use serde::Deserialize; use serde::Serialize; diff --git a/hipcheck/src/analysis/session/mod.rs b/hipcheck/src/analysis/session/mod.rs index ca1a1e52..9209bae9 100644 --- a/hipcheck/src/analysis/session/mod.rs +++ b/hipcheck/src/analysis/session/mod.rs @@ -37,13 +37,13 @@ use crate::data::ModuleProviderStorage; use crate::data::PullRequestReviewProviderStorage; use crate::error::Error; use crate::error::Result; -use crate::filesystem::create_dir_all; use crate::hc_error; use crate::report::Format; use crate::report::ReportParams; use crate::report::ReportParamsStorage; use crate::shell::Phase; use crate::shell::Shell; +use crate::util::fs::create_dir_all; use crate::version::get_version; use crate::version::VersionQuery; use crate::version::VersionQueryStorage; diff --git a/hipcheck/src/config.rs b/hipcheck/src/config.rs index 12c2aba0..3ef7eaa0 100644 --- a/hipcheck/src/config.rs +++ b/hipcheck/src/config.rs @@ -4,7 +4,7 @@ use crate::context::Context; use crate::error::Result; -use crate::filesystem as file; +use crate::util::fs as file; use crate::BINARY_CONFIG_FILE; use crate::F64; use crate::LANGS_FILE; diff --git a/hipcheck/src/data/npm.rs b/hipcheck/src/data/npm.rs index 6a9b508a..27a62704 100644 --- a/hipcheck/src/data/npm.rs +++ b/hipcheck/src/data/npm.rs @@ -4,8 +4,8 @@ use crate::command_util::log_args; use crate::command_util::DependentProgram; use crate::context::Context; use crate::error::Result; -use crate::filesystem as file; use crate::hc_error; +use crate::util::fs as file; use pathbuf::pathbuf; use serde::Deserialize; use std::collections::HashMap; diff --git a/hipcheck/src/main.rs b/hipcheck/src/main.rs index 680a6c8a..10de6cd3 100644 --- a/hipcheck/src/main.rs +++ b/hipcheck/src/main.rs @@ -6,15 +6,13 @@ mod config; mod context; mod data; mod error; -mod filesystem; mod report; mod shell; #[cfg(test)] mod test_util; #[cfg(test)] mod tests; -mod try_any; -mod try_filter; +mod util; mod version; use crate::analysis::report_builder::build_pr_report; @@ -38,6 +36,8 @@ use crate::shell::ColorChoice; use crate::shell::Output; use crate::shell::Shell; use crate::shell::Verbosity; +use crate::util::iter::TryAny; +use crate::util::iter::TryFilter; use clap::Arg; use clap::ArgAction; use clap::Command; @@ -50,8 +50,6 @@ use std::path::Path; use std::path::PathBuf; use std::process::exit; use std::str::FromStr; -use try_any::TryAny; -use try_filter::TryFilter; /// Entry point for Hipcheck. /// diff --git a/hipcheck/src/try_any.rs b/hipcheck/src/try_any.rs deleted file mode 100644 index 1ccebbcb..00000000 --- a/hipcheck/src/try_any.rs +++ /dev/null @@ -1,73 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -//! A trait containing a fallible analogue of the `Iterator::any` -//! method. - -/// A fallible analogue of the `Iterator::any` method -pub trait TryAny: Iterator { - fn try_any(&mut self, mut f: F) -> Result - where - F: FnMut(::Item) -> Result, - { - for t in self { - match f(t) { - Ok(false) => continue, - result => return result, - } - } - - Ok(false) - } -} - -impl TryAny for I {} - -#[cfg(test)] -mod tests { - use super::*; - - fn odd_not_three(n: usize) -> Result { - if n == 3 { - Err(String::from("Error")) - } else if n % 2 == 1 { - Ok(true) - } else { - Ok(false) - } - } - - #[test] - fn any_ok_true() { - let v = vec![2, 4, 5]; - - let result = v.into_iter().try_any(odd_not_three).unwrap(); - - assert!(result); - } - - #[test] - fn any_ok_true_with_three() { - let v = vec![2, 4, 5, 3]; - - let result = v.into_iter().try_any(odd_not_three).unwrap(); - - assert!(result); - } - - #[test] - fn any_ok_false() { - let v = vec![2, 4, 6, 8, 10]; - - let result = v.into_iter().try_any(odd_not_three).unwrap(); - - assert!(!result); - } - - #[test] - #[should_panic] - fn any_err() { - let v = vec![2, 4, 3, 1, 5, 7, 9]; - - v.into_iter().try_any(odd_not_three).unwrap(); - } -} diff --git a/hipcheck/src/util.rs b/hipcheck/src/util.rs new file mode 100644 index 00000000..ef284386 --- /dev/null +++ b/hipcheck/src/util.rs @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: Apache-2.0 + +//! Utility methods and types used throughout Hipcheck. + +pub mod fs; +pub mod iter; diff --git a/hipcheck/src/filesystem.rs b/hipcheck/src/util/fs.rs similarity index 68% rename from hipcheck/src/filesystem.rs rename to hipcheck/src/util/fs.rs index 023b26d2..def32e62 100644 --- a/hipcheck/src/filesystem.rs +++ b/hipcheck/src/util/fs.rs @@ -5,7 +5,6 @@ use crate::error::Result; use crate::hc_error; use serde::de::DeserializeOwned; use std::fs; -use std::fs::File; use std::ops::Not; use std::path::Path; @@ -44,28 +43,7 @@ pub fn read_json, T: DeserializeOwned>(path: P) -> Result { .with_context(|| format!("failed to read as JSON '{}'", path.display())) } -/// Write to a file. -#[allow(dead_code)] -pub fn write, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> { - fn inner(path: &Path, contents: &[u8]) -> Result<()> { - fs::write(path, contents).with_context(|| format!("failed to write '{}'", path.display())) - } - - inner(path.as_ref(), contents.as_ref()) -} - -/// Open an existing file. -#[allow(dead_code)] -pub fn open>(path: P) -> Result { - fn inner(path: &Path) -> Result { - File::open(path).with_context(|| format!("failed to open file '{}'", path.display())) - } - - inner(path.as_ref()) -} - /// Create a directory and missing parents. -#[allow(dead_code)] pub fn create_dir_all>(path: P) -> Result<()> { fn inner(path: &Path) -> Result<()> { fs::create_dir_all(path) @@ -75,17 +53,6 @@ pub fn create_dir_all>(path: P) -> Result<()> { inner(path.as_ref()) } -/// Remove a directory and any children. -#[allow(dead_code)] -pub fn remove_dir_all>(path: P) -> Result<()> { - fn inner(path: &Path) -> Result<()> { - fs::remove_dir_all(path) - .with_context(|| format!("failed to remove directory '{}'", path.display())) - } - - inner(path.as_ref()) -} - /// Check that a given path exists. pub fn exists>(path: P) -> Result<()> { fn inner(path: &Path) -> Result<()> { diff --git a/hipcheck/src/try_filter.rs b/hipcheck/src/util/iter.rs similarity index 58% rename from hipcheck/src/try_filter.rs rename to hipcheck/src/util/iter.rs index a5153c54..beb3e6fb 100644 --- a/hipcheck/src/try_filter.rs +++ b/hipcheck/src/util/iter.rs @@ -1,6 +1,25 @@ // SPDX-License-Identifier: Apache-2.0 -//! A struct and trait for performing fallible filtering of Iterators. +//! Iterator extension traits. + +/// A fallible analogue of the `Iterator::any` method +pub trait TryAny: Iterator { + fn try_any(&mut self, mut f: F) -> Result + where + F: FnMut(::Item) -> Result, + { + for t in self { + match f(t) { + Ok(false) => continue, + result => return result, + } + } + + Ok(false) + } +} + +impl TryAny for I {} /// Represents an iterator and a fallible criterion for filtering it pub struct FallibleFilter @@ -62,7 +81,17 @@ impl TryFilter for I {} mod tests { use super::*; - fn odd_not_three(n: &usize) -> Result { + fn odd_not_three(n: usize) -> Result { + if n == 3 { + Err(String::from("Error")) + } else if n % 2 == 1 { + Ok(true) + } else { + Ok(false) + } + } + + fn odd_not_three_ref(n: &usize) -> Result { if *n == 3 { Err(String::from("Error")) } else if *n % 2 == 1 { @@ -72,13 +101,48 @@ mod tests { } } + #[test] + fn any_ok_true() { + let v = vec![2, 4, 5]; + + let result = v.into_iter().try_any(odd_not_three).unwrap(); + + assert!(result); + } + + #[test] + fn any_ok_true_with_three() { + let v = vec![2, 4, 5, 3]; + + let result = v.into_iter().try_any(odd_not_three).unwrap(); + + assert!(result); + } + + #[test] + fn any_ok_false() { + let v = vec![2, 4, 6, 8, 10]; + + let result = v.into_iter().try_any(odd_not_three).unwrap(); + + assert!(!result); + } + + #[test] + #[should_panic] + fn any_err() { + let v = vec![2, 4, 3, 1, 5, 7, 9]; + + v.into_iter().try_any(odd_not_three).unwrap(); + } + #[test] fn filter_ok() { let v = vec![1, 2, 4, 5]; let result = v .into_iter() - .try_filter(odd_not_three) + .try_filter(odd_not_three_ref) .collect::, String>>() .unwrap(); @@ -91,7 +155,7 @@ mod tests { let v = vec![2, 4, 6, 8, 3, 10]; v.into_iter() - .try_filter(odd_not_three) + .try_filter(odd_not_three_ref) .collect::, String>>() .unwrap(); } From 59dfcba1f395ac3a8ae2abfd2525c405e9695d63 Mon Sep 17 00:00:00 2001 From: Andrew Lilley Brinker Date: Tue, 7 May 2024 15:19:47 -0700 Subject: [PATCH 3/3] fix: Get `cargo xtask doc --open` working again Turns out at some point, probably with the Clap upgrade, that we'd broken the `--open` flag on the `cargo xtask doc` CLI. This commit fixes it, so you can use `cargo xtask doc --open` to see the internal API docs of the `hc` crate. Signed-off-by: Andrew Lilley Brinker --- xtask/src/main.rs | 4 ++-- xtask/src/task/doc.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index d3f16d35..ac516a56 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -37,8 +37,8 @@ fn main() { .subcommand( Command::new("doc").arg( Arg::new("open") - .value_name("open") - .index(1) + .short('o') + .long("open") .action(ArgAction::SetTrue), ), ) diff --git a/xtask/src/task/doc.rs b/xtask/src/task/doc.rs index a90b3354..06585898 100644 --- a/xtask/src/task/doc.rs +++ b/xtask/src/task/doc.rs @@ -43,7 +43,7 @@ fn run_cargo_doc(should_open: OpenDoc) -> Result<()> { out_dir.pop(); // Add the rest of the path to the Hipcheck index file. - pathbuf![&out_dir, ".target", "doc", "hipcheck", "index.html"] + pathbuf![&out_dir, ".target", "doc", "hc", "index.html"] }; open::that(path)?;