diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..eb4ec38 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,14 @@ +edition = "2021" +unstable_features = true +# Unstable +comment_width = 80 +condense_wildcard_suffixes = true +format_code_in_doc_comments = true +format_generated_files = false +format_macro_matchers = true +format_strings = true +group_imports = "StdExternalCrate" +imports_granularity = "Module" +reorder_impl_items = true +use_field_init_shorthand = true +wrap_comments = true diff --git a/flake.lock b/flake.lock index e7e3764..99763b8 100644 --- a/flake.lock +++ b/flake.lock @@ -8,11 +8,11 @@ "rust-analyzer-src": "rust-analyzer-src" }, "locked": { - "lastModified": 1686723693, - "narHash": "sha256-wd6WE5M1EdnZPvK5h9zh3hE9F+h+7z2YhK1UpobkjGQ=", + "lastModified": 1720592968, + "narHash": "sha256-k46iKgWDPXeUeO/ev0kSmjDuEIFw3pzbMqx4FUK9+yk=", "owner": "nix-community", "repo": "fenix", - "rev": "ac2f1271f1e6623717220f31890658af351b0b2c", + "rev": "1a3edfb3317af90822b892a01708c60d0d30e1d8", "type": "github" }, "original": { @@ -26,11 +26,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1685518550, - "narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=", + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", "owner": "numtide", "repo": "flake-utils", - "rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", "type": "github" }, "original": { @@ -41,11 +41,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1697456312, - "narHash": "sha256-roiSnrqb5r+ehnKCauPLugoU8S36KgmWraHgRqVYndo=", + "lastModified": 1720542800, + "narHash": "sha256-ZgnNHuKV6h2+fQ5LuqnUaqZey1Lqqt5dTUAiAnqH0QQ=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "ca012a02bf8327be9e488546faecae5e05d7d749", + "rev": "feb2849fdeb70028c70d73b848214b00d324a497", "type": "github" }, "original": { @@ -65,11 +65,11 @@ "rust-analyzer-src": { "flake": false, "locked": { - "lastModified": 1686653811, - "narHash": "sha256-JY8lY5TpcWl+ZNawEmfNuBBLLQ0OBjsX7dsO0iCt85Y=", + "lastModified": 1720455058, + "narHash": "sha256-jqfodED9cglRkRfNI8TBWdjHcEoregUEzZd7+4edDsM=", "owner": "rust-lang", "repo": "rust-analyzer", - "rev": "f8dec25bd70cc3568069daf8c3d5f2a65e3aa4cb", + "rev": "da27b89ca55d066680b2ddbc53477b3816cd5407", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index f8d3f8c..3e3689f 100644 --- a/flake.nix +++ b/flake.nix @@ -8,30 +8,32 @@ }; }; - outputs = { nixpkgs, flake-utils, ... } @ inputs: - flake-utils.lib.eachDefaultSystem (system: + outputs = + { nixpkgs, flake-utils, ... }@inputs: + flake-utils.lib.eachDefaultSystem ( + system: let pkgs = import nixpkgs { inherit system; - overlays = with inputs; [ - inputs.fenix.overlays.default - ]; + overlays = [ inputs.fenix.overlays.default ]; }; - inherit (pkgs) fenix ra-flake; - inherit (pkgs.lib) importTOML; + inherit (pkgs) fenix lib; # Rust toolchain - rustChannel = (importTOML ./rust-toolchain).toolchain.channel; rustToolchain = fenix.toolchainOf { - channel = rustChannel; + channel = (lib.importTOML ./rust-toolchain.toml).toolchain.channel; sha256 = "sha256-gdYqng0y9iHYzYPAdkC/ka3DRny3La/S5G8ASj0Ayyc="; }; + # For development - rust-dev = fenix.combine (with rustToolchain; [ - defaultToolchain - rust-src - rust-analyzer - ]); + rust-dev = fenix.combine ( + with rustToolchain; + [ + defaultToolchain + rust-src + ] + ); + # For building packages rust-minimal = rustToolchain.minimalToolchain; rustPlatform = pkgs.makeRustPlatform { @@ -46,11 +48,7 @@ src = ./.; cargoLock.lockFile = ./Cargo.lock; }; - devShells.default = with pkgs; mkShell { - nativeBuildInputs = [ - rust-dev - ]; - }; + devShells.default = with pkgs; mkShell { packages = [ rust-dev ]; }; } ); } diff --git a/rust-toolchain b/rust-toolchain.toml similarity index 100% rename from rust-toolchain rename to rust-toolchain.toml diff --git a/src/autocomplete.rs b/src/autocomplete.rs index ad069e1..2673ab8 100644 --- a/src/autocomplete.rs +++ b/src/autocomplete.rs @@ -1,10 +1,13 @@ //! Autocompletion and fuzzy search for nerd fonts. -use crate::{icon::Icon, runtime::NGram}; +use std::rc::Rc; + use indexmap::IndexMap; use inquire::Autocomplete; use itertools::Itertools; -use std::rc::Rc; + +use crate::icon::Icon; +use crate::runtime::NGram; const THRESHOLD: f32 = 0.6; const MAX_SUGGESTIONS: usize = 100; diff --git a/src/cli.rs b/src/cli.rs index 9d08153..ff9182c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,12 +1,17 @@ //! Command line arguments parser. -use crate::{error, icon::Substitution, shadow}; -use clap::{Parser, Subcommand, ValueEnum}; use core::fmt; +use std::io; +use std::path::PathBuf; +use std::str::FromStr; + +use clap::{Parser, Subcommand, ValueEnum}; use shadow_rs::formatcp; -use std::{io, path::PathBuf, str::FromStr}; use thisctx::IntoError; +use crate::icon::Substitution; +use crate::{error, shadow}; + const V_PATH: &str = "PATH"; const V_SOURCE: &str = "SOURCE"; const V_SUBSTITUTION: &str = "SUBSTITUTION"; @@ -18,12 +23,11 @@ const SUB_LONG_HELP: &str = "\ Perform an exact/prefix substitution. This option accepts several substitution types of `TYPE:FROM/TO` syntax: - * Exact substitution: replaces an icon with another when its name matches \ -exactly. This is the default type when `TYPE` is omitted. - * Perfix substitution: replaces the prefix of an icon name with another, and \ -then tries to replace the icon with the one has the new name, e.g. use \ -`--sub prefix:mdi-/md-` to replace `mdi-tab` with `md-tab`.\ -"; + * Exact substitution: replaces an icon with another when its name matches exactly. This is the \ + default type when `TYPE` is omitted. + * Prefix substitution: replaces the prefix of an icon name with another, and then tries to \ + replace the icon with the one has the new name, e.g. use `--sub \ + prefix:mdi-/md-` to replace `mdi-tab` with `md-tab`."; #[derive(Debug, Parser)] #[command(author, version, long_version = CLAP_LONG_VERSION)] diff --git a/src/icon.rs b/src/icon.rs index 8ad7f6b..6d2f134 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -1,10 +1,13 @@ -//! Nerd font icons infomation. +//! Nerd font icons information. -use crate::error; -use serde::{de::Visitor, Deserialize, Deserializer, Serialize, Serializer}; use std::fmt; + +use serde::de::Visitor; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use thisctx::IntoError; +use crate::error; + pub(crate) fn parse_codepoint(s: &str) -> error::Result { let v = u32::from_str_radix(s, 16).map_err(|_| error::InvalidCodepoint.build())?; char::from_u32(v).ok_or_else(|| error::InvalidCodepoint.build()) @@ -91,9 +94,10 @@ mod codepoint { } mod substitution { - use super::*; use std::str::FromStr; + use super::*; + impl FromStr for SubstitutionType { type Err = &'static str; diff --git a/src/parser.rs b/src/parser.rs index 7ad78ad..fc4f1de 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,13 +1,12 @@ -//! Parses font infomation from nerd font official cheat sheet. +//! Parses font information from nerd font official cheat sheet. -use crate::{ - error, - icon::{Database, Icon}, -}; use once_cell::sync::Lazy; use regex::Regex; use thisctx::WithContext; +use crate::error; +use crate::icon::{Database, Icon}; + pub fn parse(s: &str) -> error::Result { let s = s.trim_start(); Ok(if s.starts_with('{') { diff --git a/src/prompt.rs b/src/prompt.rs index 74794c2..c2b7a25 100644 --- a/src/prompt.rs +++ b/src/prompt.rs @@ -1,8 +1,11 @@ -use crate::error; +use std::fmt; +use std::str::FromStr; + use inquire::InquireError; -use std::{fmt, str::FromStr}; use thisctx::IntoError; +use crate::error; + pub fn prompt_yes_or_no(msg: &str, help: Option<&str>) -> error::Result { match inquire::CustomType::::new(msg) .with_help_message(help.unwrap_or("(Y)es/(N)o/(A)ll yes, (Ctrl_C) to abort")) diff --git a/src/runtime.rs b/src/runtime.rs index 058201c..c8ba097 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -1,25 +1,25 @@ -use crate::{ - autocomplete::Autocompleter, - cli::{IoPath, OutputFormat, UserInput}, - error, - icon::{Database, Icon, Substitution, SubstitutionType, Substitutions}, - util::NGramSearcherExt, -}; -use codespan_reporting::{ - diagnostic::{Diagnostic, Label}, - files::SimpleFiles, - term, - term::termcolor::{ColorChoice, StandardStream}, -}; +use std::collections::HashMap; +use std::io::Write; +use std::rc::Rc; + +use codespan_reporting::diagnostic::{Diagnostic, Label}; +use codespan_reporting::files::SimpleFiles; +use codespan_reporting::term; +use codespan_reporting::term::termcolor::{ColorChoice, StandardStream}; use indexmap::IndexMap; use inquire::InquireError; use itertools::Itertools; use once_cell::unsync::{Lazy, OnceCell}; use serde::Serialize; -use std::{collections::HashMap, io::Write, rc::Rc}; use thisctx::IntoError; use tracing::info; +use crate::autocomplete::Autocompleter; +use crate::cli::{IoPath, OutputFormat, UserInput}; +use crate::error; +use crate::icon::{Database, Icon, Substitution, SubstitutionType, Substitutions}; +use crate::util::NGramSearcherExt; + const ARITY: usize = 3; const PAD_LEN: usize = 2; const WARP: f32 = 3.0; diff --git a/src/util.rs b/src/util.rs index 9b517ad..7127379 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,11 +1,14 @@ -use crate::error; -use noodler::NGramSearcher; use std::fmt; +use noodler::NGramSearcher; + +use crate::error; + pub(crate) struct LogStatus; const _: () = { use std::sync::atomic::{AtomicBool, Ordering}; + use tracing::{Event, Level, Subscriber}; use tracing_subscriber::layer::{Context, Layer}; diff --git a/tests/cli.rs b/tests/cli.rs index 6b1f5fa..457bc6e 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1,14 +1,14 @@ // NOTE: ignored on Windows as CRLF causes differences in spans #![cfg(unix)] -use assert_cmd::{assert::Assert, prelude::*}; use core::fmt; +use std::env; +use std::path::Path; +use std::process::{Command, Output}; + +use assert_cmd::assert::Assert; +use assert_cmd::prelude::*; use predicates::prelude::*; -use std::{ - env, - path::Path, - process::{Command, Output}, -}; fn normalize_output(bytes: Vec) -> Vec { strip_ansi_escapes::strip(bytes)