Skip to content

Commit

Permalink
Rust! (#1)
Browse files Browse the repository at this point in the history
* hermit: +go-1.19.4 +rustup-1.25.1
* rust!
* ci: run rust tests on push
* rust: add examples
* README: add Rust example
  • Loading branch information
Scott Robinson authored Jan 10, 2023
1 parent f306615 commit 3125e8a
Show file tree
Hide file tree
Showing 31 changed files with 420 additions and 2 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ on: push

name: CI
jobs:
test:
name: Test
go:
name: Test (Go)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -15,3 +15,11 @@ jobs:

- name: Test
run: go test -v

rust:
name: Test (Rust)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- run: bin/cargo test --all-features --all-targets --locked
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.hermit

/Cargo.lock
/target
14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "semantic-exit"
version = "1.0.0"
edition = "2021"

[dependencies]
num-traits = "0.2"
num-derive = "0.3"
thiserror = "1.0.38"

[dev-dependencies]
libc = "0.2"
regex = "1.7.0"
test-case = "2.2.2"
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ os.Exit(exit.Forbidden) // The user isn't permitted to perform this action
os.Exit(exit.Unavailable) // An API this program consumes isn't available
```

#### Rust

```rust
use semantic_exit::{exit, Code};

exit(Code::Forbidden);
exit(Code::Unavailable);
```

See [the complete list of exit codes](#the-codes).

### About
Expand Down
1 change: 1 addition & 0 deletions bin/.go-1.19.4.pkg
1 change: 1 addition & 0 deletions bin/.rustup-1.25.1.pkg
7 changes: 7 additions & 0 deletions bin/README.hermit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Hermit environment

This is a [Hermit](https://github.com/cashapp/hermit) bin directory.

The symlinks in this directory are managed by Hermit and will automatically
download and install Hermit itself as well as packages. These packages are
local to this environment.
21 changes: 21 additions & 0 deletions bin/activate-hermit
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
# This file must be used with "source bin/activate-hermit" from bash or zsh.
# You cannot run it directly
#
# THIS FILE IS GENERATED; DO NOT MODIFY

if [ "${BASH_SOURCE-}" = "$0" ]; then
echo "You must source this script: \$ source $0" >&2
exit 33
fi

BIN_DIR="$(dirname "${BASH_SOURCE[0]:-${(%):-%x}}")"
if "${BIN_DIR}/hermit" noop > /dev/null; then
eval "$("${BIN_DIR}/hermit" activate "${BIN_DIR}/..")"

if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ]; then
hash -r 2>/dev/null
fi

echo "Hermit environment $("${HERMIT_ENV}"/bin/hermit env HERMIT_ENV) activated"
fi
1 change: 1 addition & 0 deletions bin/cargo
1 change: 1 addition & 0 deletions bin/cargo-clippy
1 change: 1 addition & 0 deletions bin/cargo-fmt
1 change: 1 addition & 0 deletions bin/cargo-miri
1 change: 1 addition & 0 deletions bin/clippy-driver
1 change: 1 addition & 0 deletions bin/go
1 change: 1 addition & 0 deletions bin/gofmt
43 changes: 43 additions & 0 deletions bin/hermit
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
#
# THIS FILE IS GENERATED; DO NOT MODIFY

set -eo pipefail

export HERMIT_USER_HOME=~

if [ -z "${HERMIT_STATE_DIR}" ]; then
case "$(uname -s)" in
Darwin)
export HERMIT_STATE_DIR="${HERMIT_USER_HOME}/Library/Caches/hermit"
;;
Linux)
export HERMIT_STATE_DIR="${XDG_CACHE_HOME:-${HERMIT_USER_HOME}/.cache}/hermit"
;;
esac
fi

export HERMIT_DIST_URL="${HERMIT_DIST_URL:-https://github.com/cashapp/hermit/releases/download/stable}"
HERMIT_CHANNEL="$(basename "${HERMIT_DIST_URL}")"
export HERMIT_CHANNEL
export HERMIT_EXE=${HERMIT_EXE:-${HERMIT_STATE_DIR}/pkg/hermit@${HERMIT_CHANNEL}/hermit}

if [ ! -x "${HERMIT_EXE}" ]; then
echo "Bootstrapping ${HERMIT_EXE} from ${HERMIT_DIST_URL}" 1>&2
INSTALL_SCRIPT="$(mktemp)"
# This value must match that of the install script
INSTALL_SCRIPT_SHA256="180e997dd837f839a3072a5e2f558619b6d12555cd5452d3ab19d87720704e38"
if [ "${INSTALL_SCRIPT_SHA256}" = "BYPASS" ]; then
curl -fsSL "${HERMIT_DIST_URL}/install.sh" -o "${INSTALL_SCRIPT}"
else
# Install script is versioned by its sha256sum value
curl -fsSL "${HERMIT_DIST_URL}/install-${INSTALL_SCRIPT_SHA256}.sh" -o "${INSTALL_SCRIPT}"
# Verify install script's sha256sum
openssl dgst -sha256 "${INSTALL_SCRIPT}" | \
awk -v EXPECTED="$INSTALL_SCRIPT_SHA256" \
'$2!=EXPECTED {print "Install script sha256 " $2 " does not match " EXPECTED; exit 1}'
fi
/bin/bash "${INSTALL_SCRIPT}" 1>&2
fi

exec "${HERMIT_EXE}" --level=fatal exec "$0" -- "$@"
Empty file added bin/hermit.hcl
Empty file.
1 change: 1 addition & 0 deletions bin/rls
1 change: 1 addition & 0 deletions bin/rust-gdb
1 change: 1 addition & 0 deletions bin/rust-gdbgui
1 change: 1 addition & 0 deletions bin/rust-lldb
1 change: 1 addition & 0 deletions bin/rustc
1 change: 1 addition & 0 deletions bin/rustdoc
1 change: 1 addition & 0 deletions bin/rustfmt
1 change: 1 addition & 0 deletions bin/rustup
126 changes: 126 additions & 0 deletions examples-rs/Cargo.lock

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

11 changes: 11 additions & 0 deletions examples-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "examples-rs"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "ok"

[dependencies]
semantic-exit = { path = "../" }
signal-hook = "0.3.14"
5 changes: 5 additions & 0 deletions examples-rs/src/bin/ok.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use semantic_exit::{exit, Code::OK};

fn main() {
exit(OK);
}
19 changes: 19 additions & 0 deletions examples-rs/src/bin/signal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use semantic_exit::from_signal;
use signal_hook::{consts::SIGINT, iterator::Signals};
use std::process::exit;
use std::{error::Error, thread, time::Duration};

fn main() -> Result<(), Box<dyn Error>> {
let mut signals = Signals::new([SIGINT])?;

thread::spawn(move || {
for sig in signals.forever() {
println!("Received signal {:?}", sig);
exit(from_signal(sig));
}
});

thread::sleep(Duration::from_secs(2));

Ok(())
}
3 changes: 3 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "stable"
profile = "default"
Loading

0 comments on commit 3125e8a

Please sign in to comment.