-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Split the crates into a bin and a lib, within the cargo-scout wor… #61
Changes from all commits
b700bde
f193542
4bd8327
f179400
85f4a36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,5 @@ | ||
[package] | ||
name = "cargo-scout" | ||
version = "0.4.0" | ||
authors = ["o0Ignition0o <jeremy.lempereur@gmail.com>"] | ||
description = "Run clippy::pedantic on your diffs only" | ||
homepage = "https://github.com/o0Ignition0o/cargo-scout" | ||
repository = "https://github.com/o0Ignition0o/cargo-scout" | ||
keywords = ["clippy", "lint","pedantic", "cli", "diff"] | ||
license = "MIT/Apache-2.0" | ||
include = [ | ||
"**/*.rs", | ||
"Cargo.toml", | ||
[workspace] | ||
members = [ | ||
"cargo-scout", | ||
"cargo-scout-lib", | ||
] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
serde = { version = "1.0.103", features = ["derive"] } | ||
serde_json = "1.0.44" | ||
structopt = "0.3.5" | ||
cargo_toml = "0.8.0" | ||
git2 = { version = "0.11.0", default-features = false } | ||
|
||
[dev-dependencies] | ||
tempfile = "3.1.0" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[package] | ||
name = "cargo-scout-lib" | ||
version = "0.5.0" | ||
authors = ["o0Ignition0o <jeremy.lempereur@gmail.com>"] | ||
description = "Lib that powers cargo-scout, and allows you to run / implement your own linters" | ||
homepage = "https://github.com/o0Ignition0o/cargo-scout" | ||
repository = "https://github.com/o0Ignition0o/cargo-scout" | ||
keywords = ["clippy", "lint","pedantic", "cli", "diff"] | ||
license = "MIT/Apache-2.0" | ||
include = [ | ||
"**/*.rs", | ||
"Cargo.toml", | ||
] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
cargo_toml = "0.8.0" | ||
git2 = { version = "0.11.*", default-features = false } | ||
serde = { version = "1.0.*", features = ["derive"] } | ||
serde_json = "1.0.*" | ||
|
||
[dev-dependencies] | ||
tempfile = "3.1.0" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
pub mod rust; | ||
|
||
/// This trait is responsible for providing a list of members, | ||
/// which are directories to be linted against. | ||
pub trait Config { | ||
/// This function should return a list of relative paths | ||
/// a linter will iterate on. | ||
/// | ||
/// If only the current working directory must be checked, it must return `vec![".".to_string()]` | ||
/// | ||
/// If several directories must be checked, | ||
/// return their relative path as strings. | ||
/// | ||
/// For example if your current working directory is `foo`, | ||
/// and you want to check `./bar` and `./baz`, | ||
/// return `vec!["bar".to_string(), "baz".to_string()]` | ||
/// | ||
/// # Example with the root directory | ||
/// ``` | ||
/// # use cargo_scout_lib::config::Config; | ||
/// # struct CustomConfig{} | ||
/// # impl CustomConfig { | ||
/// # fn new() -> Self { | ||
/// # Self {} | ||
/// # } | ||
/// # } | ||
/// # // Your own implementation goes here | ||
/// # impl Config for CustomConfig { | ||
/// # fn members(&self) -> Vec<String> { | ||
/// # vec![".".to_string()] | ||
/// # } | ||
/// # } | ||
/// let config = CustomConfig::new(); | ||
/// // Only the current directory must be linted | ||
/// assert_eq!(vec![".".to_string()], config.members()); | ||
/// ``` | ||
/// | ||
/// # Example with two subdirectories | ||
/// ``` | ||
/// # use cargo_scout_lib::config::Config; | ||
/// # struct CustomConfig{} | ||
/// # impl CustomConfig { | ||
/// # fn new() -> Self { | ||
/// # Self {} | ||
/// # } | ||
/// # } | ||
/// # // Your own implementation goes here | ||
/// # impl Config for CustomConfig { | ||
/// # fn members(&self) -> Vec<String> { | ||
/// # vec!["foo".to_string(), "bar".to_string()] | ||
/// # } | ||
/// # } | ||
/// let config = CustomConfig::new(); | ||
/// // Directories ./foo and ./bar must be linted | ||
/// assert_eq!(vec!["foo".to_string(), "bar".to_string()], config.members()); | ||
/// ``` | ||
/// | ||
/// # Implementing your own Config | ||
/// ``` | ||
/// use cargo_scout_lib::config::Config; | ||
/// | ||
/// struct CustomConfig{} | ||
/// | ||
/// # impl CustomConfig { | ||
/// # fn new() -> Self { | ||
/// # Self {} | ||
/// # } | ||
/// # } | ||
/// impl Config for CustomConfig { | ||
/// fn members(&self) -> Vec<String> { | ||
/// // Your own code to fetch the list of | ||
/// // directories to iterate on goes here | ||
/// # vec![".".to_string()] | ||
/// } | ||
/// } | ||
/// ``` | ||
fn members(&self) -> Vec<String>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use crate::config::Config; | ||
|
||
/// This struct represents a Cargo project configuration. | ||
pub struct CargoConfig { | ||
members: Vec<String>, | ||
} | ||
|
||
impl Config for CargoConfig { | ||
#[must_use] | ||
fn members(&self) -> Vec<String> { | ||
self.members.clone() | ||
} | ||
} | ||
|
||
impl CargoConfig { | ||
/// This function will instantiate a Config from a Cargo.toml path. | ||
/// | ||
/// If in a workspace, `get_members` will return the members | ||
/// of the [[workspace]] members section in Cargo.toml. | ||
/// | ||
/// Else, it will return `vec![".".to_string()]` | ||
/// | ||
/// # cargo-scout-lib example | ||
/// ``` | ||
/// # use cargo_scout_lib::config::Config; | ||
/// # use cargo_scout_lib::config::rust::CargoConfig; | ||
/// let config = CargoConfig::from_manifest_path("Cargo.toml")?; | ||
/// // There is only one directory to lint, which is the current one. | ||
/// assert_eq!(vec!["."], config.members()); | ||
/// # Ok::<(), cargo_scout_lib::Error>(()) | ||
/// ``` | ||
/// | ||
/// # cargo-scout workspace example | ||
/// ``` | ||
/// # use cargo_scout_lib::config::Config; | ||
/// # use cargo_scout_lib::config::rust::CargoConfig; | ||
/// let config = CargoConfig::from_manifest_path("../Cargo.toml")?; | ||
/// // We will lint `./cargo-scout` and `./cargo-scout-lib`. | ||
/// assert_eq!(vec!["cargo-scout".to_string(), "cargo-scout-lib".to_string()], config.members()); | ||
/// # Ok::<(), cargo_scout_lib::Error>(()) | ||
/// ``` | ||
pub fn from_manifest_path(p: impl AsRef<std::path::Path>) -> Result<Self, crate::error::Error> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not importing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As explained in this pretty cool blog post AsRef allows the function to accept anything that allows a Path to be borrowed from it. So it allows us to automagically get a Path from a String, which I think is pretty cool ! pub use error::Error; is a great idea ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's cool indeed! I meant why don't we do
Instead of using the fully qualified path. Just for consistency with other cases There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh sorry I didn’t understand that’s what you meant! |
||
Ok(Self::from_manifest(cargo_toml::Manifest::from_path(p)?)) | ||
} | ||
|
||
fn from_manifest(m: cargo_toml::Manifest) -> Self { | ||
if let Some(w) = m.workspace { | ||
Self { members: w.members } | ||
} else { | ||
Self { | ||
// Project root only | ||
members: vec![".".to_string()], | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::config::rust::CargoConfig; | ||
use crate::config::Config; | ||
|
||
#[test] | ||
fn test_not_workspace_manifest() { | ||
let manifest = cargo_toml::Manifest::from_path("Cargo.toml").unwrap(); | ||
// Make sure we actually parsed the manifest | ||
assert_eq!("cargo-scout-lib", manifest.clone().package.unwrap().name); | ||
let config = CargoConfig::from_manifest(manifest); | ||
assert_eq!(vec!["."], config.members()); | ||
} | ||
#[test] | ||
fn test_not_workspace_path() { | ||
let config = CargoConfig::from_manifest_path("Cargo.toml").unwrap(); | ||
assert_eq!(vec!["."], config.members()); | ||
} | ||
#[test] | ||
fn test_neqo_members_manifest() { | ||
let neqo_toml = r#"[workspace] | ||
members = [ | ||
"neqo-client", | ||
"neqo-common", | ||
"neqo-crypto", | ||
"neqo-http3", | ||
"neqo-http3-server", | ||
"neqo-qpack", | ||
"neqo-server", | ||
"neqo-transport", | ||
"neqo-interop", | ||
"test-fixture", | ||
]"#; | ||
|
||
let manifest = cargo_toml::Manifest::from_slice(neqo_toml.as_bytes()).unwrap(); | ||
let config = CargoConfig::from_manifest(manifest); | ||
assert_eq!( | ||
vec![ | ||
"neqo-client", | ||
"neqo-common", | ||
"neqo-crypto", | ||
"neqo-http3", | ||
"neqo-http3-server", | ||
"neqo-qpack", | ||
"neqo-server", | ||
"neqo-transport", | ||
"neqo-interop", | ||
"test-fixture" | ||
], | ||
config.members() | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pub mod config; | ||
pub mod error; | ||
pub mod linter; | ||
pub mod scout; | ||
pub mod vcs; | ||
|
||
pub use error::Error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's personal opinion, but I find
cargo_scout_lib
to be too long and a bit redundant when used in the binary. What do you think about naming the libscout
, and the binarycargo-scout
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to do it as well, but it turns out there's already a fuzzer out there that goes by the name scout, and I'm not really sure renaming the lib scout-lib wouldn't confuse people more :/
https://crates.io/search?q=scout
Maybe we can find an other name ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see, OK then, finding a name is not easy task so let's go with this for now :)