From 0f3ae894f167cb8987c4279800cccbac6d286c3a Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Fri, 16 Sep 2022 22:24:46 +0530 Subject: [PATCH] Use formatter from nightly --- build.rs | 2 +- rustfmt.toml | 12 +++++------- src/cmd/add.rs | 6 ++++-- src/cmd/{_cmd.rs => cmd.rs} | 5 ++++- src/cmd/import.rs | 6 ++++-- src/cmd/init.rs | 8 +++++--- src/cmd/mod.rs | 6 +++--- src/cmd/query.rs | 6 ++++-- src/cmd/remove.rs | 6 ++++-- src/config.rs | 8 +++++--- src/db/dir.rs | 10 ++++++---- src/db/mod.rs | 8 ++++---- src/db/stream.rs | 11 +++++++---- src/error.rs | 3 ++- src/main.rs | 8 +++++--- src/shell.rs | 3 ++- src/util.rs | 31 ++++++++++++++----------------- xtask/src/main.rs | 11 ++++++----- 18 files changed, 85 insertions(+), 65 deletions(-) rename src/cmd/{_cmd.rs => cmd.rs} (98%) diff --git a/build.rs b/build.rs index 90075d6..aa561b2 100644 --- a/build.rs +++ b/build.rs @@ -32,7 +32,7 @@ fn git_version() -> Option { } fn generate_completions() -> io::Result<()> { - #[path = "src/cmd/_cmd.rs"] + #[path = "src/cmd/cmd.rs"] mod cmd; use clap::CommandFactory; diff --git a/rustfmt.toml b/rustfmt.toml index 450ab8a..5106da6 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,12 +1,10 @@ -# comment_width = 100 -# error_on_line_overflow = true -# error_on_unformatted = true -# group_imports = "StdExternalCrate" -# imports_granularity = "Module" +comment_width = 100 +group_imports = "StdExternalCrate" +imports_granularity = "Module" max_width = 120 newline_style = "Native" use_field_init_shorthand = true use_small_heuristics = "Max" use_try_shorthand = true -# wrap_comments = true -# version = "Two" +wrap_comments = true +version = "Two" diff --git a/src/cmd/add.rs b/src/cmd/add.rs index f33d8db..34ba0c0 100644 --- a/src/cmd/add.rs +++ b/src/cmd/add.rs @@ -1,8 +1,10 @@ +use std::path::Path; + +use anyhow::{bail, Result}; + use crate::cmd::{Add, Run}; use crate::db::DatabaseFile; use crate::{config, util}; -use anyhow::{bail, Result}; -use std::path::Path; impl Run for Add { fn run(&self) -> Result<()> { diff --git a/src/cmd/_cmd.rs b/src/cmd/cmd.rs similarity index 98% rename from src/cmd/_cmd.rs rename to src/cmd/cmd.rs index 9ac5dc7..59775c6 100644 --- a/src/cmd/_cmd.rs +++ b/src/cmd/cmd.rs @@ -1,6 +1,9 @@ -use clap::{ArgEnum, Parser, ValueHint}; +#![allow(clippy::module_inception)] + use std::path::PathBuf; +use clap::{ArgEnum, Parser, ValueHint}; + const ENV_HELP: &str = "ENVIRONMENT VARIABLES: _ZO_DATA_DIR Path for zoxide data files _ZO_ECHO Print the matched directory before navigating to it when set to 1 diff --git a/src/cmd/import.rs b/src/cmd/import.rs index b9e6299..b5a2fd2 100644 --- a/src/cmd/import.rs +++ b/src/cmd/import.rs @@ -1,8 +1,10 @@ +use std::fs; + +use anyhow::{bail, Context, Result}; + use crate::cmd::{Import, ImportFrom, Run}; use crate::config; use crate::db::{Database, DatabaseFile, Dir}; -use anyhow::{bail, Context, Result}; -use std::fs; impl Run for Import { fn run(&self) -> Result<()> { diff --git a/src/cmd/init.rs b/src/cmd/init.rs index b8ae591..32f6896 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -1,10 +1,12 @@ +use std::io::{self, Write}; + +use anyhow::{Context, Result}; +use askama::Template; + use crate::cmd::{Init, InitShell, Run}; use crate::config; use crate::error::BrokenPipeHandler; use crate::shell::{self, Opts}; -use anyhow::{Context, Result}; -use askama::Template; -use std::io::{self, Write}; impl Run for Init { fn run(&self) -> Result<()> { diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index ffdd868..876a6aa 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,14 +1,14 @@ -mod _cmd; mod add; +mod cmd; mod import; mod init; mod query; mod remove; -pub use crate::cmd::_cmd::*; - use anyhow::Result; +pub use crate::cmd::cmd::*; + pub trait Run { fn run(&self) -> Result<()>; } diff --git a/src/cmd/query.rs b/src/cmd/query.rs index 4f509ba..b6a9657 100644 --- a/src/cmd/query.rs +++ b/src/cmd/query.rs @@ -1,10 +1,12 @@ +use std::io::{self, Write}; + +use anyhow::{Context, Result}; + use crate::cmd::{Query, Run}; use crate::config; use crate::db::{Database, DatabaseFile}; use crate::error::BrokenPipeHandler; use crate::util::{self, Fzf}; -use anyhow::{Context, Result}; -use std::io::{self, Write}; impl Run for Query { fn run(&self) -> Result<()> { diff --git a/src/cmd/remove.rs b/src/cmd/remove.rs index a3fd695..45b1c9b 100644 --- a/src/cmd/remove.rs +++ b/src/cmd/remove.rs @@ -1,9 +1,11 @@ +use std::io::{self, Write}; + +use anyhow::{bail, Context, Result}; + use crate::cmd::{Remove, Run}; use crate::config; use crate::db::DatabaseFile; use crate::util::{self, Fzf}; -use anyhow::{bail, Context, Result}; -use std::io::{self, Write}; impl Run for Remove { fn run(&self) -> Result<()> { diff --git a/src/config.rs b/src/config.rs index e4fb6be..c5cb6ae 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,10 +1,12 @@ -use crate::db::Rank; -use anyhow::{Context, Result}; -use glob::Pattern; use std::env; use std::ffi::OsString; use std::path::PathBuf; +use anyhow::{Context, Result}; +use glob::Pattern; + +use crate::db::Rank; + pub fn data_dir() -> Result { let path = match env::var_os("_ZO_DATA_DIR") { Some(path) => PathBuf::from(path), diff --git a/src/db/dir.rs b/src/db/dir.rs index e2eee3d..c59a441 100644 --- a/src/db/dir.rs +++ b/src/db/dir.rs @@ -1,10 +1,11 @@ -use anyhow::{bail, Context, Result}; -use bincode::Options as _; -use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::fmt::{self, Display, Formatter}; use std::ops::{Deref, DerefMut}; +use anyhow::{bail, Context, Result}; +use bincode::Options as _; +use serde::{Deserialize, Serialize}; + #[derive(Debug, Deserialize, Serialize)] pub struct DirList<'a>(#[serde(borrow)] pub Vec>); @@ -141,9 +142,10 @@ pub type Epoch = u64; #[cfg(test)] mod tests { - use super::*; use std::borrow::Cow; + use super::*; + #[test] fn zero_copy() { let dirs = DirList(vec![Dir { path: "/".into(), rank: 0.0, last_accessed: 0 }]); diff --git a/src/db/mod.rs b/src/db/mod.rs index 1c3f2d3..cf9b54a 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,14 +1,14 @@ mod dir; mod stream; +use std::path::{Path, PathBuf}; +use std::{fs, io}; + +use anyhow::{Context, Result}; pub use dir::{Dir, DirList, Epoch, Rank}; pub use stream::Stream; use crate::util; -use anyhow::{Context, Result}; -use std::fs; -use std::io; -use std::path::{Path, PathBuf}; #[derive(Debug)] pub struct Database<'file> { diff --git a/src/db/stream.rs b/src/db/stream.rs index 044be32..275d52e 100644 --- a/src/db/stream.rs +++ b/src/db/stream.rs @@ -1,9 +1,10 @@ -use crate::db::{Database, Dir, Epoch}; -use crate::util; use std::iter::Rev; use std::ops::Range; use std::{fs, path}; +use crate::db::{Database, Dir, Epoch}; +use crate::util; + pub struct Stream<'db, 'file> { db: &'db mut Database<'file>, idxs: Rev>, @@ -119,10 +120,12 @@ impl<'db, 'file> Stream<'db, 'file> { #[cfg(test)] mod tests { - use super::*; - use rstest::rstest; use std::path::PathBuf; + use rstest::rstest; + + use super::*; + #[rstest] // Case normalization #[case(&["fOo", "bAr"], "/foo/bar", true)] diff --git a/src/error.rs b/src/error.rs index 563689b..d2baa7f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,8 @@ -use anyhow::{bail, Context, Result}; use std::fmt::{self, Display, Formatter}; use std::io; +use anyhow::{bail, Context, Result}; + /// Custom error type for early exit. #[derive(Debug)] pub struct SilentExit { diff --git a/src/main.rs b/src/main.rs index e22293a..6c7545f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,13 +11,15 @@ mod error; mod shell; mod util; -use crate::cmd::{Cmd, Run}; -use crate::error::SilentExit; -use clap::Parser; use std::env; use std::io::{self, Write}; use std::process::ExitCode; +use clap::Parser; + +use crate::cmd::{Cmd, Run}; +use crate::error::SilentExit; + pub fn main() -> ExitCode { // Forcibly disable backtraces. env::remove_var("RUST_LIB_BACKTRACE"); diff --git a/src/shell.rs b/src/shell.rs index 2767b8a..5662aa0 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -35,12 +35,13 @@ make_template!(Zsh, "zsh.txt"); #[cfg(feature = "nix-dev")] #[cfg(test)] mod tests { - use super::*; use askama::Template; use assert_cmd::Command; use rstest::rstest; use rstest_reuse::{apply, template}; + use super::*; + #[template] #[rstest] fn opts( diff --git a/src/util.rs b/src/util.rs index b3b7fe0..d2f1fdb 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,17 +1,17 @@ -use crate::config; -use crate::db::Epoch; -use crate::error::SilentExit; +use std::fs::{self, File, OpenOptions}; +use std::io::{self, Read, Write}; +use std::path::{Component, Path, PathBuf}; +use std::process::{Child, ChildStdin, Command, Stdio}; +use std::time::SystemTime; +use std::{env, mem}; + #[cfg(windows)] use anyhow::anyhow; use anyhow::{bail, Context, Result}; -use std::env; -use std::fs::{self, File, OpenOptions}; -use std::io::{self, Read, Write}; -use std::mem; -use std::path::{Component, Path, PathBuf}; -use std::process::Command; -use std::process::{Child, ChildStdin, Stdio}; -use std::time::SystemTime; + +use crate::config; +use crate::db::Epoch; +use crate::error::SilentExit; pub struct Fzf { child: Child, @@ -115,10 +115,11 @@ pub fn write, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> // Set the owner of the tmpfile (UNIX only). #[cfg(unix)] if let Ok(metadata) = path.metadata() { - use nix::unistd::{self, Gid, Uid}; use std::os::unix::fs::MetadataExt; use std::os::unix::io::AsRawFd; + use nix::unistd::{self, Gid, Uid}; + let uid = Uid::from_raw(metadata.uid()); let gid = Gid::from_raw(metadata.gid()); let _ = unistd::fchown(tmp_file.as_raw_fd(), Some(uid), Some(gid)); @@ -312,9 +313,5 @@ pub fn resolve_path>(path: &P) -> Result { /// Convert a string to lowercase, with a fast path for ASCII strings. pub fn to_lowercase>(s: S) -> String { let s = s.as_ref(); - if s.is_ascii() { - s.to_ascii_lowercase() - } else { - s.to_lowercase() - } + if s.is_ascii() { s.to_ascii_lowercase() } else { s.to_lowercase() } } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index f6f1017..424f363 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,11 +1,12 @@ -use anyhow::{bail, Context, Result}; -use clap::Parser; -use ignore::Walk; use std::env; use std::ffi::OsStr; use std::path::PathBuf; use std::process::{self, Command}; +use anyhow::{bail, Context, Result}; +use clap::Parser; +use ignore::Walk; + fn main() -> Result<()> { let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let dir = dir.parent().with_context(|| format!("could not find workspace root: {}", dir.display()))?; @@ -62,8 +63,8 @@ fn run_ci(nix_enabled: bool) -> Result<()> { fn run_fmt(nix_enabled: bool, check: bool) -> Result<()> { // Run cargo-fmt. - let check_args: &[&str] = if check { &["--check", "--files-with-diff"] } else { &[] }; - Command::new("cargo").args(&["fmt", "--all", "--"]).args(check_args).run()?; + // let check_args: &[&str] = if check { &["--check", "--files-with-diff"] } else { &[] }; + // Command::new("cargo").args(&["fmt", "--all", "--"]).args(check_args).run()?; // Run nixfmt. if nix_enabled {