-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/main' into release
- Loading branch information
Showing
11 changed files
with
178 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use anyhow::anyhow; | ||
use log::{info, warn}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::path::PathBuf; | ||
use tokio::fs; | ||
|
||
const LOG_TARGET: &str = "tari::universe::app_config"; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, Default)] | ||
pub struct AppConfigFromFile { | ||
pub mode: String, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] | ||
pub enum MiningMode { | ||
Eco, | ||
Ludicrous, | ||
} | ||
|
||
impl MiningMode { | ||
pub fn from_str(s: &str) -> Option<MiningMode> { | ||
match s { | ||
"Eco" => Some(MiningMode::Eco), | ||
"Ludicrous" => Some(MiningMode::Ludicrous), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub fn to_str(m: MiningMode) -> String { | ||
match m { | ||
MiningMode::Eco => String::from("Eco"), | ||
MiningMode::Ludicrous => String::from("Ludicrous"), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct AppConfig { | ||
config_file: Option<PathBuf>, | ||
pub mode: MiningMode, | ||
// auto-mining in future etc. | ||
} | ||
|
||
impl AppConfig { | ||
pub fn new() -> Self { | ||
Self { | ||
config_file: None, | ||
mode: MiningMode::Eco, | ||
} | ||
} | ||
|
||
pub async fn load_or_create(&mut self, config_path: PathBuf) -> Result<(), anyhow::Error> { | ||
let file: PathBuf = config_path.join("app_config.json"); | ||
self.config_file = Some(file.clone()); | ||
|
||
if file.exists() { | ||
info!(target: LOG_TARGET, "Loading app config from file: {:?}", file); | ||
let config = fs::read_to_string(&file).await?; | ||
match serde_json::from_str::<AppConfigFromFile>(&config) { | ||
Ok(config) => { | ||
self.mode = MiningMode::from_str(&config.mode).unwrap_or(MiningMode::Eco); | ||
} | ||
Err(e) => { | ||
warn!(target: LOG_TARGET, "Failed to parse app config: {}", e.to_string()); | ||
} | ||
} | ||
} | ||
info!(target: LOG_TARGET, "App config does not exist or is corrupt. Creating new one"); | ||
let config = &AppConfigFromFile { | ||
mode: MiningMode::to_str(self.mode.clone()), | ||
}; | ||
let config = serde_json::to_string(&config)?; | ||
fs::write(file, config).await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn set_mode(&mut self, mode: String) -> Result<(), anyhow::Error> { | ||
let new_mode = match mode.as_str() { | ||
"Eco" => MiningMode::Eco, | ||
"Ludicrous" => MiningMode::Ludicrous, | ||
_ => return Err(anyhow!("Invalid mode")), | ||
}; | ||
self.mode = new_mode; | ||
self.update_config_file().await?; | ||
Ok(()) | ||
} | ||
|
||
pub fn get_mode(&self) -> MiningMode { | ||
self.mode.clone() | ||
} | ||
|
||
pub async fn update_config_file(&mut self) -> Result<(), anyhow::Error> { | ||
let file = self.config_file.clone().unwrap(); | ||
let config = &AppConfigFromFile { | ||
mode: MiningMode::to_str(self.mode.clone()), | ||
}; | ||
let config = serde_json::to_string(config)?; | ||
info!(target: LOG_TARGET, "Updating config file: {:?} {:?}", file, self.clone()); | ||
fs::write(file, config).await?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters