Skip to content

Commit

Permalink
Use nightly rust for formatting to group import statements
Browse files Browse the repository at this point in the history
  • Loading branch information
zoni committed Aug 3, 2024
1 parent bd12200 commit b7a5295
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 44 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
hooks:
- id: rustfmt
name: Check formatting
entry: cargo fmt --
entry: cargo +nightly fmt --
language: system
files: \.rs$
- id: tests
Expand Down
8 changes: 5 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
>
Expand Down
4 changes: 4 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
imports_granularity = "Module"
imports_layout = "HorizontalVertical"
group_imports = "StdExternalCrate"
format_code_in_doc_comments = true
10 changes: 5 additions & 5 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -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,
}
Expand Down
8 changes: 3 additions & 5 deletions src/frontmatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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());
Expand Down
25 changes: 12 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub use pulldown_cmark;
pub use serde_yaml;
pub use {pulldown_cmark, serde_yaml};

#[macro_use]
extern crate lazy_static;
Expand All @@ -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};
Expand All @@ -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<Event<'a>>;
Expand Down Expand Up @@ -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<PathBuf> = vec![
PathBuf::from("NoteA.md"),
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -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");

Expand Down
3 changes: 2 additions & 1 deletion src/postprocessors.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
3 changes: 2 additions & 1 deletion src/references.rs
Original file line number Diff line number Diff line change
@@ -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<file>[^#|]+)??(#(?P<section>.+?))??(\|(?P<label>.+?))??$").unwrap();
Expand Down
8 changes: 5 additions & 3 deletions src/walker.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::{ExportError, WalkDirSnafu};
use ignore::{DirEntry, Walk, WalkBuilder};
use snafu::ResultExt;
use std::fmt;
use std::path::{Path, PathBuf};

use ignore::{DirEntry, Walk, WalkBuilder};
use snafu::ResultExt;

use crate::{ExportError, WalkDirSnafu};

type Result<T, E = ExportError> = std::result::Result<T, E>;
type FilterFn = dyn Fn(&DirEntry) -> bool + Send + Sync + 'static;

Expand Down
10 changes: 5 additions & 5 deletions tests/export_test.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#![allow(clippy::shadow_unrelated)]

use obsidian_export::{ExportError, Exporter, FrontmatterStrategy};
use pretty_assertions::assert_eq;
use std::fs::{create_dir, read_to_string, set_permissions, File, Permissions};
use std::io::prelude::*;
#[cfg(not(target_os = "windows"))]
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;

use obsidian_export::{ExportError, Exporter, FrontmatterStrategy};
use pretty_assertions::assert_eq;
use tempfile::TempDir;
use walkdir::WalkDir;

#[cfg(not(target_os = "windows"))]
use std::os::unix::fs::PermissionsExt;

#[test]
fn test_main_variants_with_default_options() {
let tmp_dir = TempDir::new().expect("failed to make tempdir");
Expand Down
9 changes: 5 additions & 4 deletions tests/postprocessors_test.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::collections::HashSet;
use std::fs::{read_to_string, remove_file};
use std::path::PathBuf;
use std::sync::Mutex;

use obsidian_export::postprocessors::{filter_by_tags, softbreaks_to_hardbreaks};
use obsidian_export::{Context, Exporter, MarkdownEvents, PostprocessorResult};
use pretty_assertions::assert_eq;
use pulldown_cmark::{CowStr, Event};
use serde_yaml::Value;
use std::collections::HashSet;
use std::fs::{read_to_string, remove_file};
use std::path::PathBuf;
use std::sync::Mutex;
use tempfile::TempDir;
use walkdir::WalkDir;

Expand Down

0 comments on commit b7a5295

Please sign in to comment.