doko

Crates.iodoko
lib.rsdoko
version
sourcesrc
created_at2024-11-30 20:40:07.258663
updated_at2024-12-04 07:28:47.21743
descriptionRun methods from submodules by name
homepage
repositoryhttps://github.com/nonolai/doko
max_upload_size
id1466969
Cargo.toml error:TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
(nonolai)

documentation

README

doko

Run methods in submodules by name

Overview

doko lets you include and run a method from a known submodule without importing and creating the map from name to method yourself. Behind the scenes, the doko::doko! macro:

  • Finds all Rust files in the specified directory,
  • Includes them with mod
  • Creates function doko_<function> that takes a module name and returns <module name>::<function>. You can then call this function with whatever arguments you need.

Created to improve project layout for things like Project Euler and Advent of Code.

Usage

Project Layout

project/
 └──── src/
        ├──── main.rs
        └──── submod/
               ├───── a.rs
               └───── b.rs

project/src/submod/a.rs

pub fn greeting(name: &str) -> u32 {
        println!("Hello, {}, from a", name);
        4
}

project/src/submod/b.rs

pub fn greeting(name: &str) -> u32 {
        println!("Hello, {}, from b", name);
        5
}

project/src/main.rs

doko::doko!("src/submod", "greeting", (&str) -> u32);

pub fn main() {
        let name = "username";
        assert_eq!(4, doko_greeting("a")(name));
        assert_eq!(5, doko_greeting("b")(name));
}

TODO

  • Provide a cleaner API than generated functions (i.e. let registry = doko::doko!(...); registry.run(<module>);)
Commit count: 4

cargo fmt