Adds Network::to_core_arg

This allows users to create `Network` instances from `bitcoind -chain` values
This commit is contained in:
Sergi Delgado Segura 2022-12-01 12:05:40 +01:00
parent cde120bb0f
commit bd1eb29f61
1 changed files with 22 additions and 0 deletions

View File

@ -119,6 +119,28 @@ impl Network {
Network::Regtest => "regtest",
}
}
/// Converts a `bitcoind -chain` argument name to its equivalent `Network`.
///
/// ```bash
/// $ bitcoin-23.0/bin/bitcoind --help | grep -C 3 '\-chain=<chain>'
/// Chain selection options:
///
/// -chain=<chain>
/// Use the chain <chain> (default: main). Allowed values: main, test, signet, regtest
/// ```
pub fn from_core_arg(core_arg: &str) -> Result<Self, ParseNetworkError> {
use Network::*;
let network = match core_arg {
"main" => Bitcoin,
"test" => Testnet,
"signet" => Signet,
"regtest" => Regtest,
_ => return Err(ParseNetworkError(core_arg.to_owned())),
};
Ok(network)
}
}
/// An error in parsing network string.