Skip to content

Commit

Permalink
removed trailing newlines from formatting/CLI output, fixed issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
its-the-shrimp committed Dec 18, 2023
1 parent de61f3e commit c8816d8
Show file tree
Hide file tree
Showing 12 changed files with 589 additions and 354 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ diffy = "0.3.0"
toml = "0.8.8"
serde = { version = "1.0.193", features = ["derive"] }
dirs = "5.0.1"

[features]
regen-tests = [] # solely for testing
25 changes: 25 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[cfg(feature = "regen-tests")]
fn main() {
use std::fs::{read_dir, File};
use std::io::{BufWriter, Write};

let mut test_suite = BufWriter::new(File::create("tests/main.rs").unwrap());
writeln!(test_suite, "mod common;\nuse common::cmp;\n").unwrap();

for entry in read_dir("tests/samples").unwrap() {
let entry = entry.unwrap();
let mut src = entry.path();
let name = src.file_name().unwrap().to_str().unwrap().to_owned();
src.push("source.rs");
let dst = src.with_file_name("target.rs");

writeln!(
test_suite,
"#[test] fn {name}() {{\n\tcmp({src:?}, {dst:?})\n}}\n"
)
.unwrap();
}
}

#[cfg(not(feature = "regen-tests"))]
fn main() {}
56 changes: 31 additions & 25 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{fs::read_to_string, path::Path, io, env::current_dir, num::ParseIntError};
use dirs::{home_dir, config_dir};
use crate::utils::StrExt;
use anyhow::{anyhow, bail, Context, Result};
use dirs::{config_dir, home_dir};
use serde::Deserialize;
use anyhow::{Result, Context, anyhow, bail};
use std::{env::current_dir, fs::read_to_string, io, num::ParseIntError, path::Path};
use toml::{map::Map, Value};
use crate::utils::StrExt;

#[derive(Clone)]
pub struct Config {
Expand All @@ -13,7 +13,10 @@ pub struct Config {

impl Default for Config {
fn default() -> Self {
Self { tab_spaces: 4, yew: YewConfig::default() }
Self {
tab_spaces: 4,
yew: YewConfig::default(),
}
}
}

Expand Down Expand Up @@ -44,7 +47,7 @@ struct RawConfig {
max_width: Option<usize>,
use_field_init_shorthand: Option<bool>,
#[serde(default)]
yew: RawConfigYew
yew: RawConfigYew,
}

#[derive(Deserialize, Default)]
Expand All @@ -62,7 +65,7 @@ fn parse_usize(src: &str) -> Result<usize, ParseIntError> {
Some(("0x", rest)) => (0x10, rest),
Some(("0b", rest)) => (0b10, rest),
Some(("0o", rest)) => (0o10, rest),
_ => (10, src),
_ => (10, src),
};
usize::from_str_radix(src, base)
}
Expand All @@ -71,7 +74,7 @@ fn parse_bool(src: &str) -> Result<bool> {
match src {
"true" => Ok(true),
"false" => Ok(false),
_ => bail!("expected `true` or `false`, instead got `{src}`")
_ => bail!("expected `true` or `false`, instead got `{src}`"),
}
}

Expand Down Expand Up @@ -127,35 +130,34 @@ impl Config {
}

Ok(Self {
tab_spaces: raw.tab_spaces
.unwrap_or(4),
tab_spaces: raw.tab_spaces.unwrap_or(4),
yew: YewConfig {
html_width: raw.yew.html_width
.or(raw.max_width)
.unwrap_or(100),
unwrap_literal_prop_values: raw.yew.unwrap_literal_prop_values
.unwrap_or(true),
use_prop_init_shorthand: raw.yew.use_prop_init_shorthand
html_width: raw.yew.html_width.or(raw.max_width).unwrap_or(100),
unwrap_literal_prop_values: raw.yew.unwrap_literal_prop_values.unwrap_or(true),
use_prop_init_shorthand: raw
.yew
.use_prop_init_shorthand
.or(raw.use_field_init_shorthand)
.unwrap_or(false),
self_close_elements: raw.yew.self_close_elements
.unwrap_or(true),
unknown: raw.yew.unknown
}
self_close_elements: raw.yew.self_close_elements.unwrap_or(true),
unknown: raw.yew.unknown,
},
})
}

pub fn fetch<'a>(
path: Option<&Path>,
additional: impl IntoIterator<Item = &'a (impl AsRef<str> + 'a, impl AsRef<str> + 'a)>
additional: impl IntoIterator<Item = &'a (impl AsRef<str> + 'a, impl AsRef<str> + 'a)>,
) -> Result<Self> {
macro_rules! return_parsed_if_file_exists {
($path:expr) => {{
let path: &Path = $path;
match read_to_string(path) {
Ok(src) => return Self::parse(&src, additional),
Err(err) => if err.kind() != io::ErrorKind::NotFound {
return Err(anyhow!(err).context(format!("failed to read {path:?}")))
Err(err) => {
if err.kind() != io::ErrorKind::NotFound {
return Err(anyhow!(err).context(format!("failed to read {path:?}")));
}
}
}
}};
Expand All @@ -169,7 +171,9 @@ impl Config {
path.set_file_name(".rustfmt.toml");
return_parsed_if_file_exists!(&path);
path.pop();
if !path.pop() { break }
if !path.pop() {
break;
}
}
}

Expand All @@ -180,7 +184,9 @@ impl Config {
aqui.set_file_name(".rustfmt.toml");
return_parsed_if_file_exists!(&aqui);
aqui.pop();
if !aqui.pop() { break }
if !aqui.pop() {
break;
}
}

let mut home = home_dir().context("failed to get the user's home directory")?;
Expand Down
Loading

0 comments on commit c8816d8

Please sign in to comment.