Reputation: 1329
I've recently started with Rust and coming from a Node background I'm curious if the cargo.toml
file for a project can be extended to include custom properties.
For example, in Node, some packages let you put configuration options in the package.json
file (like babel):
{
"name": "my-package",
"version": "1.0.0",
"babel": {
"presets": [ ... ],
"plugins": [ ... ],
}
}
I've looked through the manifest docs but I can't seem to find anything about custom properties.
For example, what if I had a CLI tool and it has a configurable option. Do I have to have the user make a config file just for this option or could I include in the cargo.toml
like so:
[package]
name = "my_project"
version = "0.1.0"
edition = "2018"
# custom property
store: ["one", "two", "three"]
[dependencies]
Also, if this is a "sure, as long as you have name
and version
as required", is it something that should be avoided?
Upvotes: 2
Views: 700
Reputation: 15105
The Rust equivalent would be Cargo features. You can define features for a crate like this in the crate's Cargo.toml
:
[package]
name = "crate_with_features"
version = "1.0.0"
edition = "2018"
[features]
# default set of features
default = ["has_foo", "has_bar"]
# can use these for conditional compilation
has_foo = []
has_bar = []
And then if you had a lib.rs
like this:
[cfg(feature = "has_foo")]
pub fn foo() {
println!("foo");
}
[cfg(feature = "has_bar")]
pub fn bar() {
println!("bar");
}
People would be able to select just which features of your crate they want. For example:
[package]
name = "other_project"
[dependencies]
crate_with_features = "1.0.0"
In the default case, crate_with_features
would come with both foo
and bar
functions since the default
feature requires both of them, but people could choose to only use one or the other by specifying:
[package]
name = "other_project"
[dependencies.crate_with_features]
version = "1.0.0"
# don't include default feature set
default-features = false
# cherry pick individual features
features = ["has_foo"]
In the above example crate_with_features
would only be compiled with the foo
function for that project.
The Rust Reference has a page on conditional compilation if you'd like to learn more about that specifically.
Upvotes: 3