Skip to content

Commit

Permalink
refactor: error handling with thiserror, anyhow for runtime only
Browse files Browse the repository at this point in the history
Closes #62

This PR includes:

* flake commands for helpful shortcuts
* move to cargo nextest
* worker tests
* addition of workspace macros bail and ensure for internal error handling
* moves general workflow structure to homestar-core, with generic handling, and
  allows for further building blocks
  • Loading branch information
zeeshanlakhani committed Jun 14, 2023
1 parent ec5dd3a commit 706ca25
Show file tree
Hide file tree
Showing 47 changed files with 1,324 additions and 500 deletions.
10 changes: 5 additions & 5 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ updates:
- package-ecosystem: "cargo"
directory: "/homestar-core"
commit-message:
prefix: "chore"
prefix: "[chore(core)]"
include: "scope"
target-branch: "main"
schedule:
Expand All @@ -18,7 +18,7 @@ updates:
- package-ecosystem: "cargo"
directory: "/homestar-runtime"
commit-message:
prefix: "chore"
prefix: "[chore(runtime)]"
include: "scope"
target-branch: "main"
schedule:
Expand All @@ -27,7 +27,7 @@ updates:
- package-ecosystem: "cargo"
directory: "/homestar-guest-wasm"
commit-message:
prefix: "chore"
prefix: "[chore(guest-wasm)]"
include: "scope"
target-branch: "main"
schedule:
Expand All @@ -36,7 +36,7 @@ updates:
- package-ecosystem: "cargo"
directory: "/homestar-wasm"
commit-message:
prefix: "chore"
prefix: "[chore(wasm)]"
include: "scope"
target-branch: "main"
schedule:
Expand All @@ -45,7 +45,7 @@ updates:
- package-ecosystem: "github-actions"
directory: "/"
commit-message:
prefix: "chore(ci)"
prefix: "[chore(ci)]"
include: "scope"
target-branch: "main"
schedule:
Expand Down
16 changes: 16 additions & 0 deletions .ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# cargo-watch ignores

docker
flake.lock
release-please-config.json
deny.toml
diesel.toml
LICENSE
*.nix
*.md

.envrc
.dockerignore
.gitignore
.release-please-manifest.json
.pre-commit-config.yaml
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rust-version = "1.66.0"

[workspace.dependencies]
anyhow = { version = "1.0", features = ["backtrace"] }
thiserror = "1.0"
tokio = { version = "1.26", features = ["fs", "io-util", "io-std", "macros", "rt", "rt-multi-thread"] }
tracing = "0.1"

Expand Down
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ represents the `homestar` runtime.

- Running the tests:

We recommend using [cargo nextest][cargo-nextest], which is installed via
[our Nix flake](#nix) or can be [installed separately][cargo-nextest-install].
We recommend using [cargo nextest][cargo-nextest], which is installed by default
in our [Nix flake](#nix) or can be [installed separately][cargo-nextest-install].

```console
cargo nextest run --all-features --no-capture
```

Otherwise, the above command looks like this using the default `cargo test`:
The above command translates to this using the default `cargo test`:

```console
cargo test --all-features -- --nocapture
Expand Down Expand Up @@ -105,7 +105,7 @@ with `experimental` and `buildkit` set to `true`, for example:
- Build a multi-plaform Docker image via [buildx][buildx]:

```console
docker buildx build --platform=linux/amd64,linux/arm64 -t homestar-runtime --progress=plain .
docker buildx build --file docker/Dockerfile --platform=linux/amd64,linux/arm64 -t homestar-runtime --progress=plain .
```

- Run a Docker image (depending on your platform):
Expand Down Expand Up @@ -149,8 +149,27 @@ hooks. Please run this before every commit and/or push.

- We recommend leveraging [cargo-watch][cargo-watch],
[cargo-expand][cargo-expand] and [irust][irust] for Rust development.
- We also recommend using [cargo-udeps][cargo-udeps] for removing unused dependencies
before commits and pull-requests.
- We also recommend using [cargo-udeps][cargo-udeps] for removing unused
dependencies before commits and pull-requests.
- If using our [Nix flake][nix-flake], there are a number of handy
command shortcuts available for working with `cargo-watch`, `diesel`, and
other binaries, including:
* `ci`, which runs a sequence of commands to check formatting, lints, release
builds, and tests
* `db` and `db-reset` for running `diesel` setup and migrations
* `compile-wasm` for compiling [homestar-guest-wasm](./homestar-guest-wasm),
a [wit-bindgen][]-driven example, to the `wasm32-unknown-unknown` target
* `docker-<amd64,arm64>` for running docker builds
* `nx-test`, which translates to `cargo nextest run && cargo test --doc`
* `x-test` for testing continuously as files change, translating to
`cargo watch -c -s "cargo nextest run && cargo test --doc"`
* `x-<build,check,run,clippy>` for running a variety of `cargo watch`
execution stages
* `nx-test-<all,0>`, which is just like `nx-test`, but adds `all` or `0`
for running tests either with the `all-features` flag or
`no-default-features` flag, respectively.
* `x-<build,check,run,clippy,test>-<core,wasm,runtime>` for package-specific
builds, tests, etc.

### Conventional Commits

Expand Down Expand Up @@ -221,3 +240,4 @@ conditions.
[pre-commit]: https://pre-commit.com/
[seamless-services]: https://youtu.be/Kr3B3sXh_VA
[ucan-invocation]: https://github.com/ucan-wg/invocation
[wit-bindgen]: https://github.com/bytecodealliance/wit-bindgen
38 changes: 28 additions & 10 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 108 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
description = "homestar";

inputs = {
nixpkgs.url = "nixpkgs/nixos-22.11";
nixpkgs.url = "nixpkgs/nixos-23.05";
flake-utils.url = "github:numtide/flake-utils";

rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
Expand Down Expand Up @@ -45,6 +44,109 @@
cargo-watch
twiggy
];

ci = pkgs.writeScriptBin "ci" ''
cargo fmt --check
cargo clippy
cargo build --release
nx-test
'';

db = pkgs.writeScriptBin "db" ''
diesel setup
diesel migration run
'';

dbReset = pkgs.writeScriptBin "db-reset" ''
diesel database reset
diesel setup
diesel migration run
'';

compileWasm = pkgs.writeScriptBin "compile-wasm" ''
cargo build -p homestar-guest-wasm --target wasm32-unknown-unknown --release
'';

dockerBuild = arch:
pkgs.writeScriptBin "docker-${arch}" ''
docker buildx build --file docker/Dockerfile --platform=linux/${arch} -t homestar-runtime --progress=plain .
'';

xFunc = cmd:
pkgs.writeScriptBin "x-${cmd}" ''
cargo watch -c -x ${cmd}
'';

xFuncAll = cmd:
pkgs.writeScriptBin "x-${cmd}-all" ''
cargo watch -c -s "cargo ${cmd} --all-features"
'';

xFuncNoDefault = cmd:
pkgs.writeScriptBin "x-${cmd}-0" ''
cargo watch -c -s "cargo ${cmd} --no-default-features"
'';

xFuncPackage = cmd: crate:
pkgs.writeScriptBin "x-${cmd}-${crate}" ''
cargo watch -c -s "cargo ${cmd} -p homestar-${crate} --all-features"
'';

xFuncTest = pkgs.writeScriptBin "x-test" ''
cargo watch -c -s "cargo nextest run && cargo test --doc"
'';

xFuncTestAll = pkgs.writeScriptBin "x-test-all" ''
cargo watch -c -s "cargo nextest run --all-features --nocapture \
&& cargo test --doc --all-features"
'';

xFuncTestNoDefault = pkgs.writeScriptBin "x-test-all" ''
cargo watch -c -s "cargo nextest run --no-default-features --nocapture \
&& cargo test --doc --no-default-features"
'';

xFuncTestPackage = crate:
pkgs.writeScriptBin "x-test-${crate}" ''
cargo watch -c -s "cargo nextest run -p homestar-${crate} --all-features \
&& cargo test --doc -p homestar-${crate} --all-features"
'';

nxTest = pkgs.writeScriptBin "nx-test" ''
cargo nextest run
cargo test --doc
'';

nxTestAll = pkgs.writeScriptBin "nx-test-all" ''
cargo nextest run --all-features --nocapture
cargo test --doc --all-features
'';

nxTestNoDefault = pkgs.writeScriptBin "nx-test-0" ''
cargo nextest run --no-default-features --nocapture
cargo test --doc --no-default-features
'';

scripts = [
ci
db
dbReset
compileWasm
(builtins.map (arch: dockerBuild arch) ["amd64" "arm64"])
(builtins.map (cmd: xFunc cmd) ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncAll cmd) ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncNoDefault cmd) ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncPackage cmd "core") ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncPackage cmd "wasm") ["build" "check" "run" "clippy"])
(builtins.map (cmd: xFuncPackage cmd "runtime") ["build" "check" "run" "clippy"])
xFuncTest
xFuncTestAll
xFuncTestNoDefault
(builtins.map (crate: xFuncTestPackage crate) ["core" "wasm" "guest-wasm" "runtime"])
nxTest
nxTestAll
nxTestNoDefault
];
in rec
{
devShells.default = pkgs.mkShell {
Expand All @@ -57,16 +159,17 @@
nightly-rustfmt
rust-toolchain
rust-analyzer
rustup
pkg-config
pre-commit
protobuf
rustup
diesel-cli
direnv
self.packages.${system}.irust
]
++ format-pkgs
++ cargo-installs
++ scripts
++ lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.CoreFoundation
Expand All @@ -92,6 +195,8 @@
doCheck = false;
cargoSha256 = "sha256-FmsD3ajMqpPrTkXCX2anC+cmm0a2xuP+3FHqzj56Ma4=";
};

formatter = pkgs.alejandra;
}
);
}
3 changes: 2 additions & 1 deletion homestar-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ libsqlite3-sys = { version = "0.26", features = ["bundled"] }
proptest = { version = "1.1", optional = true }
serde = { version = "1.0", features = ["derive"] }
signature = "2.0"
thiserror = "1.0"
thiserror = { workspace = true }
tracing = { workspace = true }
ucan = "0.1"
url = "2.3"
xid = "1.0"

[dev-dependencies]
criterion = "0.4"
json = "0.12"

[features]
default = []
Expand Down
4 changes: 3 additions & 1 deletion homestar-core/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Exported global constants.

/// SemVer-formatted version of the UCAN Invocation Specification.
/// SemVer-formatted version of the UCAN Invocation Specification.
pub const INVOCATION_VERSION: &str = "0.2.0";
/// DagCbor codec.
pub const DAG_CBOR: u64 = 0x71;
Loading

0 comments on commit 706ca25

Please sign in to comment.