Add Iroh relay mode config
This commit is contained in:
parent
8334cc5e00
commit
0f2ad755d9
13 changed files with 210 additions and 21 deletions
|
|
@ -9,3 +9,4 @@ license.workspace = true
|
|||
directories.workspace = true
|
||||
serde.workspace = true
|
||||
thiserror.workspace = true
|
||||
toml_edit.workspace = true
|
||||
|
|
|
|||
|
|
@ -1,5 +1,14 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub const DEFAULT_CONFIG_TOML: &str = r#"# geth local node config
|
||||
# Remote node-to-node communication is Iroh-only.
|
||||
#
|
||||
# relay_mode controls Iroh relay use for practical internet connectivity.
|
||||
# Values: "default", "staging", "disabled".
|
||||
[iroh]
|
||||
relay_mode = "default"
|
||||
"#;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct GethPaths {
|
||||
home: PathBuf,
|
||||
|
|
@ -83,4 +92,106 @@ pub enum ConfigError {
|
|||
NoDataDirectory,
|
||||
#[error("io error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("toml parse error: {0}")]
|
||||
TomlParse(#[from] toml_edit::TomlError),
|
||||
#[error("invalid iroh relay_mode: {0}")]
|
||||
InvalidRelayMode(String),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct GethConfig {
|
||||
pub iroh: IrohConfig,
|
||||
}
|
||||
|
||||
impl GethConfig {
|
||||
#[must_use]
|
||||
pub fn default_toml() -> &'static str {
|
||||
DEFAULT_CONFIG_TOML
|
||||
}
|
||||
|
||||
pub fn load(path: &Path) -> Result<Self, ConfigError> {
|
||||
if !path.exists() {
|
||||
return Ok(Self::default());
|
||||
}
|
||||
Self::parse(&std::fs::read_to_string(path)?)
|
||||
}
|
||||
|
||||
pub fn parse(text: &str) -> Result<Self, ConfigError> {
|
||||
let document = text.parse::<toml_edit::DocumentMut>()?;
|
||||
let relay_mode = match document.get("iroh").and_then(|iroh| iroh.get("relay_mode")) {
|
||||
Some(item) => {
|
||||
let value = item
|
||||
.as_str()
|
||||
.ok_or_else(|| ConfigError::InvalidRelayMode(item.to_string()))?;
|
||||
RelayMode::parse(value)?
|
||||
}
|
||||
None => RelayMode::default(),
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
iroh: IrohConfig { relay_mode },
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct IrohConfig {
|
||||
pub relay_mode: RelayMode,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum RelayMode {
|
||||
Disabled,
|
||||
#[default]
|
||||
Default,
|
||||
Staging,
|
||||
}
|
||||
|
||||
impl RelayMode {
|
||||
pub fn parse(value: &str) -> Result<Self, ConfigError> {
|
||||
match value {
|
||||
"disabled" => Ok(Self::Disabled),
|
||||
"default" => Ok(Self::Default),
|
||||
"staging" => Ok(Self::Staging),
|
||||
other => Err(ConfigError::InvalidRelayMode(other.to_owned())),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::Disabled => "disabled",
|
||||
Self::Default => "default",
|
||||
Self::Staging => "staging",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn default_config_uses_iroh_relays() {
|
||||
let config = GethConfig::parse(GethConfig::default_toml()).expect("parse config");
|
||||
assert_eq!(config.iroh.relay_mode, RelayMode::Default);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_parses_disabled_relay_mode() {
|
||||
let config = GethConfig::parse("[iroh]\nrelay_mode = \"disabled\"\n").expect("parse");
|
||||
assert_eq!(config.iroh.relay_mode, RelayMode::Disabled);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_rejects_unknown_relay_mode() {
|
||||
let error = GethConfig::parse("[iroh]\nrelay_mode = \"magic\"\n").expect_err("error");
|
||||
assert!(matches!(error, ConfigError::InvalidRelayMode(value) if value == "magic"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_rejects_non_string_relay_mode() {
|
||||
let error = GethConfig::parse("[iroh]\nrelay_mode = true\n").expect_err("error");
|
||||
assert!(matches!(error, ConfigError::InvalidRelayMode(_)));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue