: write back tokens to the config file

This commit is contained in:
VC
2024-09-09 13:33:07 +02:00
parent 5f03c3b8f1
commit 4c2433a79e
6 changed files with 86 additions and 61 deletions

View File

@@ -1,8 +1,11 @@
use serde::Deserialize;
use std::fs::read_to_string;
use serde::{Deserialize, Serialize};
use std::{
error::Error,
fs::{read_to_string, write},
};
// General configuration Struct
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
pub peertube: PeertubeConfig,
pub youtube: YoutubeConfig,
@@ -10,7 +13,7 @@ pub struct Config {
pub tootube: TootubeConfig,
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct PeertubeConfig {
pub base_url: String,
#[serde(default)]
@@ -28,21 +31,21 @@ impl Default for PeertubeConfig {
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PeertubeConfigOauth2 {
pub client_id: String,
pub client_secret: String,
pub refresh_token: String,
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct YoutubeConfig {
pub refresh_token: String,
pub client_id: String,
pub client_secret: String,
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct TootubeConfig {
pub progress_bar: String,
pub progress_chars: String,
@@ -57,13 +60,22 @@ impl Default for TootubeConfig {
}
}
/// Parses the TOML file into a Config struct
pub fn parse_toml(toml_file: &str) -> Config {
let toml_config =
read_to_string(toml_file).unwrap_or_else(|e| panic!("Cannot open file {toml_file}: {e}"));
impl Config {
/// Parses the TOML file into a Config struct
pub fn new(toml_file: &str) -> Self {
let toml_config = read_to_string(toml_file)
.unwrap_or_else(|e| panic!("Cannot open file {toml_file}: {e}"));
let config: Config = toml::from_str(&toml_config)
.unwrap_or_else(|e| panic!("Cannot parse TOML file {toml_file}: {e}"));
let config: Config = toml::from_str(&toml_config)
.unwrap_or_else(|e| panic!("Cannot parse TOML file {toml_file}: {e}"));
config
config
}
pub fn dump(&mut self, toml_file: &str) -> Result<(), Box<dyn Error>> {
// bring back the default for progress bar
self.tootube = TootubeConfig::default();
write(toml_file, toml::to_string_pretty(self)?)?;
Ok(())
}
}