[WIP] tangled knot rust implementation
1#![allow(dead_code)]
2
3use std::path::PathBuf;
4
5use serde::Deserialize;
6use url::Url;
7
8#[derive(Clone, Debug, Deserialize)]
9pub struct Config {
10 /// legacy, maybe replace to did in future
11 pub hostname: String,
12 pub owner_did: String,
13 pub listen_addr: String,
14 /// path to store db, git-motd, etc
15 pub data_dir: PathBuf,
16 /// path to store git repositories
17 pub data_repo_dir: PathBuf,
18 pub plc_url: Url,
19 pub jetstream_endpoint: Url,
20 pub appview_endpoint: Url,
21
22 pub git_user_name: String,
23 pub git_user_email: String,
24 // /// uhhhh do we need this? the default branch should be configured from user side
25 // pub git_init_default_branch: String,
26}
27
28#[derive(Debug, Default, Deserialize)]
29pub struct ConfigBuilder {
30 hostname: Option<String>,
31 // TODO: use DID type instead
32 owner_did: Option<String>,
33 listen_addr: Option<String>,
34 data_dir: Option<PathBuf>,
35 data_repo_dir: Option<PathBuf>,
36
37 plc_url: Option<Url>,
38 jetstream_endpoint: Option<Url>,
39 appview_endpoint: Option<Url>,
40 git_user_name: Option<String>,
41 git_user_email: Option<String>,
42}
43
44// TODO(boltless): maybe use bon-rs instead
45impl ConfigBuilder {
46 pub fn new(path: PathBuf) -> anyhow::Result<Self> {
47 if path.is_file() {
48 Ok(toml::from_str(&std::fs::read_to_string(&path)?)?)
49 } else {
50 Ok(Self::default())
51 }
52 }
53
54 pub fn build(self) -> anyhow::Result<Config> {
55 Ok(Config {
56 hostname: self.hostname.unwrap(),
57 owner_did: self.owner_did.unwrap(),
58 listen_addr: self.listen_addr.unwrap_or("0.0.0.0:5555".to_string()),
59 data_dir: self.data_dir.unwrap_or("/home/git".into()),
60 data_repo_dir: self.data_repo_dir.unwrap_or("/home/git".into()),
61 plc_url: self
62 .plc_url
63 .unwrap_or_else(|| Url::parse("https://plc.directory").unwrap()),
64 jetstream_endpoint: self.jetstream_endpoint.unwrap_or_else(|| {
65 Url::parse("wss://jetstream1.us-west.bsky.network").unwrap()
66 }),
67 appview_endpoint: self
68 .appview_endpoint
69 .unwrap_or_else(|| Url::parse("https://tangled.org").unwrap()),
70
71 git_user_name: self.git_user_name.unwrap_or("Tangled".to_string()),
72 git_user_email: self
73 .git_user_email
74 .unwrap_or("noreply@tangled.org".to_string()),
75 })
76 }
77}