From ebfa6f61d65098fa20f93f1c5fb8941e499eb106 Mon Sep 17 00:00:00 2001 From: chrysn Date: Sat, 11 Jun 2022 02:05:48 +0200 Subject: [PATCH] shell/democommands: Add example Rust module --- makefiles/cargo-targets.inc.mk | 3 +++ sys/Makefile.dep | 5 +++++ sys/Makefile.include | 2 ++ sys/rust_riotmodules/Cargo.toml | 1 + sys/rust_riotmodules/src/lib.rs | 3 +++ sys/shell/democommands/Cargo.toml | 14 ++++++++++++++ sys/shell/democommands/src/lib.rs | 17 +++++++++++++++++ 7 files changed, 45 insertions(+) create mode 100644 sys/shell/democommands/Cargo.toml create mode 100644 sys/shell/democommands/src/lib.rs diff --git a/makefiles/cargo-targets.inc.mk b/makefiles/cargo-targets.inc.mk index dc44c7f73b..c12518bf28 100644 --- a/makefiles/cargo-targets.inc.mk +++ b/makefiles/cargo-targets.inc.mk @@ -13,6 +13,9 @@ CARGO_COMPILE_COMMANDS_FLAGS = --clang ifneq (,$(filter lsm303agr,$(USEMODULE))) CARGO_OPTIONS += --features rust_riotmodules/riot-module-lsm303agr endif +ifneq (,$(filter shell_democommands,$(USEMODULE))) + CARGO_OPTIONS += --features rust_riotmodules/riot-module-shell-democommands +endif # This is duplicating the compile-commands rule because unlike in the use case # when a $(RIOTBASE)/compile_commands.json is built, we *want* this to be diff --git a/sys/Makefile.dep b/sys/Makefile.dep index 9980b81ced..1d993381eb 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -338,6 +338,11 @@ ifneq (,$(filter shell_commands,$(USEMODULE))) endif endif +ifneq (,$(filter shell_democommands,$(USEMODULE))) + USEMODULE += rust_riotmodules + USEMODULE += shell +endif + ifneq (,$(filter md5sum sha1sum sha256sum,$(USEMODULE))) USEMODULE += vfs_util USEMODULE += hashes diff --git a/sys/Makefile.include b/sys/Makefile.include index 659b7e1a23..4e90267c10 100644 --- a/sys/Makefile.include +++ b/sys/Makefile.include @@ -160,6 +160,8 @@ ifneq (,$(filter shell_lock,$(USEMODULE))) include $(RIOTBASE)/sys/shell_lock/Makefile.include endif +PSEUDOMODULES += shell_democommands + ifneq (,$(filter rust_riotmodules,$(USEMODULE))) include $(RIOTBASE)/sys/rust_riotmodules/Makefile.include endif diff --git a/sys/rust_riotmodules/Cargo.toml b/sys/rust_riotmodules/Cargo.toml index 1d76a04ab2..45fe52acbe 100644 --- a/sys/rust_riotmodules/Cargo.toml +++ b/sys/rust_riotmodules/Cargo.toml @@ -11,3 +11,4 @@ publish = false # autogenerated (or at least automatically checked for consistency). riot-module-lsm303agr = { path = "../../drivers/lsm303agr", optional = true } +riot-module-shell-democommands = { path = "../../sys/shell/democommands", optional = true } diff --git a/sys/rust_riotmodules/src/lib.rs b/sys/rust_riotmodules/src/lib.rs index 99dfa4814a..7cfc869e4c 100644 --- a/sys/rust_riotmodules/src/lib.rs +++ b/sys/rust_riotmodules/src/lib.rs @@ -8,3 +8,6 @@ #[cfg(feature = "riot-module-lsm303agr")] extern crate riot_module_lsm303agr; + +#[cfg(feature = "riot-module-shell-democommands")] +extern crate riot_module_shell_democommands; diff --git a/sys/shell/democommands/Cargo.toml b/sys/shell/democommands/Cargo.toml new file mode 100644 index 0000000000..91bebce99f --- /dev/null +++ b/sys/shell/democommands/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "riot-module-shell-democommands" +version = "0.1.0" +edition = "2021" + +authors = ["Christian Amsüss "] +license = "LGPL-2.1-only" + +# Shipped with RIOT-OS; this has no external API that would make +# sense to consume in any context than from within RIOT +publish = false + +[dependencies] +riot-wrappers = "^0.7.17" diff --git a/sys/shell/democommands/src/lib.rs b/sys/shell/democommands/src/lib.rs new file mode 100644 index 0000000000..3c8623c2b1 --- /dev/null +++ b/sys/shell/democommands/src/lib.rs @@ -0,0 +1,17 @@ +#![no_std] + +use riot_wrappers::println; +use core::fmt::Write; + +riot_wrappers::static_command!(static_hello_world, "hello_world", "Print a greeting", hello_world); + +pub fn hello_world<'a>(_w: &mut impl Write, args: impl IntoIterator) { + let mut args = args.into_iter(); + let commandname = args.next().expect("How was this started without an argv[0]?"); + + match args.next() { + Some("--help") => println!("Usage: {commandname}"), + None => println!("Hello RIOT!"), + _ => println!("Invalid argument."), + }; +}