Skip to content

Commit

Permalink
Move config functions to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
tapnisu committed Apr 5, 2024
1 parent 28fccbc commit 51973a8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 69 deletions.
69 changes: 69 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::Cli;
use serde::{Deserialize, Serialize};
use std::env;
use std::error::Error;
use std::fmt::Display;
use std::fs::{self, File};
use std::io::{self, Write};
use std::path::PathBuf;
Expand Down Expand Up @@ -62,3 +66,68 @@ pub fn parse_config(path: &PathBuf) -> anyhow::Result<Config> {

Ok(config)
}

// Todo: Return struct instead of tuple
pub fn get_tokens(args: &Cli, config: &Config) -> anyhow::Result<(String, String, String)> {
let discord_token = get_discord_token(args, config)?;
let telegram_token = get_telegram_token(args, config)?;
let output_channel = get_output_channel(args, config)?;

Ok((discord_token, telegram_token, output_channel))
}

fn get_telegram_token(args: &Cli, config: &Config) -> anyhow::Result<String> {
let telegram_token = match (
args.telegram_token.clone(),
config.telegram_token.clone(),
env::var("TELEGRAM_TOKEN"),
) {
(Some(telegram_token), _, _) => telegram_token,
(_, Some(telegram_token), _) => telegram_token,
(_, _, Ok(telegram_token)) => telegram_token,
(None, None, Err(_)) => return Err(ConfigTokenError("Telegram token".to_owned()).into()),
};

Ok(telegram_token)
}

fn get_discord_token(args: &Cli, config: &Config) -> anyhow::Result<String> {
let discord_token = match (
args.discord_token.clone(),
config.discord_token.clone(),
env::var("DISCORD_TOKEN"),
) {
(Some(discord_token), _, _) => discord_token,
(_, Some(discord_token), _) => discord_token,
(_, _, Ok(discord_token)) => discord_token,
(None, None, Err(_)) => return Err(ConfigTokenError("Discord token".to_owned()).into()),
};

Ok(discord_token)
}

fn get_output_channel(args: &Cli, config: &Config) -> anyhow::Result<String> {
let output_chat_id = match (
args.output_chat_id.clone(),
config.output_chat_id.clone(),
env::var("OUTPUT_CHAT_ID"),
) {
(Some(output_chat_id), _, _) => output_chat_id,
(_, Some(output_chat_id), _) => output_chat_id,
(_, _, Ok(output_chat_id)) => output_chat_id,
(None, None, Err(_)) => return Err(ConfigTokenError("Output channel".to_owned()).into()),
};

Ok(output_chat_id)
}

#[derive(Clone, Debug)]
struct ConfigTokenError(String);

impl Display for ConfigTokenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} not found!", self.0)
}
}

impl Error for ConfigTokenError {}
73 changes: 4 additions & 69 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::{error::ErrorKind, CommandFactory, Parser};
use dsc_tg_forwarder::{cli::Cli, config::parse_config, handler::Handler, Config};
use dsc_tg_forwarder::{cli::Cli, config, config::parse_config, handler::Handler};
use serenity::prelude::*;
use std::{env, error::Error, fmt::Display};
use teloxide::prelude::*;

#[tokio::main]
Expand All @@ -18,8 +17,9 @@ async fn main() {

let config = parse_config(&config_path.into())
.unwrap_or_else(|err| cmd.error(ErrorKind::InvalidValue, err).exit());
let (discord_token, telegram_token, output_chat_id) = get_tokens(&args.clone(), &config)
.unwrap_or_else(|err| cmd.error(ErrorKind::InvalidValue, err).exit());
let (discord_token, telegram_token, output_chat_id) =
config::get_tokens(&args.clone(), &config)
.unwrap_or_else(|err| cmd.error(ErrorKind::InvalidValue, err).exit());

// Login with a bot token from the environment
let mut client = Client::builder(discord_token)
Expand All @@ -41,68 +41,3 @@ async fn main() {
cmd.error(ErrorKind::InvalidValue, err).exit()
}
}

// Todo: Return struct instead of tuple
fn get_tokens(args: &Cli, config: &Config) -> anyhow::Result<(String, String, String)> {
let discord_token = get_discord_token(args, config)?;
let telegram_token = get_telegram_token(args, config)?;
let output_channel = get_output_channel(args, config)?;

Ok((discord_token, telegram_token, output_channel))
}

fn get_telegram_token(args: &Cli, config: &Config) -> anyhow::Result<String> {
let telegram_token = match (
args.telegram_token.clone(),
config.telegram_token.clone(),
env::var("TELEGRAM_TOKEN"),
) {
(Some(telegram_token), _, _) => telegram_token,
(_, Some(telegram_token), _) => telegram_token,
(_, _, Ok(telegram_token)) => telegram_token,
(None, None, Err(_)) => return Err(ConfigTokenError("Telegram token".to_owned()).into()),
};

Ok(telegram_token)
}

fn get_discord_token(args: &Cli, config: &Config) -> anyhow::Result<String> {
let discord_token = match (
args.discord_token.clone(),
config.discord_token.clone(),
env::var("DISCORD_TOKEN"),
) {
(Some(discord_token), _, _) => discord_token,
(_, Some(discord_token), _) => discord_token,
(_, _, Ok(discord_token)) => discord_token,
(None, None, Err(_)) => return Err(ConfigTokenError("Discord token".to_owned()).into()),
};

Ok(discord_token)
}

fn get_output_channel(args: &Cli, config: &Config) -> anyhow::Result<String> {
let output_chat_id = match (
args.output_chat_id.clone(),
config.output_chat_id.clone(),
env::var("OUTPUT_CHAT_ID"),
) {
(Some(output_chat_id), _, _) => output_chat_id,
(_, Some(output_chat_id), _) => output_chat_id,
(_, _, Ok(output_chat_id)) => output_chat_id,
(None, None, Err(_)) => return Err(ConfigTokenError("Output channel".to_owned()).into()),
};

Ok(output_chat_id)
}

#[derive(Clone, Debug)]
struct ConfigTokenError(String);

impl Display for ConfigTokenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} not found!", self.0)
}
}

impl Error for ConfigTokenError {}

0 comments on commit 51973a8

Please sign in to comment.