Add example, config, cli

This commit is contained in:
fluo10 2025-05-24 06:11:00 +09:00
parent c6f72e5e2b
commit ba87ccf734
12 changed files with 156 additions and 2 deletions

View file

@ -1,5 +1,5 @@
[workspace]
members = [ "lazy-supplements", "lazy-supplements-*" ]
members = [ "examples/timestamper", "lazy-supplements", "lazy-supplements-*" ]
[workspace.package]
edition = "2024"
@ -9,4 +9,5 @@ license = "MIT OR Apache-2.0"
repository = "https://forgejo.fireturlte.net"
[workspace.dependencies]
lazy-supplements.path = "lazy-supplements"
libp2p = "0.55.0"

View file

@ -0,0 +1,10 @@
[package]
name = "timestamper"
edition.workspace = true
version.workspace = true
description.workspace = true
license.workspace = true
repository.workspace = true
[dependencies]
lazy-supplements.workspace = true

View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View file

@ -8,4 +8,11 @@ repository.workspace = true
[dependencies]
automerge = "0.6.1"
libp2p.workspace = true
autosurgeon = "0.8.7"
base64 = "0.22.1"
clap = { version = "4.5.38", features = ["derive"] }
libp2p.workspace = true
serde = { version = "1.0.219", features = ["derive"] }
thiserror = "2.0.12"
tokio = { version = "1.45.0", features = ["macros", "rt"] }
toml = "0.8.22"

View file

@ -0,0 +1,13 @@
use std::{net::IpAddr, path::PathBuf};
use clap::Args;
#[derive(Args, Debug)]
pub struct ConnectArgs {
#[arg(long)]
endpoint: IpAddr,
#[arg(long)]
port: i32,
#[arg(long)]
config: PathBuf,
}

View file

@ -0,0 +1,29 @@
use std::path::PathBuf;
use clap::Args;
use libp2p::identity;
#[derive(Args, Debug)]
pub struct InitArgs {
#[arg(long)]
config: Option<PathBuf>
}
impl InitArgs {
fn main(self) {
let config_path = if let Some(x) = self.config {
x
} else {
crate::cli::default_config_path()
};
if config_path.exists() {
println!("Config file already exists!");
return;
} else {
let keypair = identity::Keypair::generate_ed25519();
let buf = keypair.to_protobuf_encoding().unwrap();
}
}
}

View file

@ -0,0 +1,9 @@
use std::path::PathBuf;
mod connect;
mod init;
mod server;
pub fn default_config_path() -> PathBuf {
todo!()
}

View file

@ -0,0 +1,13 @@
use std::{net::IpAddr, path::PathBuf};
use clap::Args;
#[derive(Args, Debug)]
pub struct ServerArgs {
#[arg(long)]
listen_ip: IpAddr,
#[arg(long)]
port: i32,
#[arg(long)]
config: PathBuf,
}

View file

@ -0,0 +1,5 @@
mod node;
mod server;
pub use node::NodeConfig;
pub use server::ServerConfig;

View file

@ -0,0 +1,53 @@
use libp2p::identity::{self, Keypair};
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
pub struct NodeConfig {
#[serde(with = "keypair")]
secret: Keypair,
}
mod keypair {
use base64::{prelude::BASE64_STANDARD, Engine};
use libp2p::identity::Keypair;
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S>(keypair: &Keypair, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
let vec = keypair.to_protobuf_encoding().unwrap();
let base64 = BASE64_STANDARD.encode(vec);
serializer.serialize_str(&base64)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Keypair, D::Error>
where D: Deserializer<'de>
{
let base64 = String::deserialize(deserializer)?;
let vec = BASE64_STANDARD.decode(base64).unwrap();
Ok(Keypair::from_protobuf_encoding(&vec).unwrap())
}
}
pub struct PartialNodeConfig {
pub secret: Option<Vec<u8>>,
}
#[cfg(test)]
mod tests {
use libp2p::identity;
use super::*;
#[tokio::test]
async fn parse_ed25519() {
let keypair = identity::Keypair::generate_ed25519();
let config = NodeConfig {
secret: keypair.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())
}
}

View file

@ -0,0 +1,9 @@
use std::{collections::HashSet, net::IpAddr};
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
pub struct ServerConfig {
listen_ips: HashSet<IpAddr>,
port: i32,
}

View file

@ -0,0 +1,2 @@
pub mod cli;
pub mod config;