From 3d0d55c8d8ea06895a80f0d25bc530b4706f7920 Mon Sep 17 00:00:00 2001 From: "Jens K." <47693+sectore@users.noreply.github.com> Date: Fri, 29 Nov 2024 15:49:55 +0100 Subject: [PATCH] rustfmt, clippy, gh actions, justfile, `Default` app (#4) * `Default` app * add zed settings * add justfile * flake: update devShell, ignore tests, add `just` * update README * gh actions: lint, format, tests. build --- .github/workflows/ci.yml | 39 +++++++++++++++++++++++++++++++++++++++ .zed/settings.json | 17 +++++++++++++++++ README.md | 30 ++++++++++++++++++++++++++---- flake.nix | 24 +++++++++++------------- justfile | 32 ++++++++++++++++++++++++++++++++ src/app.rs | 17 +++++------------ src/main.rs | 2 +- 7 files changed, 131 insertions(+), 30 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .zed/settings.json create mode 100644 justfile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..fdf9e52 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,39 @@ +name: lint, format, test, build + +on: + push: + branches: [ master ] + pull_request: + +jobs: + + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: DeterminateSystems/nix-installer-action@main + - uses: DeterminateSystems/magic-nix-cache-action@main + - name: Check formatting + run: nix develop --command cargo fmt --all -- --check + - name: Run clippy + run: nix develop --command cargo clippy -- -D warnings + - name: Run tests + run: nix develop --command cargo test + + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: DeterminateSystems/nix-installer-action@main + - uses: DeterminateSystems/magic-nix-cache-action@main + - name: Run tests + run: nix develop --command cargo test + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: DeterminateSystems/nix-installer-action@main + - uses: DeterminateSystems/magic-nix-cache-action@main + - name: Build project + run: nix build .#timr diff --git a/.zed/settings.json b/.zed/settings.json new file mode 100644 index 0000000..396a7e1 --- /dev/null +++ b/.zed/settings.json @@ -0,0 +1,17 @@ +// Folder-specific settings +// +// For a full list of overridable settings, and general information on folder-specific settings, +// see the documentation: https://zed.dev/docs/configuring-zed#settings-files +{ + "format_on_save": "on", + "formatter": "language_server", + "lsp": { + "rust-analyzer": { + "initialization_options": { + "check": { + "command": "clippy" // rust-analyzer.check.command (default: "check") + } + } + } + } +} diff --git a/README.md b/README.md index 042c4fb..ae7a339 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # tim:r -## Development +## Build from source ### Requirements @@ -13,17 +13,20 @@ If `direnv` is installed, run `direnv allow` once to install dependencies. Other #### Non Nix user -- [`Rust`](https://www.rust-lang.org/) +- [`Rust`](https://www.rust-lang.org/learn/get-started) +- [`Clippy`](https://github.com/rust-lang/rust-clippy) +- [`rustfmt`](https://github.com/rust-lang/rustfmt) +- [`just`](https://just.systems) -#### Build/run +#### Run ```sh cargo run ``` -## Build release +#### Build - Linux ```sh @@ -34,3 +37,22 @@ nix build ```sh nix build .#windows ``` + +#### Commands to `run`, `build` etc. + +```sh +just --list + +Available recipes: + build # build app + b # alias for `build` + default + format # format files + f # alias for `format` + lint # lint + l # alias for `lint` + run # run app + r # alias for `run` + test # run tests + t # alias for `test` +``` diff --git a/flake.nix b/flake.nix index c2f2370..9907d2b 100644 --- a/flake.nix +++ b/flake.nix @@ -28,6 +28,7 @@ cargoArtifacts = craneLib.buildDepsOnly { src = craneLib.cleanCargoSource ./.; }; + doCheck = false; # skip tests during nix build }; # Native build @@ -64,20 +65,17 @@ windows = crossBuild; }; - # Separate artifacts for CI caching - checks = { - inherit timr; - cargoArtifacts = commonArgs.cargoArtifacts; - }; - # Development shell with all necessary tools - devShells.default = pkgs.mkShell { - buildInputs = with pkgs; [ - rust-analyzer - clippy - rustfmt - toolchain - ]; + devShell = with nixpkgs.legacyPackages.${system}; mkShell { + buildInputs = with fenix.packages.${system}.stable; [ + rust-analyzer + clippy + rustfmt + toolchain + just + ]; + + inherit (commonArgs) src; RUST_SRC_PATH = "${toolchain}/lib/rustlib/src/rust/library"; diff --git a/justfile b/justfile new file mode 100644 index 0000000..b5f52d1 --- /dev/null +++ b/justfile @@ -0,0 +1,32 @@ +# The `--fmt` command is currently unstable. + +set unstable := true + +default: run + +alias b := build +alias f := format +alias l := lint +alias t := test +alias r := run + +# build app +build: + cargo build + +# run tests +test: + cargo test + +# format files +format: + just --fmt + cargo fmt --check + +# lint +lint: + cargo clippy --no-deps + +# run app +run: + cargo run diff --git a/src/app.rs b/src/app.rs index d3b6615..9d1a01c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -7,27 +7,28 @@ use ratatui::{ widgets::{Block, Paragraph, Widget}, DefaultTerminal, Frame, }; -use strum::{Display, EnumIter, FromRepr}; use crate::footer::Footer; use crate::pomodoro::Pomodoro; use crate::timer::Timer; use crate::{countdown::Countdown, utils::center}; -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] enum Mode { + #[default] Running, Quit, } -#[derive(Debug, Clone, Copy, Display, EnumIter, FromRepr, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum Content { + #[default] Countdown, Timer, Pomodoro, } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub struct App { content: Content, mode: Mode, @@ -35,14 +36,6 @@ pub struct App { } impl App { - pub const fn new() -> Self { - Self { - content: Content::Countdown, - mode: Mode::Running, - show_menu: false, - } - } - pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> { while self.is_running() { terminal diff --git a/src/main.rs b/src/main.rs index a6b95b3..5d6a649 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use color_eyre::{eyre::Context, Result}; fn main() -> Result<()> { color_eyre::install()?; let terminal = ratatui::init(); - let app_result = App::new().run(terminal).context("app loop failed"); + let app_result = App::default().run(terminal).context("app loop failed"); ratatui::restore(); app_result }