From b7a5295acaebc60f5d5d81beaa99b59c8c0038e7 Mon Sep 17 00:00:00 2001 From: Nick Groenen Date: Sat, 3 Aug 2024 13:38:23 +0200 Subject: [PATCH] Use nightly rust for formatting to group import statements --- .github/workflows/ci.yml | 4 ++-- .pre-commit-config.yaml | 2 +- CONTRIBUTING.md | 8 +++++--- rustfmt.toml | 4 ++++ src/context.rs | 10 +++++----- src/frontmatter.rs | 8 +++----- src/lib.rs | 25 ++++++++++++------------- src/main.rs | 4 +++- src/postprocessors.rs | 3 ++- src/references.rs | 3 ++- src/walker.rs | 8 +++++--- tests/export_test.rs | 10 +++++----- tests/postprocessors_test.rs | 9 +++++---- 13 files changed, 54 insertions(+), 44 deletions(-) create mode 100644 rustfmt.toml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 29a50e1..9246199 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: strategy: matrix: job: - - cargo fmt --all -- --check + - rustup toolchain install nightly --profile minimal --component rustfmt && cargo fmt --all -- --check - cargo test --all-targets --all-features - cargo clippy --all-targets --all-features -- -D warning fail-fast: false @@ -54,7 +54,7 @@ jobs: needs: populate-rust-cache env: # These hooks are expensive and already run as dedicated jobs above - SKIP: "tests,clippy" + SKIP: "rustfmt,tests,clippy" steps: - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a4e28c3..6b63919 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,7 @@ repos: hooks: - id: rustfmt name: Check formatting - entry: cargo fmt -- + entry: cargo +nightly fmt -- language: system files: \.rs$ - id: tests diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8b0b377..3c68cd0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -35,9 +35,11 @@ You can see some examples of this in: ## Conventions -Code is formatted with [rustfmt](https://github.com/rust-lang/rustfmt) using the default options. -In addition, all default [clippy](https://github.com/rust-lang/rust-clippy) checks on the latest stable Rust compiler must also pass. -Both of these are enforced through CI using GitHub actions. +Code is formatted with [rustfmt](https://github.com/rust-lang/rustfmt). +The nightly toolchain is used for this as things like sorting of imports is not yet available on stable yet. +If you don't have the nightly toolchain installed, run: `rustup toolchain install nightly --component rustfmt` + +In addition, [clippy](https://github.com/rust-lang/rust-clippy) is configured to be quite pedantic and all of its checks must also pass for CI builds to succeed. > **💡 Tip: install pre-commit hooks** > diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..0206013 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,4 @@ +imports_granularity = "Module" +imports_layout = "HorizontalVertical" +group_imports = "StdExternalCrate" +format_code_in_doc_comments = true diff --git a/src/context.rs b/src/context.rs index 64c4621..4773d44 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,6 +1,7 @@ -use crate::Frontmatter; use std::path::{Path, PathBuf}; +use crate::Frontmatter; + #[derive(Debug, Clone)] /// Context holds metadata about a note which is being parsed. /// @@ -36,10 +37,9 @@ pub struct Context { /// # let mut context = Context::new(PathBuf::from("source"), PathBuf::from("destination")); /// let key = Value::String("foo".to_string()); /// - /// context.frontmatter.insert( - /// key.clone(), - /// Value::String("bar".to_string()), - /// ); + /// context + /// .frontmatter + /// .insert(key.clone(), Value::String("bar".to_string())); /// ``` pub frontmatter: Frontmatter, } diff --git a/src/frontmatter.rs b/src/frontmatter.rs index e80e8bf..5f039a6 100644 --- a/src/frontmatter.rs +++ b/src/frontmatter.rs @@ -14,10 +14,7 @@ use serde_yaml::Result; /// let mut frontmatter = Frontmatter::new(); /// let key = Value::String("foo".to_string()); /// -/// frontmatter.insert( -/// key.clone(), -/// Value::String("bar".to_string()), -/// ); +/// frontmatter.insert(key.clone(), Value::String("bar".to_string())); /// /// assert_eq!( /// frontmatter.get(&key), @@ -67,10 +64,11 @@ pub enum FrontmatterStrategy { #[cfg(test)] mod tests { - use super::*; use pretty_assertions::assert_eq; use serde_yaml::Value; + use super::*; + #[test] fn empty_string_should_yield_empty_frontmatter() { assert_eq!(frontmatter_from_str("").unwrap(), Frontmatter::new()); diff --git a/src/lib.rs b/src/lib.rs index 7cfc7c8..a278d26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ -pub use pulldown_cmark; -pub use serde_yaml; +pub use {pulldown_cmark, serde_yaml}; #[macro_use] extern crate lazy_static; @@ -10,11 +9,16 @@ pub mod postprocessors; mod references; mod walker; -pub use context::Context; -pub use frontmatter::{Frontmatter, FrontmatterStrategy}; -pub use walker::{vault_contents, WalkOptions}; +use std::ffi::OsString; +use std::fs::{self, File}; +use std::io::prelude::*; +use std::io::ErrorKind; +use std::path::{Path, PathBuf}; +use std::{fmt, str}; +pub use context::Context; use frontmatter::{frontmatter_from_str, frontmatter_to_str}; +pub use frontmatter::{Frontmatter, FrontmatterStrategy}; use pathdiff::diff_paths; use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS}; use pulldown_cmark::{CodeBlockKind, CowStr, Event, HeadingLevel, Options, Parser, Tag}; @@ -23,14 +27,8 @@ use rayon::prelude::*; use references::{ObsidianNoteReference, RefParser, RefParserState, RefType}; use slug::slugify; use snafu::{ResultExt, Snafu}; -use std::ffi::OsString; -use std::fmt; -use std::fs::{self, File}; -use std::io::prelude::*; -use std::io::ErrorKind; -use std::path::{Path, PathBuf}; -use std::str; use unicode_normalization::UnicodeNormalization; +pub use walker::{vault_contents, WalkOptions}; /// A series of markdown [Event]s that are generated while traversing an Obsidian markdown note. pub type MarkdownEvents<'a> = Vec>; @@ -897,10 +895,11 @@ fn codeblock_kind_to_owned<'a>(codeblock_kind: CodeBlockKind<'_>) -> CodeBlockKi #[cfg(test)] mod tests { - use super::*; use pretty_assertions::assert_eq; use rstest::rstest; + use super::*; + lazy_static! { static ref VAULT: Vec = vec![ PathBuf::from("NoteA.md"), diff --git a/src/main.rs b/src/main.rs index 273af39..7c4ff19 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ +use std::env; +use std::path::PathBuf; + use eyre::{eyre, Result}; use gumdrop::Options; use obsidian_export::postprocessors::{filter_by_tags, softbreaks_to_hardbreaks}; use obsidian_export::{ExportError, Exporter, FrontmatterStrategy, WalkOptions}; -use std::{env, path::PathBuf}; const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/src/postprocessors.rs b/src/postprocessors.rs index 16b3786..966baca 100644 --- a/src/postprocessors.rs +++ b/src/postprocessors.rs @@ -1,9 +1,10 @@ //! A collection of officially maintained [postprocessors][crate::Postprocessor]. -use super::{Context, MarkdownEvents, PostprocessorResult}; use pulldown_cmark::Event; use serde_yaml::Value; +use super::{Context, MarkdownEvents, PostprocessorResult}; + /// This postprocessor converts all soft line breaks to hard line breaks. Enabling this mimics /// Obsidian's _'Strict line breaks'_ setting. pub fn softbreaks_to_hardbreaks( diff --git a/src/references.rs b/src/references.rs index f5b04ee..1d7e731 100644 --- a/src/references.rs +++ b/src/references.rs @@ -1,6 +1,7 @@ -use regex::Regex; use std::fmt; +use regex::Regex; + lazy_static! { static ref OBSIDIAN_NOTE_LINK_RE: Regex = Regex::new(r"^(?P[^#|]+)??(#(?P
.+?))??(\|(?P