Allow ConfigOption to be specified as const

This is the first part of two commits that attempts to simplify
the API that is used to retrieve configuration values.

Previously, we encouraged the following pattern to build a plugin.

```rust
let configured_plugin =
  Builder::new(
      tokio::io::stdin(),
      tokio::io::stdout())
  .option(ConfigOption::new_i64_with_default("some-option", 0, "Description"))
  .configure();

let value = configured_plugion.option("some-option");
match value {
  Some(Integer(i)) => {}, // Config provided
  None => {}, 		  // No config set
  _ => {}, 	          // This should never happened
}
```

This commit helps to move to the following pattern

```rust
const SOME_OPTION : ConfigOption<i64> = ConfigOption::new_i64_with_default(
                           "some-option",
			   "description");

async fn main() -> Result<()> {
    let plugin = Builder::new(tokio::io::stdin(), tokio::io::stdoout())
    	.option(SOME_OPTION)
	.configure()
	.await?;

    let value : i64 = plugin.option(SOME_OPTION)?;
}
```
This commit is contained in:
Erik De Smedt
2024-02-02 09:08:50 +01:00
committed by Christian Decker
parent 4ae18b2cfa
commit 543e67495c
4 changed files with 132 additions and 84 deletions

View File

@@ -12,15 +12,15 @@ async fn main() -> Result<(), anyhow::Error> {
let state = ();
if let Some(plugin) = Builder::new(tokio::io::stdin(), tokio::io::stdout())
.option(
options::ConfigOption::new_i64_with_default(
"test-option",
42,
"a test-option with default 42",
)
.build(),
)
.option(options::ConfigOption::new_opt_i64("opt-option", "An optional option").build())
.option(options::ConfigOption::new_i64_with_default(
"test-option",
42,
"a test-option with default 42",
))
.option(options::ConfigOption::new_i64_no_default(
"opt-option",
"An optional option",
))
.rpcmethod("testmethod", "This is a test", testmethod)
.rpcmethod(
"testoptions",