Skip to content
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

Toml errors #109

Merged
merged 2 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
on:
workflow_dispatch:
push:
pull_request:
push:
branches:
- master

name: ci

Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ serde_json = "1.0.99"
thiserror = "1.0.64"
toml = "0.8.19"
serde = { version = "1.0.164", features = [ "derive" ] }
sysexits = "0.8.2"

[target.'cfg(target_arch = "wasm32")'.dependencies]
tokio = { version = "1.40.0", features = [ "macros", "rt" ] }
Expand Down
40 changes: 30 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use sysexits::ExitCode;

#[derive(Debug, Parser)]
struct Opts {
/// Print the default configuration.
Expand Down Expand Up @@ -111,17 +113,29 @@ fn list_lints() {
type Options<S = String> = eipw_lint::Options<Vec<DefaultModifier<S>>, HashMap<S, DefaultLint<S>>>;

#[cfg(target_arch = "wasm32")]
async fn read_config(_path: &Path) -> Options {
async fn read_config(_path: &Path) -> Result<Options, toml::de::Error> {
todo!()
}

#[cfg(not(target_arch = "wasm32"))]
async fn read_config(path: &Path) -> Options {
async fn read_config(path: &Path) -> Result<Options, toml::de::Error> {
let contents = tokio::fs::read_to_string(path)
.await
.expect("couldn't read config file");

toml::from_str(&contents).expect("couldn't parse config file")
toml::from_str(&contents)
}

async fn try_read_config(path: &Path) -> Result<Options, ExitCode> {
let error = match read_config(path).await {
Ok(o) => return Ok(o),
Err(e) => e,
};

eprintln!("Error(s) encountered in configuration file:");
eprintln!("{}", error);

Err(ExitCode::Config)
}

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -173,7 +187,7 @@ struct Lints {

#[cfg_attr(target_arch = "wasm32", tokio::main(flavor = "current_thread"))]
#[cfg_attr(not(target_arch = "wasm32"), tokio::main)]
async fn run(opts: Opts) -> Result<(), usize> {
async fn run(opts: Opts) -> Result<(), ExitCode> {
if opts.list_lints {
list_lints();
return Ok(());
Expand Down Expand Up @@ -201,7 +215,7 @@ async fn run(opts: Opts) -> Result<(), usize> {
let options: Options;
let mut linter;
if let Some(ref path) = opts.config {
options = read_config(path).await;
options = try_read_config(path).await?;
let options_iter = options.to_iters();
linter = Linter::with_options(reporter, options_iter);
} else {
Expand Down Expand Up @@ -246,17 +260,23 @@ async fn run(opts: Opts) -> Result<(), usize> {
}

if n_errors > 0 {
Err(n_errors)
eprintln!("validation failed with {} errors :(", n_errors);
Err(ExitCode::DataErr)
} else {
Ok(())
}
}

fn main() {
let opts = Opts::parse();
let opts = match Opts::try_parse() {
Ok(o) => o,
Err(e) => {
e.print().unwrap();
std::process::exit(ExitCode::Usage.into());
}
};

if let Err(n_errors) = run(opts) {
eprintln!("validation failed with {} errors :(", n_errors);
std::process::exit(1);
if let Err(e) = run(opts) {
std::process::exit(e.into());
}
}
Loading