From f870420d55ed566797aaa1f769e6137380ebc3af Mon Sep 17 00:00:00 2001 From: Brian Hicks Date: Thu, 3 Dec 2020 20:20:16 -0600 Subject: [PATCH] add forbids from a CSV --- Cargo.lock | 41 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 4edb72e..4b45f16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,9 +44,18 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "473fc6b38233f9af7baa94fb5852dca389e3d95b8e21c8e3719301462c5d9faf" dependencies = [ + "lazy_static", "memchr", + "regex-automata", + "serde", ] +[[package]] +name = "byteorder" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" + [[package]] name = "cc" version = "1.0.62" @@ -185,6 +194,28 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "csv" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9d58633299b24b515ac72a3f869f8b91306a3cec616a602843a383acd6f9e97" +dependencies = [ + "bstr", + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" +dependencies = [ + "memchr", +] + [[package]] name = "elm-forbid-import" version = "0.1.0" @@ -193,6 +224,7 @@ dependencies = [ "cc", "clap", "crossbeam", + "csv", "ignore", "lazy_static", "pathdiff", @@ -381,6 +413,15 @@ dependencies = [ "thread_local", ] +[[package]] +name = "regex-automata" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4" +dependencies = [ + "byteorder", +] + [[package]] name = "regex-syntax" version = "0.6.21" diff --git a/Cargo.toml b/Cargo.toml index b95ff36..a2a311c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ license-file = "LICENSE" anyhow = "1.0" clap = "3.0.0-beta.2" crossbeam = "0.8" +csv = "1.0" ignore = "0.4" lazy_static = "1.4" pathdiff = "0.1" diff --git a/src/main.rs b/src/main.rs index 46fd012..67cf7e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use anyhow::{Context, Result}; +use anyhow::{anyhow, Context, Result}; use clap::Clap; use std::path::PathBuf; use std::process; @@ -44,6 +44,13 @@ enum Mode { hint: Option, }, + /// Forbid a list of imports held in a CSV. The file should be a 2-column + /// CSV with "module" and "hint" fields and no headers. + ForbidFromCsv { + /// What file has the forbidden import list? + path: PathBuf, + }, + /// Stop forbidding the use of a specific import. Unforbid { /// The fully-qualified name to forbid (e.g. `Html.Events`) @@ -126,6 +133,34 @@ fn run(opts: Options) -> Result { Ok(0) } + Mode::ForbidFromCsv { path } => { + let mut reader = csv::ReaderBuilder::new() + .flexible(true) + .has_headers(false) + .from_path(path) + .context("could not read the CSV of forbidden imports")?; + + for record in reader.records() { + let mut record = record.context("could not read record")?; + record.trim(); + + let module = record + .get(0) + .and_then(|name| if !name.is_empty() { Some(name) } else { None }) + .map(|name| name.to_string()) + .ok_or(anyhow!( + "I need a module name in the first column of the CSV at " + ))?; + let hint = record.get(1).map(|name| name.to_string()); + + store.forbid(module, hint); + } + + store.write().context("could not update the config file")?; + + Ok(1) + } + Mode::Unforbid { name } => { store.unforbid(name); store.write().context("could not update the config file")?;