Skip to content

Commit

Permalink
refactor: refactor ConfigRef struct
Browse files Browse the repository at this point in the history
  • Loading branch information
yixiaojiu committed Nov 10, 2024
1 parent 98af4fe commit f2dc0a3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
18 changes: 13 additions & 5 deletions kimika/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,30 @@ pub struct SenderConfig {
pub receiver_port: u16,
}

pub struct ConfigOnceCell(pub OnceCell<Config>);
pub struct ConfigOnceCell {
inner: OnceCell<Config>,
}

impl std::ops::Deref for ConfigOnceCell {
type Target = Config;
fn deref(&self) -> &Self::Target {
self.0.get().unwrap()
self.inner.get().unwrap()
}
}

impl std::fmt::Debug for ConfigOnceCell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0.get().unwrap())
write!(f, "{:?}", self.inner.get().unwrap())
}
}

impl ConfigOnceCell {
pub const fn new() -> Self {
Self {
inner: OnceCell::new(),
}
}

pub fn set_from_send_args(&self, args: &send::SendArgs) -> Result<(), Config> {
let mut config = Config::new();
if let Some(alias) = args.alias.clone() {
Expand All @@ -60,7 +68,7 @@ impl ConfigOnceCell {
if let Some(receiver_port) = args.receiver_port {
config.sender.receiver_port = receiver_port
}
self.0.set(config)
self.inner.set(config)
}

pub fn set_from_receive_args(&self, args: &receive::ReceiveArgs) -> Result<(), Config> {
Expand All @@ -74,7 +82,7 @@ impl ConfigOnceCell {
if let Some(save_folder) = args.folder.clone() {
config.receiver.save_folder = save_folder
}
self.0.set(config)
self.inner.set(config)
}
}

Expand Down
3 changes: 1 addition & 2 deletions kimika/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ mod utils;

use clap::{Parser, Subcommand};
use config::ConfigOnceCell;
use once_cell::sync::OnceCell;

pub static CONFIG: ConfigOnceCell = ConfigOnceCell(OnceCell::new());
pub static CONFIG: ConfigOnceCell = ConfigOnceCell::new();

#[derive(Parser)]
#[command(version, long_about = None, styles = utils::clap::clap_styles())]
Expand Down
2 changes: 1 addition & 1 deletion kimika/src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct ReceiveArgs {
}

pub async fn receive(args: ReceiveArgs) -> Result<(), Box<dyn std::error::Error>> {
let _result = CONFIG.set_from_receive_args(&args);
CONFIG.set_from_receive_args(&args).unwrap();

if args.server {
remote::remote_receive(&args).await?;
Expand Down
2 changes: 1 addition & 1 deletion kimika/src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub async fn send(args: SendArgs) -> Result<(), Box<dyn std::error::Error>> {
return Ok(());
}

let _result = CONFIG.set_from_send_args(&args);
CONFIG.set_from_send_args(&args).unwrap();

if args.server {
remote::remote_send(&args).await?;
Expand Down

0 comments on commit f2dc0a3

Please sign in to comment.