Skip to content

Commit 0500348

Browse files
committed
Add Rust CI workflow
1 parent 498040b commit 0500348

6 files changed

Lines changed: 85 additions & 12 deletions

File tree

.cargo/config.toml

Lines changed: 0 additions & 5 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions:
8+
contents: read
9+
10+
env:
11+
CARGO_TERM_COLOR: always
12+
CARGO_TARGET_DIR: target
13+
14+
jobs:
15+
rust:
16+
name: Rust lint and tests
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v4
20+
with:
21+
path: rustscript-egui-ui-policy
22+
- uses: actions/checkout@v4
23+
with:
24+
repository: rustscript-lang/rustscript
25+
path: rustscript
26+
- uses: dtolnay/rust-toolchain@stable
27+
with:
28+
components: rustfmt, clippy
29+
- name: Format check
30+
working-directory: rustscript-egui-ui-policy
31+
run: cargo fmt --all -- --check
32+
- name: Clippy
33+
working-directory: rustscript-egui-ui-policy
34+
run: cargo clippy --all-targets
35+
- name: Tests
36+
working-directory: rustscript-egui-ui-policy
37+
run: cargo test --tests --jobs 4
38+
- name: Example run
39+
working-directory: rustscript-egui-ui-policy
40+
run: cargo run --example panel

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target
2+
.cargo/

scripts/ui_policy.rss

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
fn ui_spec(mode, state) -> string;
2+
13
let compact = width < 480;
2-
let state = if has_errors => {
3-
if compact => { "compact:error" } else => { "wide:error" }
4-
} else => {
5-
if compact => { "compact:normal" } else => { "wide:normal" }
6-
};
7-
state;
4+
let mode = if compact => { "compact" } else => { "wide" };
5+
let state = if has_errors => { "error" } else => { "normal" };
6+
ui_spec(mode, state);

src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use egui::Color32;
2-
use vm::{Value, Vm, VmStatus, compile_source};
2+
use vm::{CallOutcome, CallReturn, HostFunction, Value, Vm, VmError, VmStatus, compile_source};
33

44
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55
pub enum UiMode {
@@ -64,9 +64,23 @@ fn evaluate_raw(source: &str, width: i64, has_errors: bool) -> Result<String, St
6464
}
6565
}
6666

67+
struct UiSpecHost;
68+
69+
impl HostFunction for UiSpecHost {
70+
fn call(&mut self, _vm: &mut Vm, args: &[Value]) -> Result<CallOutcome, VmError> {
71+
match args {
72+
[Value::String(mode), Value::String(state)] => Ok(CallOutcome::Return(
73+
CallReturn::one(Value::string(format!("{mode}:{state}"))),
74+
)),
75+
_ => Err(VmError::TypeMismatch("ui mode and state strings")),
76+
}
77+
}
78+
}
79+
6780
fn run_value(source: &str) -> Result<Value, String> {
6881
let compiled = compile_source(source).map_err(|err| err.to_string())?;
6982
let mut vm = Vm::new(compiled.program);
83+
vm.bind_function("ui_spec", Box::new(UiSpecHost));
7084
let status = vm.run().map_err(|err| err.to_string())?;
7185
if status != VmStatus::Halted {
7286
return Err(format!("script did not halt: {status:?}"));

tests/example_smoke.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::process::Command;
2+
3+
#[test]
4+
fn panel_example_runs_end_to_end() {
5+
let output = Command::new(std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string()))
6+
.args(["run", "--quiet", "--example", "panel"])
7+
.current_dir(env!("CARGO_MANIFEST_DIR"))
8+
.output()
9+
.expect("panel example should launch");
10+
11+
assert!(
12+
output.status.success(),
13+
"panel example failed\nstatus: {:?}\nstdout:\n{}\nstderr:\n{}",
14+
output.status.code(),
15+
String::from_utf8_lossy(&output.stdout),
16+
String::from_utf8_lossy(&output.stderr)
17+
);
18+
19+
let stdout = String::from_utf8_lossy(&output.stdout);
20+
assert!(
21+
stdout.contains("mode=Compact, title=Fix errors"),
22+
"unexpected panel example stdout:\n{stdout}"
23+
);
24+
}

0 commit comments

Comments
 (0)