| Crates.io | module |
| lib.rs | module |
| version | 0.2.0 |
| created_at | 2025-05-04 15:49:43.532284+00 |
| updated_at | 2025-06-14 11:03:05.334996+00 |
| description | Modular NixOS-style configuration crate. |
| homepage | https://github.com/threadexio/module-rs |
| repository | https://github.com/threadexio/module-rs |
| max_upload_size | |
| id | 1659729 |
| size | 70,458 |
A crate that provides:
A standard interface for merging data.
Strategies for merging that data.
The above can be used to build a module system akin to that of NixOS. With this crate you can easily build modular and composable configuration for your application.
The core functionality of module is provided by the Merge trait, it is the
lowest-level building block of this crate and provides the interface for merging
data. A value is called "mergeable" if it can be merged with the Merge trait.
A collection of such values is typically referred to as a "module".
Upon the Merge trait build wrappers like Overridable and NoMerge
which provide the "how" to merge data. Most common types from core, alloc
and std have a sensible merge strategy. However, these wrappers can be used
to refine the merging of data even more. Such wrappers live under types.
When merging complex nested data, it is possible that an error occurs in a merge
operation deep inside the data. Error and Context exist to provide useful
errors and context about where the failure happened and why.
To create such complex structures, the crate provides the Merge derive macro
that merges each field of the struct and automatically adds the necessary context.
NOTE: The derive macro only supports structs. It is not obvious how to merge enums and a handwritten implementation will probably be better for readability in this case.
Just the Merge trait is not enough however to build a fully functioning
module system from configuration files in some format. Users of the crate must
build an "evaluator".
The evaluator's job is to construct such modules and merge them together. The source of these modules can be files from disk, environment variables and generally any medium that can be used to pass information to the app.
Evaluators can be purpose-written for custom solutions. However, for some common
use cases, the module-util crate provides some fully functional evaluators.
Full examples do not fit here. See examples for reimplementations of popular programs using this crate.
std: Implement Merge for many std types. This is a default feature.
Disabling it will also make the entire crate no_std. (alloc is still required)
derive: Enable the Merge derive macro. This is disabled by default to
avoid introducing the heavy dependencies of proc-macros when not needed.
serde: Implement serde::Deserialize for types under types,
allowing them to be used seamlessly with serde.
All code in this repository is licensed under the Apache 2.0 license, a copy of which can be found here.