Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Переписал код на константы.
Browse files Browse the repository at this point in the history
Теперь не нужно передавать инстанс обьекта через аргументы функции
  • Loading branch information
TOwInOK committed Jul 4, 2024
1 parent 7137e4f commit 3524744
Show file tree
Hide file tree
Showing 19 changed files with 203 additions and 235 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"


[dependencies]
async-lazy = { version = "=0.1.0", features = ["parking_lot"] }
async-trait = "0.1.80"
async-watcher = "0.3.0"
bytes = "1.6.0"
Expand Down
71 changes: 0 additions & 71 deletions src/dictionary/dictionary.rs

This file was deleted.

85 changes: 84 additions & 1 deletion src/dictionary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,89 @@
mod config;
mod defunct;
pub mod dictionary;
mod downloader;
mod manager;
mod model;

use std::{fs::File, io::Write};

use config::ConfigMessages;
use defunct::DefunctMessages;
use downloader::DownloaderMessages;
use log::{error, warn};
use manager::ManagerMessages;
use model::ModelMessages;
use serde::{Deserialize, Serialize};

use crate::tr::load::Load;

#[derive(Deserialize, Serialize)]
pub struct MessageDictionary {
intro: String,
downloader: DownloaderMessages,
manager: ManagerMessages,
config: ConfigMessages,
model: ModelMessages,
defunct: DefunctMessages,
}

impl Default for MessageDictionary {
fn default() -> Self {
Self {
intro: "MDM ready to work!".into(),
downloader: DownloaderMessages::default(),
manager: ManagerMessages::default(),
config: ConfigMessages::default(),
model: ModelMessages::default(),
defunct: DefunctMessages::default(),
}
}
}
impl MessageDictionary {
pub fn get_dict() -> crate::errors::error::Result<Self> {
'_default_language_scope: {
let path = <MessageDictionary as Load>::PATH;
if File::open(path).is_err() {
let default = MessageDictionary::default();
warn!("Create default language file");
let mut file = File::create(path)?;
let toml_default = toml::to_string_pretty(&default)?;
file.write_all(toml_default.as_bytes())?;
}
}
match MessageDictionary::load_sync() {
Ok(e) => Ok(e),
Err(e) => {
error!("MessageDictionary: {}", e);
warn!("Load default Dictionary");
Ok(MessageDictionary::default())
}
}
}

pub fn intro(&self) -> &str {
&self.intro
}

pub fn downloader(&self) -> &DownloaderMessages {
&self.downloader
}

pub fn config(&self) -> &ConfigMessages {
&self.config
}
pub fn manager(&self) -> &ManagerMessages {
&self.manager
}

pub fn model(&self) -> &ModelMessages {
&self.model
}

pub fn defunct(&self) -> &DefunctMessages {
&self.defunct
}
}

impl Load for MessageDictionary {
const PATH: &'static str = "dictionary.toml";
}
8 changes: 4 additions & 4 deletions src/lock/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ impl CoreMeta {
&self.provider
}

pub fn version(&self) -> Option<&String> {
self.version.as_ref()
pub fn version(&self) -> Option<&str> {
self.version.as_deref()
}

pub fn build(&self) -> Option<&String> {
self.build.as_ref()
pub fn build(&self) -> Option<&str> {
self.build.as_deref()
}
pub fn path(&self) -> &str {
self.path.as_ref()
Expand Down
8 changes: 3 additions & 5 deletions src/lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ pub mod ext_metalist;
use indicatif::ProgressBar;
use log::debug;
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;

use self::core::CoreMeta;
use self::ext_metalist::ExtensionMetaList;
use crate::settings::Settings;
use crate::DICTIONARY;
use crate::{
settings::core::Core,
tr::{load::Load, save::Save},
};
use crate::{DICTIONARY, SETTINGS};
use std::sync::Arc;

#[derive(Default, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -49,15 +47,15 @@ impl Lock {
pub async fn remove_core(&mut self) {
self.core.remove().await;
}
pub async fn remove_defunct(&mut self, settings: Arc<RwLock<Settings>>, pb: Arc<ProgressBar>) {
pub async fn remove_defunct(&mut self, pb: Arc<ProgressBar>) {
debug!(
"fn() remove_nonexistent => keys list: {:#?}",
&self.plugins().0
);
pb.set_message(DICTIONARY.defunct().start_remove_defunct());

// delete for core you can found in Core::download()
if let Some(settings_plugins) = settings.read().await.plugins() {
if let Some(settings_plugins) = SETTINGS.read().await.plugins() {
let lock_list = self.plugins().get_list().clone();
for (key, _) in lock_list {
if !settings_plugins.items().contains_key(&key) {
Expand Down
17 changes: 15 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@ pub mod query;
pub mod settings;
pub mod tr;

use std::sync::Arc;

use crate::errors::error::Result;
use dictionary::dictionary::MessageDictionary;
use dictionary::MessageDictionary;
use indicatif::MultiProgress;
use lock::Lock;
use manager::{load_lock, load_settings};
use once_cell::sync::Lazy;
use settings::Settings;
use tokio::sync::{Mutex, RwLock};

static DICTIONARY: Lazy<MessageDictionary> = Lazy::new(|| MessageDictionary::get_dict().unwrap());

static MPB: Lazy<Arc<MultiProgress>> = Lazy::new(|| Arc::new(MultiProgress::new()));

static SETTINGS: Lazy<Arc<RwLock<Settings>>> = Lazy::new(|| load_settings().unwrap());
static LOCK: Lazy<Arc<Mutex<Lock>>> = Lazy::new(|| load_lock().unwrap());

static DICTIONARY: Lazy<MessageDictionary> = Lazy::new(|| MessageDictionary::get_dict());
#[tokio::main]
async fn main() -> Result<()> {
manager::run().await
Expand Down
28 changes: 6 additions & 22 deletions src/manager/download.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
use std::sync::Arc;
use std::time::Duration;

use indicatif::MultiProgress;
use log::error;
use tokio::sync::{Mutex, RwLock};
use tokio::time::sleep;
use tokio_util::sync::CancellationToken;

use crate::errors::error::Result;
use crate::settings::additions::Additions;
use crate::{lock::Lock, settings::Settings};
use crate::SETTINGS;

pub async fn download(
settings: Arc<RwLock<Settings>>,
lock: Arc<Mutex<Lock>>,
mpb: Arc<MultiProgress>,
key: Arc<CancellationToken>,
) -> Result<()> {
let duration = settings
pub async fn download(key: Arc<CancellationToken>) -> Result<()> {
let duration = SETTINGS
.read()
.await
.additions()
Expand All @@ -27,28 +20,19 @@ pub async fn download(
let cooldown = Duration::from_secs(duration);
loop {
'_core_scope: {
let lock = Arc::clone(&lock);
let settings = Arc::clone(&settings);
let mpb = Arc::clone(&mpb);
tokio::spawn(async move {
let settings = settings.read().await;
settings.core().download(lock, mpb).await
let settings = SETTINGS.read().await;
settings.core().download().await
});
}
'_plugins_scope: {
let lock = Arc::clone(&lock);
let settings = Arc::clone(&settings);
let mpb = Arc::clone(&mpb);

tokio::spawn(async move {
let settings = settings.read().await;
let settings = SETTINGS.read().await;
if let Some(plugins) = settings.plugins() {
plugins
.download_all(
settings.core().provider().as_str(),
settings.core().version(),
lock,
mpb,
)
.await
.map_err(|e| {
Expand Down
22 changes: 6 additions & 16 deletions src/manager/manage.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
use crate::DICTIONARY;
use crate::{DICTIONARY, MPB};
use std::sync::Arc;

use indicatif::MultiProgress;
use tokio::sync::{mpsc::Receiver, Mutex, RwLock};
use tokio::sync::mpsc::Receiver;
use tokio_util::sync::CancellationToken;

use crate::errors::error::Result;
use crate::manager::download::download;
use crate::manager::messages::Messages;
use crate::{lock::Lock, settings::Settings};

pub async fn manage(
mut rx: Receiver<Messages>,
lock: Arc<Mutex<Lock>>,
settings: Arc<RwLock<Settings>>,
mpb: Arc<MultiProgress>,
) -> Result<()> {
let lock = Arc::clone(&lock);
let settings = Arc::clone(&settings);
let mpb = Arc::clone(&mpb);
pub async fn manage(mut rx: Receiver<Messages>) -> Result<()> {
let key = Arc::new(CancellationToken::new());
loop {
tokio::select! {
Expand All @@ -28,10 +18,10 @@ pub async fn manage(
let key = Arc::clone(&key);
pb.set_message(DICTIONARY.manager().stop_iteration());
key.cancel();
mpb.clear()?;
MPB.clear()?;
if key.is_cancelled() {
pb.set_message(DICTIONARY.manager().start_new_iteration());
tokio::spawn(download(settings.clone(), lock.clone(), mpb.clone(), key));
tokio::spawn(download(key));
} else {
pb.set_message(DICTIONARY.manager().waiting_new_iteration());
}
Expand All @@ -44,7 +34,7 @@ pub async fn manage(
Messages::Start(pb) => {
let key = Arc::clone(&key);
pb.finish_with_message(DICTIONARY.manager().start_new_iteration());
tokio::spawn(download(settings.clone(), lock.clone(), mpb.clone(), key));
tokio::spawn(download(key));
pb.finish_and_clear();
}
},
Expand Down
Loading

0 comments on commit 3524744

Please sign in to comment.