Skip to content

Commit

Permalink
Add .rustfmt.toml configuration
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
Kijewski committed Jul 4, 2024
1 parent 37c300d commit ee3a9e6
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 39 deletions.
12 changes: 12 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -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",
]
1 change: 0 additions & 1 deletion rinja/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub type Result<I, E = Error> = std::result::Result<I, E>;
/// 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 {
Expand Down
6 changes: 2 additions & 4 deletions rinja/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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")]
Expand Down Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions rinja_axum/tests/basic.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 3 additions & 3 deletions rinja_derive/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -65,7 +65,7 @@ impl<'a> Config<'a> {
return Err(CompileError::new(
format!("invalid value for `whitespace`: \"{s}\""),
file_info,
))
));
}
};
}
Expand Down
22 changes: 11 additions & 11 deletions rinja_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -240,7 +240,7 @@ impl<'a> Generator<'a> {
));
}

/* Helper methods for handling node types */
// Helper methods for handling node types

fn handle(
&mut self,
Expand Down Expand Up @@ -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));
}
};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions rinja_derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl TemplateInput<'_> {
(&Source::Source(_), None) => {
return Err(CompileError::no_file_info(
"must include 'ext' attribute when using 'source' attribute",
))
));
}
};

Expand Down Expand Up @@ -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}"
)))
)));
}
};
}
Expand All @@ -264,7 +264,7 @@ impl TemplateArgs {
return Err(CompileError::no_file_info(format!(
"unsupported attribute argument {:?}",
item.to_token_stream()
)))
)));
}
};

Expand All @@ -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:?}"
)))
)));
}
};

Expand Down Expand Up @@ -440,7 +440,7 @@ impl FromStr for Print {
v => {
return Err(CompileError::no_file_info(format!(
"invalid value for print option: {v}"
)))
)));
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion rinja_escape/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "");
Expand Down
3 changes: 2 additions & 1 deletion rinja_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion testing/tests/inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion testing/tests/render_in_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
4 changes: 2 additions & 2 deletions testing/tests/simple.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down
1 change: 1 addition & 0 deletions testing/tests/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::os::unix::fs::symlink;
use std::path::Path;

use trybuild::TestCases;

#[test]
Expand Down
5 changes: 4 additions & 1 deletion testing/tests/whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 &quot;a0&quot;,\n &quot;a1&quot;,\n &quot;a2&quot;,\n &quot;a3&quot;\n]\n[\n &quot;a0&quot;,\n &quot;a1&quot;,\n &quot;a2&quot;,\n &quot;a3&quot;\n][\n &quot;a0&quot;,\n &quot;a1&quot;,\n &quot;a2&quot;,\n &quot;a3&quot;\n]\n[\n &quot;a1&quot;\n][\n &quot;a1&quot;\n]\n[\n &quot;a1&quot;,\n &quot;a2&quot;\n][\n &quot;a1&quot;,\n &quot;a2&quot;\n]\n[\n &quot;a1&quot;\n][\n &quot;a1&quot;\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 &quot;a0&quot;,\n &quot;a1&quot;,\n &quot;a2&quot;,\n &quot;a3&quot;\n]\n[\n &quot;a0&quot;,\n &quot;a1&quot;,\n &quot;a2&quot;,\n &quot;a3&quot;\n][\n &quot;a0&quot;,\n &quot;a1&quot;,\n &quot;a2&quot;,\n &quot;a3&quot;\n]\n[\n &quot;a1&quot;\n][\n &quot;a1&quot;\n]\n[\n &quot;a1&quot;,\n &quot;a2&quot;\n][\n &quot;a1&quot;,\n &quot;a2&quot;\n]\n[\n &quot;a1&quot;\n][\n &quot;a1&quot;\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 {
Expand Down

0 comments on commit ee3a9e6

Please sign in to comment.