Refactor source code with rustfmt and clippy

[skip ci]
This commit is contained in:
Kogia-sima 2020-06-07 17:58:52 +09:00
parent 06c2d10255
commit fd3d224067
16 changed files with 79 additions and 43 deletions

View File

@ -7,13 +7,13 @@ use sailfish::TemplateOnce;
#[template(path = "include.stpl")]
struct Include {
title: String,
name: String
name: String,
}
fn main() {
let ctx = Include {
title: "Website".to_owned(),
name: "Hanako".to_owned()
name: "Hanako".to_owned(),
};
println!("{}", ctx.render_once().unwrap());
}

View File

@ -88,13 +88,18 @@ struct Include<'a> {
#[test]
fn test_include() {
assert_render("include", Include { strs: &["foo", "bar"] });
assert_render(
"include",
Include {
strs: &["foo", "bar"],
},
);
}
#[derive(TemplateOnce)]
#[template(path = "big-table.stpl")]
struct BigTable {
table: Vec<Vec<usize>>
table: Vec<Vec<usize>>,
}
#[test]
@ -151,7 +156,12 @@ struct RmWhitespace<'a, 'b> {
#[test]
fn test_rm_whitespace() {
assert_render("rm_whitespace", RmWhitespace { messages: &["foo", "bar"] });
assert_render(
"rm_whitespace",
RmWhitespace {
messages: &["foo", "bar"],
},
);
}
#[cfg(unix)]

View File

@ -8,12 +8,12 @@ use crate::error::*;
use crate::optimizer::Optimizer;
use crate::parser::Parser;
use crate::resolver::Resolver;
use crate::translator::{Translator, TranslatedSource};
use crate::translator::{TranslatedSource, Translator};
use crate::util::{read_to_string, rustfmt_block};
#[derive(Default)]
pub struct Compiler {
config: Config
config: Config,
}
impl Compiler {
@ -35,7 +35,12 @@ impl Compiler {
translator.translate(stream)
}
pub fn compile_file(&self, template_dir: &Path, input: &Path, output: &Path) -> Result<(), Error> {
pub fn compile_file(
&self,
template_dir: &Path,
input: &Path,
output: &Path,
) -> Result<(), Error> {
// TODO: introduce cache system
let input = input.canonicalize()?;

View File

@ -7,7 +7,7 @@ pub struct Config {
pub cache_dir: PathBuf,
pub rm_whitespace: bool,
#[doc(hidden)]
pub _non_exhaustive: ()
pub _non_exhaustive: (),
}
impl Default for Config {
@ -17,7 +17,7 @@ impl Default for Config {
escape: true,
cache_dir: Path::new(env!("OUT_DIR")).join("cache"),
rm_whitespace: false,
_non_exhaustive: ()
_non_exhaustive: (),
}
}
}

View File

@ -1,5 +1,3 @@
#![allow(dead_code)]
#[macro_use]
mod error;

View File

@ -4,6 +4,7 @@ use syn::visit_mut::VisitMut;
use syn::{Block, Expr, ExprMacro, Ident, LitStr, Stmt, Token};
struct RenderTextMacroArgument {
#[allow(dead_code)]
context: Ident,
arg: LitStr,
}

View File

@ -103,6 +103,7 @@ pub struct ParseStream<'a> {
delimiter: char,
}
#[allow(dead_code)]
impl<'a> ParseStream<'a> {
/// Returns an empty `ParseStream` containing no tokens
pub fn new() -> Self {
@ -220,7 +221,7 @@ impl<'a> ParseStream<'a> {
let end = self
.source
.find(&*self.block_delimiter.0)
.unwrap_or(self.source.len());
.unwrap_or_else(|| self.source.len());
let token = Token {
content: self.take_n(end),
offset,

View File

@ -6,8 +6,8 @@ use syn::parse::{Parse, ParseStream, Result as ParseResult};
use syn::punctuated::Punctuated;
use syn::{Fields, Ident, ItemStruct, Lifetime, LitBool, LitChar, LitStr, Token};
use crate::config::Config;
use crate::compiler::Compiler;
use crate::config::Config;
use crate::error::*;
enum GenericParamName {

View File

@ -23,13 +23,6 @@ macro_rules! return_if_some {
};
}
fn empty_block() -> Block {
Block {
brace_token: Default::default(),
stmts: Vec::new(),
}
}
struct ResolverImpl<'s, 'h> {
template_dir: &'s Path,
path_stack: Vec<PathBuf>,
@ -66,10 +59,11 @@ impl<'s, 'h> VisitMut for ResolverImpl<'s, 'h> {
// resolve include! for rust file
if arg.ends_with(".rs") {
if !arg.starts_with("/") {
let absolute_path = self.path_stack.last().unwrap().parent().unwrap().join(arg);
if !arg.starts_with('/') {
let absolute_path =
self.path_stack.last().unwrap().parent().unwrap().join(arg);
let absolute_path_str = absolute_path.to_string_lossy();
em.mac.tokens = quote!{ include!(#absolute_path_str) };
em.mac.tokens = quote! { include!(#absolute_path_str) };
}
return;
}
@ -81,7 +75,12 @@ impl<'s, 'h> VisitMut for ResolverImpl<'s, 'h> {
self.template_dir.join(&arg[1..])
} else {
// relative include
self.path_stack.last().unwrap().parent().unwrap().join(arg.clone())
self.path_stack
.last()
.unwrap()
.parent()
.unwrap()
.join(arg.clone())
};
// parse and translate the child template
@ -135,13 +134,18 @@ impl<'h> Resolver<'h> {
}
#[inline]
pub fn resolve(&self, template_dir: &Path, input_file: &Path, ast: &mut Block) -> Result<(), Error> {
pub fn resolve(
&self,
template_dir: &Path,
input_file: &Path,
ast: &mut Block,
) -> Result<(), Error> {
let mut child = ResolverImpl {
template_dir: template_dir,
template_dir,
path_stack: vec![input_file.to_owned()],
deps: Vec::new(),
error: None,
include_handler: Arc::clone(&self.include_handler)
include_handler: Arc::clone(&self.include_handler),
};
child.visit_block_mut(ast);

View File

@ -18,10 +18,10 @@ pub struct SourceMap {
}
impl SourceMap {
#[inline]
pub fn entries(&self) -> &[SourceMapEntry] {
&*self.entries
}
// #[inline]
// pub fn entries(&self) -> &[SourceMapEntry] {
// &*self.entries
// }
pub fn reverse_mapping(&self, offset: usize) -> Option<usize> {
// find entry which satisfies entry.new <= offset < entry.new + entry.length

View File

@ -58,9 +58,20 @@ impl Buffer {
self.capacity
}
/// Force the length of buffer to `new_len`
///
/// # Safety
///
/// - `new_len` must be less than or equal to `capacity()`
/// - The elements at `old_len..new_len` must be initialized
#[inline]
pub unsafe fn set_len(&mut self, new: usize) {
self.len = new;
pub unsafe fn set_len(&mut self, new_len: usize) {
self.len = new_len;
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn reserve(&mut self, size: usize) {
@ -145,7 +156,7 @@ impl From<String> for Buffer {
Buffer {
data: other.as_mut_ptr(),
len: other.len(),
capacity: other.capacity()
capacity: other.capacity(),
}
} else {
Buffer::new()

View File

@ -1,3 +1,5 @@
#![allow(clippy::cast_ptr_alignment)]
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]

View File

@ -1,3 +1,5 @@
#![allow(clippy::cast_ptr_alignment)]
use super::naive;
#[cfg(target_pointer_width = "16")]
@ -13,8 +15,8 @@ const USIZE_ALIGN: usize = USIZE_BYTES - 1;
#[inline(always)]
fn contains_zero_byte(x: usize) -> bool {
const LO_U64: u64 = 0x0101010101010101;
const HI_U64: u64 = 0x8080808080808080;
const LO_U64: u64 = 0x0101_0101_0101_0101;
const HI_U64: u64 = 0x8080_8080_8080_8080;
const LO_USIZE: usize = LO_U64 as usize;
const HI_USIZE: usize = HI_U64 as usize;
@ -23,10 +25,10 @@ fn contains_zero_byte(x: usize) -> bool {
#[inline]
fn contains_key(x: usize) -> bool {
const INDEPENDENTS1: usize = 0x0404040404040404_u64 as usize;
const INDEPENDENTS2: usize = 0x0202020202020202_u64 as usize;
const KEY1: usize = 0x2626262626262626_u64 as usize;
const KEY2: usize = 0x3e3e3e3e3e3e3e3e_u64 as usize;
const INDEPENDENTS1: usize = 0x0404_0404_0404_0404_u64 as usize;
const INDEPENDENTS2: usize = 0x0202_0202_0202_0202_u64 as usize;
const KEY1: usize = 0x2626_2626_2626_2626_u64 as usize;
const KEY2: usize = 0x3e3e_3e3e_3e3e_3e3e_u64 as usize;
let y1 = x | INDEPENDENTS1;
let y2 = x | INDEPENDENTS2;

View File

@ -22,7 +22,7 @@ static ESCAPE_LUT: [u8; 256] = [
9, 9, 9, 9,
];
const ESCAPED: [&'static str; 4] = ["&quot;", "&amp;", "&lt;", "&gt;"];
const ESCAPED: [&str; 4] = ["&quot;", "&amp;", "&lt;", "&gt;"];
const ESCAPED_LEN: usize = 4;
/// write the escaped contents with custom function

View File

@ -42,5 +42,5 @@ pub(super) unsafe fn proceed<F: FnMut(&str)>(
debug_assert_eq!(ptr, end_ptr);
debug_assert!(start_ptr <= ptr);
return start_ptr;
start_ptr
}

View File

@ -1,3 +1,5 @@
#![allow(clippy::cast_ptr_alignment)]
#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]