From 641a93906df2948aeadbe2b61e5cf899d671967f Mon Sep 17 00:00:00 2001 From: Jika Date: Thu, 30 Oct 2025 18:55:42 +0100 Subject: [PATCH] Moved to a workspace for future crates --- Cargo.lock | 2 +- Cargo.toml | 22 +++++----- README.md | 40 +++++++++---------- crates/oc2r-core/Cargo.toml | 9 +++++ {docs => crates/oc2r-core/docs}/index.md | 4 +- {docs => crates/oc2r-core/docs}/wrappers.md | 0 .../oc2r-core/examples}/invoke-method.rs | 2 +- .../oc2r-core/examples}/list-devices.rs | 2 +- .../oc2r-core/examples}/redstone-events.rs | 2 +- .../oc2r-core/examples}/redstone.rs | 2 +- {src => crates/oc2r-core/src}/bus.rs | 0 .../src}/devices/block_operations.rs | 0 {src => crates/oc2r-core/src}/devices/cpu.rs | 0 .../oc2r-core/src}/devices/energy_storage.rs | 0 .../src}/devices/file_import_export.rs | 0 .../oc2r-core/src}/devices/fluid_handler.rs | 0 .../src}/devices/inventory_operations.rs | 0 .../oc2r-core/src}/devices/item_handler.rs | 0 {src => crates/oc2r-core/src}/devices/mod.rs | 0 .../oc2r-core/src}/devices/redstone.rs | 0 .../oc2r-core/src}/devices/robot.rs | 0 .../oc2r-core/src}/devices/sound.rs | 0 {src => crates/oc2r-core/src}/error.rs | 0 {src => crates/oc2r-core/src}/event.rs | 0 {src => crates/oc2r-core/src}/lib.rs | 0 {src => crates/oc2r-core/src}/rpc.rs | 0 {src => crates/oc2r-core/src}/transport.rs | 0 {src => crates/oc2r-core/src}/value.rs | 0 src/main.rs | 28 ------------- 29 files changed, 48 insertions(+), 65 deletions(-) create mode 100644 crates/oc2r-core/Cargo.toml rename {docs => crates/oc2r-core/docs}/index.md (97%) rename {docs => crates/oc2r-core/docs}/wrappers.md (100%) rename {examples => crates/oc2r-core/examples}/invoke-method.rs (94%) rename {examples => crates/oc2r-core/examples}/list-devices.rs (91%) rename {examples => crates/oc2r-core/examples}/redstone-events.rs (97%) rename {examples => crates/oc2r-core/examples}/redstone.rs (96%) rename {src => crates/oc2r-core/src}/bus.rs (100%) rename {src => crates/oc2r-core/src}/devices/block_operations.rs (100%) rename {src => crates/oc2r-core/src}/devices/cpu.rs (100%) rename {src => crates/oc2r-core/src}/devices/energy_storage.rs (100%) rename {src => crates/oc2r-core/src}/devices/file_import_export.rs (100%) rename {src => crates/oc2r-core/src}/devices/fluid_handler.rs (100%) rename {src => crates/oc2r-core/src}/devices/inventory_operations.rs (100%) rename {src => crates/oc2r-core/src}/devices/item_handler.rs (100%) rename {src => crates/oc2r-core/src}/devices/mod.rs (100%) rename {src => crates/oc2r-core/src}/devices/redstone.rs (100%) rename {src => crates/oc2r-core/src}/devices/robot.rs (100%) rename {src => crates/oc2r-core/src}/devices/sound.rs (100%) rename {src => crates/oc2r-core/src}/error.rs (100%) rename {src => crates/oc2r-core/src}/event.rs (100%) rename {src => crates/oc2r-core/src}/lib.rs (100%) rename {src => crates/oc2r-core/src}/rpc.rs (100%) rename {src => crates/oc2r-core/src}/transport.rs (100%) rename {src => crates/oc2r-core/src}/value.rs (100%) delete mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock index c9b8a1f..276ef03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -48,7 +48,7 @@ dependencies = [ ] [[package]] -name = "oc2r-rust" +name = "oc2r-core" version = "0.1.0" dependencies = [ "libc", diff --git a/Cargo.toml b/Cargo.toml index 1ead56b..a970105 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,14 @@ -[package] -name = "oc2r-rust" -version = "0.1.0" +[workspace] +resolver = "3" +members = ["crates/*"] + +[workspace.package] +authors = ["Christopher Head "] edition = "2024" +repository = "https://git.jika.li/jika/oc2r-rust" +license = "GPL-3.0-only" +keywords = ["minecraft", "opencomputers"] +categories = ["games"] [workspace.lints.clippy] enum_glob_use = "deny" @@ -13,10 +20,7 @@ unwrap_used = "deny" strip = true opt-level = "s" lto = true - codegen-units = 1 -[dependencies] -miniserde = "0.1" -miniserde-enum = {git = "https://git.jika.li/Jika/miniserde_enum"} -# miniserde-enum = {git = "https://github.com/yuki0iq/miniserde-enum"} -libc = "0.2" + +[workspace.dependencies] +oc2r-core = { path = "crates/oc2r-core" } diff --git a/README.md b/README.md index 84df76a..e81ffa7 100644 --- a/README.md +++ b/README.md @@ -1,41 +1,39 @@ -OC2R Rust Client -================ +# OC2R Rust Client Thin, synchronous access to the OC2R high-level API from Rust. The crate mirrors -the stock Lua helpers (`devices.lua`) so you can list devices, invoke methods, +the stock Lua helpers (`devices.lua`, `robot.lua`) so you can list devices, invoke methods, subscribe to events, and work with typed wrappers for common peripherals. -Documentation -------------- +## Documentation See [`docs/index.md`](docs/index.md) for the full guide and [`docs/wrappers.md`](docs/wrappers.md) for device-by-device examples. -Examples --------- +## Development -We ship three binaries that mirror Lua scripts: +The crate targets rust nightly for some size optimisations and relies on `miniserde` and `libc`. -- `examples/list-devices.rs` -- `examples/invoke-method.rs` -- `examples/redstone.rs` -- `examples/redstone-events.rs` +To ease developpemnt we use [cross-rs](https://github.com/cross-rs/cross), +but you can install all toolchain component or your local computer and replace cross with cargo. + +By default the option "-Zfmt-debug=none" is set meaning all debug print (:?) are off. If you can disable this option at any time. + +# Compilation + +Simply `cross clippy --all-targets` to compile all examples + +## Examples + +Examples of how to use the crate are in the `examples` directory. Build them with: ```bash -# Install instructions: https://github.com/cross-rs/cross#installation -cross build --release --example list-devices +cross build --release --example example-names ``` Copy the resulting binaries from `target//release/examples/` into Minux (an Import/Export card works -well), then execute them inside the VM. +well), mark them as executable and then execute them inside the VM. -Development ------------ -The crate targets stable Rust 1.74+ and relies on `miniserde` and `libc`. Use -`cross clippy --all-targets` before submitting changes. CI scripts are still in -flight; for now the `cross build` and `cross clippy` invocations exercised in -the examples are the reference. diff --git a/crates/oc2r-core/Cargo.toml b/crates/oc2r-core/Cargo.toml new file mode 100644 index 0000000..1f07a40 --- /dev/null +++ b/crates/oc2r-core/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "oc2r-core" +version = "0.1.0" +edition = "2024" + +[dependencies] +miniserde = "0.1" +miniserde-enum = {git = "https://git.jika.li/Jika/miniserde_enum"} +libc = "0.2" diff --git a/docs/index.md b/crates/oc2r-core/docs/index.md similarity index 97% rename from docs/index.md rename to crates/oc2r-core/docs/index.md index 498f4e8..4330bda 100644 --- a/docs/index.md +++ b/crates/oc2r-core/docs/index.md @@ -6,13 +6,13 @@ wrappers that mirror `devices.lua`. ## Getting Started -1. Install `cross` (one-time): +1. Install [cross](https://github.com/cross-rs/cross): ```bash cargo install cross --git https://github.com/cross-rs/cross ``` 2. Compile your binary for Minux: ```bash - cross build --release --bin your-app + cross build --release ``` 3. Copy `target//release/your-app` onto the Import/Export card and drop it inside the OC2R VM (e.g. `/usr/bin/your-app`). diff --git a/docs/wrappers.md b/crates/oc2r-core/docs/wrappers.md similarity index 100% rename from docs/wrappers.md rename to crates/oc2r-core/docs/wrappers.md diff --git a/examples/invoke-method.rs b/crates/oc2r-core/examples/invoke-method.rs similarity index 94% rename from examples/invoke-method.rs rename to crates/oc2r-core/examples/invoke-method.rs index 1be0d86..4ebe996 100644 --- a/examples/invoke-method.rs +++ b/crates/oc2r-core/examples/invoke-method.rs @@ -1,6 +1,6 @@ // Build with: cross build --release --example invoke-method -use oc2r_rust::{DEFAULT_DEVICE_PATH, DeviceBus, Result}; +use oc2r_core::{DEFAULT_DEVICE_PATH, DeviceBus, Result}; fn main() -> Result<()> { let mut bus = DeviceBus::connect(DEFAULT_DEVICE_PATH)?; diff --git a/examples/list-devices.rs b/crates/oc2r-core/examples/list-devices.rs similarity index 91% rename from examples/list-devices.rs rename to crates/oc2r-core/examples/list-devices.rs index 9a16132..60665a3 100644 --- a/examples/list-devices.rs +++ b/crates/oc2r-core/examples/list-devices.rs @@ -1,6 +1,6 @@ // Build with: cross build --release --example list-devices -use oc2r_rust::{DEFAULT_DEVICE_PATH, DeviceBus, Result}; +use oc2r_core::{DEFAULT_DEVICE_PATH, DeviceBus, Result}; fn main() -> Result<()> { let mut bus = DeviceBus::connect(DEFAULT_DEVICE_PATH)?; diff --git a/examples/redstone-events.rs b/crates/oc2r-core/examples/redstone-events.rs similarity index 97% rename from examples/redstone-events.rs rename to crates/oc2r-core/examples/redstone-events.rs index ebaeb4f..d2e589f 100644 --- a/examples/redstone-events.rs +++ b/crates/oc2r-core/examples/redstone-events.rs @@ -1,4 +1,4 @@ -use oc2r_rust::{DEFAULT_DEVICE_PATH, DeviceBus, EventLoop, JsonValueExt, Redstone, Result}; +use oc2r_core::{DEFAULT_DEVICE_PATH, DeviceBus, EventLoop, JsonValueExt, Redstone, Result}; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; diff --git a/examples/redstone.rs b/crates/oc2r-core/examples/redstone.rs similarity index 96% rename from examples/redstone.rs rename to crates/oc2r-core/examples/redstone.rs index 6d721af..49491ad 100644 --- a/examples/redstone.rs +++ b/crates/oc2r-core/examples/redstone.rs @@ -1,6 +1,6 @@ // Build with: cross build --release --example redstone -use oc2r_rust::{DEFAULT_DEVICE_PATH, DeviceBus, Redstone, Result, Side}; +use oc2r_core::{DEFAULT_DEVICE_PATH, DeviceBus, Redstone, Result, Side}; use std::env; use std::str::FromStr; diff --git a/src/bus.rs b/crates/oc2r-core/src/bus.rs similarity index 100% rename from src/bus.rs rename to crates/oc2r-core/src/bus.rs diff --git a/src/devices/block_operations.rs b/crates/oc2r-core/src/devices/block_operations.rs similarity index 100% rename from src/devices/block_operations.rs rename to crates/oc2r-core/src/devices/block_operations.rs diff --git a/src/devices/cpu.rs b/crates/oc2r-core/src/devices/cpu.rs similarity index 100% rename from src/devices/cpu.rs rename to crates/oc2r-core/src/devices/cpu.rs diff --git a/src/devices/energy_storage.rs b/crates/oc2r-core/src/devices/energy_storage.rs similarity index 100% rename from src/devices/energy_storage.rs rename to crates/oc2r-core/src/devices/energy_storage.rs diff --git a/src/devices/file_import_export.rs b/crates/oc2r-core/src/devices/file_import_export.rs similarity index 100% rename from src/devices/file_import_export.rs rename to crates/oc2r-core/src/devices/file_import_export.rs diff --git a/src/devices/fluid_handler.rs b/crates/oc2r-core/src/devices/fluid_handler.rs similarity index 100% rename from src/devices/fluid_handler.rs rename to crates/oc2r-core/src/devices/fluid_handler.rs diff --git a/src/devices/inventory_operations.rs b/crates/oc2r-core/src/devices/inventory_operations.rs similarity index 100% rename from src/devices/inventory_operations.rs rename to crates/oc2r-core/src/devices/inventory_operations.rs diff --git a/src/devices/item_handler.rs b/crates/oc2r-core/src/devices/item_handler.rs similarity index 100% rename from src/devices/item_handler.rs rename to crates/oc2r-core/src/devices/item_handler.rs diff --git a/src/devices/mod.rs b/crates/oc2r-core/src/devices/mod.rs similarity index 100% rename from src/devices/mod.rs rename to crates/oc2r-core/src/devices/mod.rs diff --git a/src/devices/redstone.rs b/crates/oc2r-core/src/devices/redstone.rs similarity index 100% rename from src/devices/redstone.rs rename to crates/oc2r-core/src/devices/redstone.rs diff --git a/src/devices/robot.rs b/crates/oc2r-core/src/devices/robot.rs similarity index 100% rename from src/devices/robot.rs rename to crates/oc2r-core/src/devices/robot.rs diff --git a/src/devices/sound.rs b/crates/oc2r-core/src/devices/sound.rs similarity index 100% rename from src/devices/sound.rs rename to crates/oc2r-core/src/devices/sound.rs diff --git a/src/error.rs b/crates/oc2r-core/src/error.rs similarity index 100% rename from src/error.rs rename to crates/oc2r-core/src/error.rs diff --git a/src/event.rs b/crates/oc2r-core/src/event.rs similarity index 100% rename from src/event.rs rename to crates/oc2r-core/src/event.rs diff --git a/src/lib.rs b/crates/oc2r-core/src/lib.rs similarity index 100% rename from src/lib.rs rename to crates/oc2r-core/src/lib.rs diff --git a/src/rpc.rs b/crates/oc2r-core/src/rpc.rs similarity index 100% rename from src/rpc.rs rename to crates/oc2r-core/src/rpc.rs diff --git a/src/transport.rs b/crates/oc2r-core/src/transport.rs similarity index 100% rename from src/transport.rs rename to crates/oc2r-core/src/transport.rs diff --git a/src/value.rs b/crates/oc2r-core/src/value.rs similarity index 100% rename from src/value.rs rename to crates/oc2r-core/src/value.rs diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 212b79d..0000000 --- a/src/main.rs +++ /dev/null @@ -1,28 +0,0 @@ -use oc2r_rust::{DEFAULT_DEVICE_PATH, DeviceBus, DeviceEntry, Result}; - -fn main() -> Result<()> { - println!("Opening {DEFAULT_DEVICE_PATH}…"); - let mut bus = DeviceBus::connect(DEFAULT_DEVICE_PATH)?; - - let drained = bus.flush()?; - println!("Drained {drained} stale bytes."); - - println!("Listing devices…"); - let devices = bus.list()?; - for device in devices { - print_device(&device); - } - - Ok(()) -} - -fn print_device(device: &DeviceEntry) { - println!("deviceId: {}", device.device_id); - if !device.type_names.is_empty() { - println!(" typeNames: {}", device.type_names.join(", ")); - } - if let Some(desc) = &device.description { - println!(" description: {desc}"); - } - println!("--------"); -}