From 305960c98b3b71d4b915003430730c76fcda3af3 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 14 Jan 2022 22:21:42 +0530 Subject: add dump command, support version overrides in statix.toml --- bin/src/config.rs | 78 +++++++++++++++++++++++++++++++------------------------ bin/src/dump.rs | 7 +++++ bin/src/err.rs | 8 +++--- bin/src/fix.rs | 24 ++++++++--------- bin/src/lib.rs | 1 + bin/src/lint.rs | 17 +++++++----- bin/src/main.rs | 2 ++ bin/src/utils.rs | 4 +++ 8 files changed, 84 insertions(+), 57 deletions(-) create mode 100644 bin/src/dump.rs (limited to 'bin/src') diff --git a/bin/src/config.rs b/bin/src/config.rs index 572ddde..28e6cc3 100644 --- a/bin/src/config.rs +++ b/bin/src/config.rs @@ -8,7 +8,7 @@ use std::{ use crate::{dirs, err::ConfigErr, utils, LintMap}; use clap::Parser; -use lib::LINTS; +use lib::{session::Version, LINTS}; use serde::{Deserialize, Serialize}; use vfs::ReadOnlyVfs; @@ -29,6 +29,8 @@ pub enum SubCommand { Single(Single), /// Print detailed explanation for a lint warning Explain(Explain), + /// Dump a sample config to stdout + Dump(Dump), } #[derive(Parser, Debug)] @@ -51,7 +53,7 @@ pub struct Check { pub format: OutFormat, /// Path to statix.toml - #[clap(short = 'c', long = "config", default_value = ".")] + #[clap(short = 'c', long = "config", default_value = "./statix.toml")] pub conf_path: PathBuf, /// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout @@ -76,10 +78,6 @@ impl Check { vfs(files.collect::>()) } } - - pub fn lints(&self) -> Result { - lints(&self.conf_path) - } } #[derive(Parser, Debug)] @@ -101,7 +99,7 @@ pub struct Fix { pub diff_only: bool, /// Path to statix.toml - #[clap(short = 'c', long = "config", default_value = ".")] + #[clap(short = 'c', long = "config", default_value = "./statix.toml")] pub conf_path: PathBuf, /// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout @@ -144,10 +142,6 @@ impl Fix { FixOut::Write } } - - pub fn lints(&self) -> Result { - lints(&self.conf_path) - } } #[derive(Parser, Debug)] @@ -167,6 +161,10 @@ pub struct Single { /// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout #[clap(short, long = "stdin")] pub streaming: bool, + + /// Path to statix.toml + #[clap(short = 'c', long = "config", default_value = "./statix.toml")] + pub conf_path: PathBuf, } impl Single { @@ -204,6 +202,9 @@ pub struct Explain { pub target: u32, } +#[derive(Parser, Debug)] +pub struct Dump {} + #[derive(Debug, Copy, Clone)] pub enum OutFormat { #[cfg(feature = "json")] @@ -251,13 +252,19 @@ impl FromStr for OutFormat { #[derive(Serialize, Deserialize, Debug)] pub struct ConfFile { + #[serde(default = "Vec::new")] disabled: Vec, + nix_version: Option, } impl Default for ConfFile { fn default() -> Self { let disabled = vec![]; - Self { disabled } + let nix_version = Some(utils::default_nix_version()); + Self { + disabled, + nix_version, + } } } @@ -265,15 +272,7 @@ impl ConfFile { pub fn from_path>(path: P) -> Result { let path = path.as_ref(); let config_file = fs::read_to_string(path).map_err(ConfigErr::InvalidPath)?; - toml::de::from_str(&config_file).map_err(|err| { - let pos = err.line_col(); - let msg = if let Some((line, col)) = pos { - format!("line {}, col {}", line, col) - } else { - "unknown".to_string() - }; - ConfigErr::ConfFileParse(msg) - }) + (toml::de::from_str(&config_file)).map_err(ConfigErr::ConfFileParse) } pub fn discover>(path: P) -> Result { let cannonical_path = fs::canonicalize(path.as_ref()).map_err(ConfigErr::InvalidPath)?; @@ -288,6 +287,29 @@ impl ConfFile { pub fn dump(&self) -> String { toml::ser::to_string_pretty(&self).unwrap() } + pub fn lints(&self) -> LintMap { + utils::lint_map_of( + (&*LINTS) + .iter() + .filter(|l| !self.disabled.iter().any(|check| check == l.name())) + .cloned() + .collect::>() + .as_slice(), + ) + } + pub fn version(&self) -> Result { + if let Some(v) = &self.nix_version { + v.parse::() + .map_err(|_| ConfigErr::ConfFileVersionParse(v.clone())) + } else if let Some(v) = utils::get_version_info() + .map(|o| o.parse::().ok()) + .flatten() + { + Ok(v) + } else { + Ok(utils::default_nix_version().parse::().unwrap()) + } + } } fn parse_line_col(src: &str) -> Result<(usize, usize), ConfigErr> { @@ -328,20 +350,8 @@ fn vfs(files: Vec) -> Result { let _id = vfs.alloc_file_id(&file); vfs.set_file_contents(&file, data.as_bytes()); } else { - println!("{} contains non-utf8 content", file.display()); + println!("`{}` contains non-utf8 content", file.display()); }; } Ok(vfs) } - -fn lints(conf_path: &Path) -> Result { - let config_file = ConfFile::discover(conf_path)?; - Ok(utils::lint_map_of( - (&*LINTS) - .iter() - .filter(|l| !config_file.disabled.iter().any(|check| check == l.name())) - .cloned() - .collect::>() - .as_slice(), - )) -} diff --git a/bin/src/dump.rs b/bin/src/dump.rs new file mode 100644 index 0000000..5ec3f0d --- /dev/null +++ b/bin/src/dump.rs @@ -0,0 +1,7 @@ +pub mod main { + use crate::{config::ConfFile, err::StatixErr}; + pub fn main() -> Result<(), StatixErr> { + println!("{}", ConfFile::dump(&ConfFile::default())); + Ok(()) + } +} diff --git a/bin/src/err.rs b/bin/src/err.rs index f44d27b..8124c1f 100644 --- a/bin/src/err.rs +++ b/bin/src/err.rs @@ -1,7 +1,5 @@ use std::io; -// use globset::ErrorKind; -// use rnix::parser::ParseError; use thiserror::Error; #[derive(Error, Debug)] @@ -14,8 +12,10 @@ pub enum ConfigErr { InvalidPosition(String), #[error("unable to parse `{0}` as warning code")] InvalidWarningCode(String), - #[error("unable to parse config file, error at: `{0}`")] - ConfFileParse(String), + #[error("unable to parse config file: {0}")] + ConfFileParse(toml::de::Error), + #[error("unable to parse nix version: `{0}`")] + ConfFileVersionParse(String), } // #[derive(Error, Debug)] diff --git a/bin/src/fix.rs b/bin/src/fix.rs index 4b2ce0c..84186d4 100644 --- a/bin/src/fix.rs +++ b/bin/src/fix.rs @@ -41,22 +41,22 @@ pub mod main { use std::borrow::Cow; use crate::{ - config::{Fix as FixConfig, FixOut, Single as SingleConfig}, + config::{ + FixOut, Single as SingleConfig, {ConfFile, Fix as FixConfig}, + }, err::{FixErr, StatixErr}, - utils, }; - use lib::session::{SessionInfo, Version}; + use lib::session::SessionInfo; use similar::TextDiff; pub fn all(fix_config: FixConfig) -> Result<(), StatixErr> { let vfs = fix_config.vfs()?; - let lints = fix_config.lints()?; + let conf_file = ConfFile::discover(&fix_config.conf_path)?; + + let lints = conf_file.lints(); + let version = conf_file.version()?; - let version = utils::get_version_info() - .unwrap() - .parse::() - .unwrap(); let session = SessionInfo::from_version(version); for entry in vfs.iter() { @@ -102,10 +102,10 @@ pub mod main { let original_src = entry.contents; let (line, col) = single_config.position; - let version = utils::get_version_info() - .unwrap() - .parse::() - .unwrap(); + let conf_file = ConfFile::discover(&single_config.conf_path)?; + + let version = conf_file.version()?; + let session = SessionInfo::from_version(version); match ( diff --git a/bin/src/lib.rs b/bin/src/lib.rs index 01d3ea7..8200347 100644 --- a/bin/src/lib.rs +++ b/bin/src/lib.rs @@ -1,5 +1,6 @@ pub mod config; pub mod dirs; +pub mod dump; pub mod err; pub mod explain; pub mod fix; diff --git a/bin/src/lint.rs b/bin/src/lint.rs index c385007..055bb76 100644 --- a/bin/src/lint.rs +++ b/bin/src/lint.rs @@ -43,19 +43,22 @@ pub mod main { use std::io; use super::lint_with; - use crate::{config::Check as CheckConfig, err::StatixErr, traits::WriteDiagnostic, utils}; + use crate::{ + config::{Check as CheckConfig, ConfFile}, + err::StatixErr, + traits::WriteDiagnostic, + }; - use lib::session::{SessionInfo, Version}; + use lib::session::SessionInfo; pub fn main(check_config: CheckConfig) -> Result<(), StatixErr> { let vfs = check_config.vfs()?; let mut stdout = io::stdout(); - let lints = check_config.lints()?; - let version = utils::get_version_info() - .unwrap() - .parse::() - .unwrap(); + let conf_file = ConfFile::discover(&check_config.conf_path)?; + let lints = conf_file.lints(); + let version = conf_file.version()?; + let session = SessionInfo::from_version(version); let lint = |vfs_entry| lint_with(vfs_entry, &lints, &session); diff --git a/bin/src/main.rs b/bin/src/main.rs index f504796..be79bb8 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -1,6 +1,7 @@ use clap::Parser; use statix::{ config::{Opts, SubCommand}, + dump, err::StatixErr, explain, fix, lint, }; @@ -12,6 +13,7 @@ fn _main() -> Result<(), StatixErr> { SubCommand::Fix(config) => fix::main::all(config), SubCommand::Single(config) => fix::main::single(config), SubCommand::Explain(config) => explain::main::main(config), + SubCommand::Dump(_) => dump::main::main(), } } diff --git a/bin/src/utils.rs b/bin/src/utils.rs index a3d51b4..1adac4a 100644 --- a/bin/src/utils.rs +++ b/bin/src/utils.rs @@ -35,3 +35,7 @@ pub fn get_version_info() -> Option { .nth(2) .map(ToOwned::to_owned) } + +pub fn default_nix_version() -> String { + String::from("2.4") +} -- cgit v1.2.3