From ee3a9e675a020e65d956e91f3361b0bedae9103e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Thu, 4 Jul 2024 07:05:57 +0200 Subject: [PATCH] Add `.rustfmt.toml` configuration This PR configures the formatting with a few defaults that aid readability, in my opinion. One drawback of adding this file is that rustfmt uses unstable features, now, and you have to use nightly to run it: ```sh cargo +nightly fmt --all ``` --- .rustfmt.toml | 12 ++++++++++++ rinja/src/error.rs | 1 - rinja/src/filters/mod.rs | 6 ++---- rinja_axum/tests/basic.rs | 10 ++++------ rinja_derive/src/config.rs | 6 +++--- rinja_derive/src/generator.rs | 22 +++++++++++----------- rinja_derive/src/input.rs | 14 +++++++------- rinja_escape/src/lib.rs | 3 ++- rinja_parser/src/lib.rs | 3 ++- testing/tests/inheritance.rs | 3 ++- testing/tests/render_in_place.rs | 2 +- testing/tests/simple.rs | 4 ++-- testing/tests/ui.rs | 1 + testing/tests/whitespace.rs | 5 ++++- 14 files changed, 53 insertions(+), 39 deletions(-) create mode 100644 .rustfmt.toml diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 000000000..2702b857b --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,12 @@ +edition = "2021" +group_imports = "StdExternalCrate" +imports_granularity = "Module" +newline_style = "Unix" +normalize_comments = true +unstable_features = true +use_field_init_shorthand = true +version = "Two" + +ignore = [ + "testing/tests/hello.rs", +] diff --git a/rinja/src/error.rs b/rinja/src/error.rs index a9db72ccf..25c195b68 100644 --- a/rinja/src/error.rs +++ b/rinja/src/error.rs @@ -22,7 +22,6 @@ pub type Result = std::result::Result; /// using a adapter the benefits `failure` would /// bring to this crate are small, which is why /// `std::error::Error` was used. -/// #[non_exhaustive] #[derive(Debug)] pub enum Error { diff --git a/rinja/src/filters/mod.rs b/rinja/src/filters/mod.rs index 2405ccc06..58a726460 100644 --- a/rinja/src/filters/mod.rs +++ b/rinja/src/filters/mod.rs @@ -9,9 +9,6 @@ use std::fmt::{self, Write}; #[cfg(feature = "serde_json")] mod json; -#[cfg(feature = "serde_json")] -pub use self::json::{json, json_pretty, AsIndent}; - #[cfg(feature = "humansize")] use humansize::{ISizeFormatter, ToF64, DECIMAL}; #[cfg(feature = "num-traits")] @@ -20,6 +17,8 @@ use num_traits::{cast::NumCast, Signed}; use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC}; use rinja_escape::{Escaper, MarkupDisplay}; +#[cfg(feature = "serde_json")] +pub use self::json::{json, json_pretty, AsIndent}; use crate::{Error, Result}; #[cfg(feature = "urlencode")] @@ -427,7 +426,6 @@ where /// This struct implements [`fmt::Display`], but only produces a string once. /// Any subsequent call to `.to_string()` will result in an empty string, because the iterator is /// already consumed. -// // The filter contains a [`Cell`], so we can modify iterator inside a method that takes `self` by // reference: [`fmt::Display::fmt()`] normally has the contract that it will produce the same result // in multiple invocations for the same object. We break this contract, because have to consume the diff --git a/rinja_axum/tests/basic.rs b/rinja_axum/tests/basic.rs index eb63884d9..1425597db 100644 --- a/rinja_axum/tests/basic.rs +++ b/rinja_axum/tests/basic.rs @@ -1,9 +1,7 @@ -use axum::{ - body::Body, - http::{Request, StatusCode}, - routing::get, - Router, -}; +use axum::body::Body; +use axum::http::{Request, StatusCode}; +use axum::routing::get; +use axum::Router; use http_body_util::BodyExt; use rinja_axum::Template; use tower::util::ServiceExt; diff --git a/rinja_derive/src/config.rs b/rinja_derive/src/config.rs index 929e20d29..398971c48 100644 --- a/rinja_derive/src/config.rs +++ b/rinja_derive/src/config.rs @@ -3,12 +3,12 @@ use std::path::{Path, PathBuf}; use std::rc::Rc; use std::{env, fs}; +use parser::node::Whitespace; +use parser::Syntax; #[cfg(feature = "config")] use serde::Deserialize; use crate::{CompileError, FileInfo, CRATE}; -use parser::node::Whitespace; -use parser::Syntax; #[derive(Debug)] pub(crate) struct Config<'a> { @@ -65,7 +65,7 @@ impl<'a> Config<'a> { return Err(CompileError::new( format!("invalid value for `whitespace`: \"{s}\""), file_info, - )) + )); } }; } diff --git a/rinja_derive/src/generator.rs b/rinja_derive/src/generator.rs index 201d41516..7d7cde3d0 100644 --- a/rinja_derive/src/generator.rs +++ b/rinja_derive/src/generator.rs @@ -6,17 +6,17 @@ use std::path::Path; use std::rc::Rc; use std::{cmp, hash, mem, str}; -use crate::config::WhitespaceHandling; -use crate::heritage::{Context, Heritage}; -use crate::input::{Source, TemplateInput}; -use crate::{CompileError, CRATE}; - use parser::node::{ Call, Comment, CondTest, FilterBlock, If, Include, Let, Lit, Loop, Match, Whitespace, Ws, }; use parser::{Expr, Filter, Node, Target, WithSpan}; use quote::quote; +use crate::config::WhitespaceHandling; +use crate::heritage::{Context, Heritage}; +use crate::input::{Source, TemplateInput}; +use crate::{CompileError, CRATE}; + pub(crate) struct Generator<'a> { // The template input state: original struct AST and attributes input: &'a TemplateInput<'a>, @@ -240,7 +240,7 @@ impl<'a> Generator<'a> { )); } - /* Helper methods for handling node types */ + // Helper methods for handling node types fn handle( &mut self, @@ -960,7 +960,7 @@ impl<'a> Generator<'a> { (None, Some((prev_name, gen))) => (prev_name, gen + 1), // `super()` is called from outside a block (None, None) => { - return Err(ctx.generate_error("cannot call 'super()' outside block", node)) + return Err(ctx.generate_error("cannot call 'super()' outside block", node)); } }; @@ -1225,7 +1225,7 @@ impl<'a> Generator<'a> { self.handle_ws(comment.ws); } - /* Visitor methods for expression types */ + // Visitor methods for expression types fn visit_expr_root( &mut self, @@ -1415,7 +1415,7 @@ impl<'a> Generator<'a> { let opt_escaper = match args.get(1).map(|expr| &**expr) { Some(Expr::StrLit(name)) => Some(*name), Some(_) => { - return Err(ctx.generate_error("invalid escaper type for escape filter", node)) + return Err(ctx.generate_error("invalid escaper type for escape filter", node)); } None => None, }; @@ -1618,7 +1618,7 @@ impl<'a> Generator<'a> { _ => { return Err( ctx.generate_error("loop.cycle(…) cannot use an empty array", left) - ) + ); } }, s => return Err(ctx.generate_error(&format!("unknown loop method: {s:?}"), left)), @@ -1873,7 +1873,7 @@ impl<'a> Generator<'a> { } } - /* Helper methods for dealing with whitespace nodes */ + // Helper methods for dealing with whitespace nodes // Combines `flush_ws()` and `prepare_ws()` to handle both trailing whitespace from the // preceding literal and leading whitespace from the succeeding literal. diff --git a/rinja_derive/src/input.rs b/rinja_derive/src/input.rs index 66e30d663..7909faa86 100644 --- a/rinja_derive/src/input.rs +++ b/rinja_derive/src/input.rs @@ -57,7 +57,7 @@ impl TemplateInput<'_> { (&Source::Source(_), None) => { return Err(CompileError::no_file_info( "must include 'ext' attribute when using 'source' attribute", - )) + )); } }; @@ -240,12 +240,12 @@ impl TemplateArgs { Ok(_) => { return Err(CompileError::no_file_info( "duplicated 'template' attribute", - )) + )); } Err(e) => { return Err(CompileError::no_file_info(format!( "unable to parse template arguments: {e}" - ))) + ))); } }; } @@ -264,7 +264,7 @@ impl TemplateArgs { return Err(CompileError::no_file_info(format!( "unsupported attribute argument {:?}", item.to_token_stream() - ))) + ))); } }; @@ -280,13 +280,13 @@ impl TemplateArgs { _ => { return Err(CompileError::no_file_info(format!( "unsupported argument value type for {ident:?}" - ))) + ))); } }, _ => { return Err(CompileError::no_file_info(format!( "unsupported argument value type for {ident:?}" - ))) + ))); } }; @@ -440,7 +440,7 @@ impl FromStr for Print { v => { return Err(CompileError::no_file_info(format!( "invalid value for print option: {v}" - ))) + ))); } }) } diff --git a/rinja_escape/src/lib.rs b/rinja_escape/src/lib.rs index f1ff0b0f9..036302c55 100644 --- a/rinja_escape/src/lib.rs +++ b/rinja_escape/src/lib.rs @@ -169,9 +169,10 @@ pub trait Escaper { mod tests { extern crate std; - use super::*; use std::string::ToString; + use super::*; + #[test] fn test_escape() { assert_eq!(escape("", Html).to_string(), ""); diff --git a/rinja_parser/src/lib.rs b/rinja_parser/src/lib.rs index 9b5d34845..b521dfb01 100644 --- a/rinja_parser/src/lib.rs +++ b/rinja_parser/src/lib.rs @@ -792,9 +792,10 @@ pub fn strip_common(base: &Path, path: &Path) -> String { #[cfg(not(windows))] #[cfg(test)] mod test { - use super::{char_lit, num_lit, strip_common}; use std::path::Path; + use super::{char_lit, num_lit, strip_common}; + #[test] fn test_strip_common() { // Full path is returned instead of empty when the entire path is in common. diff --git a/testing/tests/inheritance.rs b/testing/tests/inheritance.rs index 64415c6d0..b806d8d11 100644 --- a/testing/tests/inheritance.rs +++ b/testing/tests/inheritance.rs @@ -62,9 +62,10 @@ pub mod parent { } pub mod child { - use super::parent::*; use rinja::Template; + use super::parent::*; + #[derive(Template)] #[template(path = "child.html")] pub struct ChildTemplate<'a> { diff --git a/testing/tests/render_in_place.rs b/testing/tests/render_in_place.rs index b09ce45d1..444fb2fa7 100644 --- a/testing/tests/render_in_place.rs +++ b/testing/tests/render_in_place.rs @@ -34,6 +34,6 @@ fn test_render_in_place() { }; assert_eq!( t.render().unwrap(), - "Section 1: A=A\nB=B\nSection 2: C=C\nD=D\nSection 3 for:\n* A=1\nB=2\n* A=A\nB=B\n* A=a\nB=b\n" + "Section 1: A=A\nB=B\nSection 2: C=C\nD=D\nSection 3 for:\n* A=1\nB=2\n* A=A\nB=B\n* A=a\nB=b\n" ); } diff --git a/testing/tests/simple.rs b/testing/tests/simple.rs index 2e8455933..669db8912 100644 --- a/testing/tests/simple.rs +++ b/testing/tests/simple.rs @@ -1,9 +1,9 @@ #![allow(clippy::disallowed_names)] // For the use of `foo` in test cases -use rinja::Template; - use std::collections::HashMap; +use rinja::Template; + #[derive(Template)] #[template(path = "simple.html")] struct VariablesTemplate<'a> { diff --git a/testing/tests/ui.rs b/testing/tests/ui.rs index acd295a8f..bc92bf379 100644 --- a/testing/tests/ui.rs +++ b/testing/tests/ui.rs @@ -2,6 +2,7 @@ use std::os::unix::fs::symlink; use std::path::Path; + use trybuild::TestCases; #[test] diff --git a/testing/tests/whitespace.rs b/testing/tests/whitespace.rs index 98a44403a..5301dbca3 100644 --- a/testing/tests/whitespace.rs +++ b/testing/tests/whitespace.rs @@ -39,7 +39,10 @@ fn test_extra_whitespace() { let mut template = AllowWhitespaces::default(); template.nested_1.nested_2.array = &["a0", "a1", "a2", "a3"]; template.nested_1.nested_2.hash.insert("key", "value"); - assert_eq!(template.render().unwrap(), "\n0\n0\n0\n0\n\n\n\n0\n0\n0\n0\n0\n\na0\na1\nvalue\n\n\n\n\n\n[\n "a0",\n "a1",\n "a2",\n "a3"\n]\n[\n "a0",\n "a1",\n "a2",\n "a3"\n][\n "a0",\n "a1",\n "a2",\n "a3"\n]\n[\n "a1"\n][\n "a1"\n]\n[\n "a1",\n "a2"\n][\n "a1",\n "a2"\n]\n[\n "a1"\n][\n "a1"\n]1-1-1\n3333 3\n2222 2\n0000 0\n3333 3\n\ntruefalse\nfalsefalsefalse\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); + assert_eq!( + template.render().unwrap(), + "\n0\n0\n0\n0\n\n\n\n0\n0\n0\n0\n0\n\na0\na1\nvalue\n\n\n\n\n\n[\n "a0",\n "a1",\n "a2",\n "a3"\n]\n[\n "a0",\n "a1",\n "a2",\n "a3"\n][\n "a0",\n "a1",\n "a2",\n "a3"\n]\n[\n "a1"\n][\n "a1"\n]\n[\n "a1",\n "a2"\n][\n "a1",\n "a2"\n]\n[\n "a1"\n][\n "a1"\n]1-1-1\n3333 3\n2222 2\n0000 0\n3333 3\n\ntruefalse\nfalsefalsefalse\n\n\n\n\n\n\n\n\n\n\n\n\n\n", + ); } macro_rules! test_template_minimize {