From fa6591ab8788fa96241253089f5330074aa96b19 Mon Sep 17 00:00:00 2001 From: fluo10 Date: Sat, 24 May 2025 09:17:45 +0900 Subject: [PATCH] Add actor_id to NodeConfig --- lazy-supplements/src/cli/server.rs | 8 ++-- lazy-supplements/src/config/mod.rs | 10 ++++- lazy-supplements/src/config/node.rs | 34 ++++++++++++++--- lazy-supplements/src/config/server.rs | 54 +++++++++++++++++++++++++-- lazy-supplements/src/error.rs | 5 +++ lazy-supplements/src/lib.rs | 3 +- 6 files changed, 99 insertions(+), 15 deletions(-) create mode 100644 lazy-supplements/src/error.rs diff --git a/lazy-supplements/src/cli/server.rs b/lazy-supplements/src/cli/server.rs index 0b2e4f4..0c17508 100644 --- a/lazy-supplements/src/cli/server.rs +++ b/lazy-supplements/src/cli/server.rs @@ -2,12 +2,12 @@ use std::{net::IpAddr, path::PathBuf}; use clap::Args; +use crate::config::PartialServerConfig; + #[derive(Args, Debug)] pub struct ServerArgs { - #[arg(long)] - listen_ip: IpAddr, - #[arg(long)] - port: i32, + #[command(flatten)] + server_config: PartialServerConfig, #[arg(long)] config: PathBuf, } \ No newline at end of file diff --git a/lazy-supplements/src/config/mod.rs b/lazy-supplements/src/config/mod.rs index 16b8333..4006ea8 100644 --- a/lazy-supplements/src/config/mod.rs +++ b/lazy-supplements/src/config/mod.rs @@ -2,4 +2,12 @@ mod node; mod server; pub use node::NodeConfig; -pub use server::ServerConfig; +pub use server::{ + PartialServerConfig, + ServerConfig, + DEFAULT_LISTEN_IPS, + DEFAULT_PORT, + DEFAULT_PARTIAL_SERVER_CONFIG, + DEFAULT_SERVER_CONFIG +}; + diff --git a/lazy-supplements/src/config/node.rs b/lazy-supplements/src/config/node.rs index 4f80ba9..0513554 100644 --- a/lazy-supplements/src/config/node.rs +++ b/lazy-supplements/src/config/node.rs @@ -1,3 +1,4 @@ +use automerge::ActorId; use libp2p::identity::{self, Keypair}; use serde::{Deserialize, Serialize}; @@ -5,6 +6,8 @@ use serde::{Deserialize, Serialize}; pub struct NodeConfig { #[serde(with = "keypair")] secret: Keypair, + #[serde(with = "actor_id")] + actor_id: ActorId } mod keypair { @@ -26,28 +29,49 @@ mod keypair { let vec = BASE64_STANDARD.decode(base64).unwrap(); Ok(Keypair::from_protobuf_encoding(&vec).unwrap()) } - } -pub struct PartialNodeConfig { - pub secret: Option>, +mod actor_id { + use automerge::ActorId; + use base64::{prelude::BASE64_STANDARD, Engine}; + use serde::{Deserialize, Deserializer, Serializer}; + + pub fn serialize(actor_id: &ActorId, serializer: S) -> Result + where S: Serializer + { + let bytes = actor_id.to_bytes(); + let base64 = BASE64_STANDARD.encode(bytes); + serializer.serialize_str(&base64) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result + where D: Deserializer<'de> + { + let base64 = String::deserialize(deserializer)?; + let vec = BASE64_STANDARD.decode(base64).unwrap(); + Ok(ActorId::from(vec)) + } } #[cfg(test)] mod tests { + use automerge::ActorId; use libp2p::identity; use super::*; #[tokio::test] - async fn parse_ed25519() { + async fn serialize_deserialize() { let keypair = identity::Keypair::generate_ed25519(); + let actor_id = ActorId::random(); let config = NodeConfig { secret: keypair.clone(), + actor_id: actor_id.clone(), }; let string = toml::to_string(&config).unwrap(); println!("Parsed config: {}", &string); let parsed_config: NodeConfig = toml::from_str(&string).unwrap(); - assert_eq!(keypair.public(), parsed_config.secret.public()) + assert_eq!(keypair.public(), parsed_config.secret.public()); + assert_eq!(actor_id, parsed_config.actor_id); } } diff --git a/lazy-supplements/src/config/server.rs b/lazy-supplements/src/config/server.rs index 854dc7c..0deed9b 100644 --- a/lazy-supplements/src/config/server.rs +++ b/lazy-supplements/src/config/server.rs @@ -1,9 +1,55 @@ -use std::{collections::HashSet, net::IpAddr}; +use std::{collections::HashSet, net::{IpAddr, Ipv4Addr}, str::FromStr, sync::LazyLock}; +use automerge::hydrate::List; +use clap::Args; use serde::{Deserialize, Serialize}; -#[derive(Debug, Deserialize, Serialize)] +use crate::error::Error; + +pub static DEFAULT_LISTEN_IPS: &[IpAddr] = &[IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))]; +pub static DEFAULT_PORT: u16 = 8080; +pub static DEFAULT_SERVER_CONFIG: LazyLock = LazyLock::new(|| { + ServerConfig{ + listen_ips: Vec::from(DEFAULT_LISTEN_IPS), + port: DEFAULT_PORT + } +}); +pub static DEFAULT_PARTIAL_SERVER_CONFIG: LazyLock = LazyLock::new(|| { + PartialServerConfig::from((*DEFAULT_SERVER_CONFIG).clone()) +}); + +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct ServerConfig { - listen_ips: HashSet, - port: i32, + listen_ips: Vec, + port: u16, } + +impl TryFrom for ServerConfig { + type Error = Error; + fn try_from(config: PartialServerConfig) -> Result{ + Ok(ServerConfig { + listen_ips: config.listen_ips.ok_or(Error::MissingConfig("listen_ips".to_string()))?, + port: config.port.ok_or(Error::MissingConfig("port".to_string()))? + }) + } +} + +#[derive(Args, Debug, Deserialize, Serialize)] +pub struct PartialServerConfig { + #[arg(long)] + listen_ips: Option>, + #[arg(long)] + port: Option, +} + +impl From for PartialServerConfig { + fn from(config: ServerConfig) -> PartialServerConfig { + PartialServerConfig { + listen_ips: Some(config.listen_ips), + port: Some(config.port) + } + } +} + + + diff --git a/lazy-supplements/src/error.rs b/lazy-supplements/src/error.rs new file mode 100644 index 0000000..cca475f --- /dev/null +++ b/lazy-supplements/src/error.rs @@ -0,0 +1,5 @@ +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("mandatory config `{0}` is missing")] + MissingConfig(String), +} \ No newline at end of file diff --git a/lazy-supplements/src/lib.rs b/lazy-supplements/src/lib.rs index 973dec0..bb3cdbf 100644 --- a/lazy-supplements/src/lib.rs +++ b/lazy-supplements/src/lib.rs @@ -1,2 +1,3 @@ pub mod cli; -pub mod config; \ No newline at end of file +pub mod config; +pub mod error; \ No newline at end of file