diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3d0cac79d..a5396f71a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,7 +52,7 @@ jobs: - uses: Swatinem/rust-cache@v2 if: github.event_name != 'release' && github.event_name != 'workflow_dispatch' - run: just env-info - - run: just fmt + - run: just test-fmt - run: just clippy - run: just check - run: just check-doc diff --git a/justfile b/justfile index 419ebbfe8..b3bdb2558 100755 --- a/justfile +++ b/justfile @@ -8,7 +8,7 @@ set shell := ["bash", "-c"] PGPARAMS := "" PGPORT := "5411" -export DATABASE_URL := "postgres://postgres:postgres@localhost:" + PGPORT + "/db" + (if PGPARAMS != "" { "?" + PGPARAMS } else { "" }) +export DATABASE_URL := ("postgres://postgres:postgres@localhost:" + PGPORT + "/db" + (if PGPARAMS != "" { "?" + PGPARAMS } else { "" })) export CARGO_TERM_COLOR := "always" # Set AWS variables for testing pmtiles from S3 @@ -22,7 +22,7 @@ export AWS_REGION := "eu-central-1" dockercompose := `if docker-compose --version &> /dev/null; then echo "docker-compose"; else echo "docker compose"; fi` @_default: - {{just_executable()}} --list --unsorted + {{just_executable()}} --list # Start Martin server run *ARGS="--webui enable-for-all": @@ -261,31 +261,45 @@ print-conn-str: # Run cargo fmt and cargo clippy lint: fmt clippy -# Run cargo fmt -fmt: +# Test code formatting +test-fmt: cargo fmt --all -- --check +# Reformat all code `cargo fmt`. If nightly is available, use it for better results +fmt: + #!/usr/bin/env bash + set -euo pipefail + if rustup component list --toolchain nightly | grep rustfmt &> /dev/null; then + echo 'Reformatting Rust code using nightly Rust fmt to sort imports' + cargo +nightly fmt --all -- --config imports_granularity=Module,group_imports=StdExternalCrate + else + echo 'Reformatting Rust with the stable cargo fmt. Install nightly with `rustup install nightly` for better results' + cargo fmt --all + fi + # Reformat markdown files using markdownlint-cli2 fmt-md: docker run -it --rm -v $PWD:/workdir davidanson/markdownlint-cli2 --config /workdir/.github/files/config.markdownlint-cli2.jsonc --fix + +# Reformat all SQL files using docker fmt-sql: docker run -it --rm -v $PWD:/sql sqlfluff/sqlfluff:latest fix --dialect=postgres --exclude-rules=AL07,LT05,LT12 -# Run Nightly cargo fmt, ordering imports -fmt2: - cargo +nightly fmt -- --config imports_granularity=Module,group_imports=StdExternalCrate # Run cargo check check: - RUSTFLAGS='-D warnings' cargo check --all-targets -p martin-tile-utils - RUSTFLAGS='-D warnings' cargo check --all-targets -p mbtiles - RUSTFLAGS='-D warnings' cargo check --all-targets -p mbtiles --no-default-features - RUSTFLAGS='-D warnings' cargo check --all-targets -p martin - RUSTFLAGS='-D warnings' cargo check --all-targets -p martin --no-default-features - RUSTFLAGS='-D warnings' cargo check --all-targets -p martin --no-default-features --features fonts - RUSTFLAGS='-D warnings' cargo check --all-targets -p martin --no-default-features --features mbtiles - RUSTFLAGS='-D warnings' cargo check --all-targets -p martin --no-default-features --features pmtiles - RUSTFLAGS='-D warnings' cargo check --all-targets -p martin --no-default-features --features postgres - RUSTFLAGS='-D warnings' cargo check --all-targets -p martin --no-default-features --features sprites + #!/usr/bin/env bash + set -euo pipefail + export RUSTFLAGS='-D warnings' + cargo check --all-targets -p martin-tile-utils + cargo check --all-targets -p mbtiles + cargo check --all-targets -p mbtiles --no-default-features + cargo check --all-targets -p martin + cargo check --all-targets -p martin --no-default-features + cargo check --all-targets -p martin --no-default-features --features fonts + cargo check --all-targets -p martin --no-default-features --features mbtiles + cargo check --all-targets -p martin --no-default-features --features pmtiles + cargo check --all-targets -p martin --no-default-features --features postgres + cargo check --all-targets -p martin --no-default-features --features sprites # Verify doc build check-doc: @@ -306,12 +320,11 @@ update: cargo update # A few useful tests to run locally to simulate CI -ci-test: env-info restart fmt clippy check-doc test check +ci-test: env-info restart test-fmt clippy check-doc test check -# Get environment info -[private] +# Print environment info env-info: - @echo "OS is {{os()}}, arch is {{arch()}}" + @echo "Running on {{os()}} / {{arch()}}" {{just_executable()}} --version rustc --version cargo --version diff --git a/martin/src/args/root.rs b/martin/src/args/root.rs index 19d9d49e2..fec5300d3 100644 --- a/martin/src/args/root.rs +++ b/martin/src/args/root.rs @@ -1,9 +1,8 @@ use std::path::PathBuf; -use clap::{ - Parser, - builder::{Styles, styling::AnsiColor}, -}; +use clap::Parser; +use clap::builder::Styles; +use clap::builder::styling::AnsiColor; use log::warn; use crate::MartinError::ConfigAndConnectionsError; diff --git a/martin/src/bin/martin-cp.rs b/martin/src/bin/martin-cp.rs index 8fa072a6f..ba322f7b3 100644 --- a/martin/src/bin/martin-cp.rs +++ b/martin/src/bin/martin-cp.rs @@ -7,10 +7,9 @@ use std::time::Duration; use actix_http::error::ParseError; use actix_http::test::TestRequest; use actix_web::http::header::{ACCEPT_ENCODING, AcceptEncoding, Header as _}; -use clap::{ - Parser, - builder::{Styles, styling::AnsiColor}, -}; +use clap::Parser; +use clap::builder::Styles; +use clap::builder::styling::AnsiColor; use futures::TryStreamExt; use futures::stream::{self, StreamExt}; use log::{debug, error, info, log_enabled}; diff --git a/martin/src/cog/model.rs b/martin/src/cog/model.rs index ebc6e4e12..c69202cb6 100644 --- a/martin/src/cog/model.rs +++ b/martin/src/cog/model.rs @@ -1,6 +1,8 @@ -use std::{fs::File, path::Path}; +use std::fs::File; +use std::path::Path; -use tiff::{decoder::Decoder, tags::Tag}; +use tiff::decoder::Decoder; +use tiff::tags::Tag; use super::CogError; diff --git a/martin/src/cog/source.rs b/martin/src/cog/source.rs index 4cc5357d6..3bc2c4316 100644 --- a/martin/src/cog/source.rs +++ b/martin/src/cog/source.rs @@ -458,14 +458,16 @@ fn get_origin( #[cfg(test)] mod tests { - use std::{fs::File, path::PathBuf}; + use std::fs::File; + use std::path::PathBuf; use insta::assert_yaml_snapshot; use martin_tile_utils::TileCoord; use rstest::rstest; use tiff::decoder::Decoder; - use crate::cog::{model::ModelInfo, source::get_tile_idx}; + use crate::cog::model::ModelInfo; + use crate::cog::source::get_tile_idx; #[test] fn can_calc_tile_idx() { diff --git a/martin/src/fonts/mod.rs b/martin/src/fonts/mod.rs index 6af57fe1d..d8eff91da 100644 --- a/martin/src/fonts/mod.rs +++ b/martin/src/fonts/mod.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::ffi::OsStr; use std::fmt::Debug; use std::path::PathBuf; @@ -12,7 +13,6 @@ use pbf_font_tools::protobuf::Message; use pbf_font_tools::{Fontstack, Glyphs, PbfFontError, render_sdf_glyph}; use regex::Regex; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; use crate::OptOneMany; diff --git a/martin/src/pmtiles/mod.rs b/martin/src/pmtiles/mod.rs index f435cca77..4b23be8b5 100644 --- a/martin/src/pmtiles/mod.rs +++ b/martin/src/pmtiles/mod.rs @@ -10,7 +10,8 @@ use async_trait::async_trait; use log::{trace, warn}; use martin_tile_utils::{Encoding, Format, TileCoord, TileInfo}; use pmtiles::async_reader::AsyncPmTilesReader; -use pmtiles::aws_sdk_s3::{Client as S3Client, config::Builder as S3ConfigBuilder}; +use pmtiles::aws_sdk_s3::Client as S3Client; +use pmtiles::aws_sdk_s3::config::Builder as S3ConfigBuilder; use pmtiles::cache::{DirCacheResult, DirectoryCache}; use pmtiles::reqwest::Client; use pmtiles::{AwsS3Backend, Compression, Directory, HttpBackend, MmapBackend, TileType}; diff --git a/martin/src/source.rs b/martin/src/source.rs index 65e82d6ae..529ee013e 100644 --- a/martin/src/source.rs +++ b/martin/src/source.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::fmt::Debug; use actix_web::error::ErrorNotFound; @@ -6,7 +7,6 @@ use dashmap::DashMap; use log::debug; use martin_tile_utils::{TileCoord, TileInfo}; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; use tilejson::TileJSON; use crate::MartinResult; diff --git a/martin/src/sprites/mod.rs b/martin/src/sprites/mod.rs index 80a349cac..8afc9dcf6 100644 --- a/martin/src/sprites/mod.rs +++ b/martin/src/sprites/mod.rs @@ -1,5 +1,4 @@ -use std::collections::BTreeMap; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; use std::fmt::Debug; use std::path::PathBuf; diff --git a/martin/src/srv/styles.rs b/martin/src/srv/styles.rs index 6e32c2cde..be2b297be 100644 --- a/martin/src/srv/styles.rs +++ b/martin/src/srv/styles.rs @@ -1,11 +1,11 @@ -use crate::styles::StyleSources; use actix_web::http::header::ContentType; -use actix_web::middleware; use actix_web::web::{Data, Path}; -use actix_web::{HttpResponse, route}; +use actix_web::{HttpResponse, middleware, route}; use log::error; use serde::Deserialize; +use crate::styles::StyleSources; + #[derive(Deserialize, Debug)] struct StyleRequest { style_id: String, diff --git a/martin/src/styles/mod.rs b/martin/src/styles/mod.rs index 9cc0b9f90..9212c8053 100644 --- a/martin/src/styles/mod.rs +++ b/martin/src/styles/mod.rs @@ -1,10 +1,11 @@ -use dashmap::{DashMap, Entry}; -use log::{info, warn}; -use serde::{Deserialize, Serialize}; use std::collections::{BTreeMap, HashMap}; use std::fmt::Debug; use std::path::{Path, PathBuf}; +use dashmap::{DashMap, Entry}; +use log::{info, warn}; +use serde::{Deserialize, Serialize}; + use crate::config::UnrecognizedValues; use crate::file_config::{ConfigExtras, FileConfigEnum, FileError, FileResult}; @@ -190,9 +191,8 @@ fn is_hidden(entry: &walkdir::DirEntry) -> bool { #[cfg(test)] mod tests { - use crate::file_config::FileConfigSrc; - use super::*; + use crate::file_config::FileConfigSrc; #[test] fn test_add_single_source() { use std::fs::File; diff --git a/mbtiles/src/bin/mbtiles.rs b/mbtiles/src/bin/mbtiles.rs index ee5e205e8..6c0a4c1fe 100644 --- a/mbtiles/src/bin/mbtiles.rs +++ b/mbtiles/src/bin/mbtiles.rs @@ -1,6 +1,7 @@ use std::path::{Path, PathBuf}; -use clap::builder::{Styles, styling::AnsiColor}; +use clap::builder::Styles; +use clap::builder::styling::AnsiColor; use clap::{Parser, Subcommand}; use log::error; use mbtiles::{ diff --git a/rustfmt.toml b/rustfmt.toml deleted file mode 100644 index a0a6602a5..000000000 --- a/rustfmt.toml +++ /dev/null @@ -1,4 +0,0 @@ -## These should be enabled in the future, but for now its a manual step to simplify usage. -## Use Justfile fmt2 target instead: just fmt2 -#imports_granularity = "Module" -#group_imports = "StdExternalCrate"