Files
ncmmiao/src/messager.rs
lkhsss 3ad5be3a01 - 修正了trace级别日志输出时显示debug级别的问题
- 🔨 优雅处理所有的错误
- 🔨 将代码分离为单个文件
2025-08-12 17:49:34 +08:00

73 lines
2.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use colored::Colorize;
use log::{error, info, warn};
use crate::{messager, ncmdump,AppError};
use std::fmt::Debug;
use std::sync::mpsc;
pub struct Messager {
name: String,
sender: mpsc::Sender<messager::Message>,
}
pub struct Message {
pub name: String,
pub signal: Signals,
}
impl Message {
// 定义一个公共方法 log用于记录不同信号状态下的日志信息
pub fn log(&self) {
let loginfo = match &self.signal {
Signals::Start => "读取文件",
Signals::GetMetaInfo => "解密歌曲元信息",
Signals::GetCover => "解密封面图片数据",
Signals::Decrypt => "解密歌曲信息",
Signals::Save => "保存文件",
Signals::End => "成功!",
Signals::Err(e) => &e.to_string(),
};
match &self.signal {
Signals::Err(e) => match e {
AppError::ProtectFile => warn!("[{}] {}", self.name.cyan(), loginfo),
_ => error!("[{}] {}", self.name.cyan(), loginfo),
},
_ => info!("[{}] {}", self.name.cyan(), loginfo),
}
}
}
#[derive(PartialEq)]
pub enum Signals {
Start,
GetMetaInfo,
GetCover,
Decrypt,
Save,
End,
Err(AppError),
}
impl Messager {
pub fn new(name: String, sender: mpsc::Sender<messager::Message>) -> Self {
Self { name, sender }
}
pub fn send(&self, s: Signals) -> Result<(), std::sync::mpsc::SendError<messager::Message>> {
self.sender.send(Message {
name: self.name.clone(),
signal: s,
})
}
}
impl Debug for Message {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let message = match &self.signal {
Signals::Start => "开始破解",
Signals::Decrypt => "开始解密",
Signals::Save => "保存文件",
Signals::End => "破解完成",
Signals::GetMetaInfo => "获取元数据",
Signals::GetCover => "获取封面",
Signals::Err(e) => &e.to_string(),
};
write!(f, "[{}] {}", self.name, message)
}
}