lot of refactor

This commit is contained in:
2026-03-14 19:21:56 +01:00
parent b63982714b
commit 8d4a002b73
22 changed files with 256 additions and 313 deletions
@@ -1,5 +1,5 @@
[package]
name = "nats"
name = "nats-libs"
version = "0.1.0"
edition = "2024"
@@ -1,16 +1,19 @@
use async_nats::Client;
use async_trait::async_trait;
use postcard::{from_bytes, to_stdvec};
use types::jobs::{JobResponse, JobsMap, JobsResponseMap};
use types::{
error::{CorroError, CorroErrorType},
jobs::{JobResponse, JobsMap, JobsResponseMap},
};
#[async_trait]
pub trait JobClient {
async fn send_job(&self, job: JobsMap) -> Result<JobsResponseMap, String>;
async fn send_job(&self, job: JobsMap) -> Result<JobsResponseMap, CorroError>;
}
#[async_trait]
impl JobClient for Client {
async fn send_job(&self, job: JobsMap) -> Result<JobsResponseMap, String> {
async fn send_job(&self, job: JobsMap) -> Result<JobsResponseMap, CorroError> {
let subject = match &job {
JobsMap::Download(_job) => "download",
JobsMap::Play(_job) => "play",
@@ -25,8 +28,6 @@ impl JobClient for Client {
.await
{
Ok(response) => {
println!("{:?}", &response.payload);
let parsed: JobResponse = from_bytes(&response.payload).unwrap();
if let Some(error) = parsed.error {
@@ -34,10 +35,16 @@ impl JobClient for Client {
} else if let Some(content) = parsed.content {
Ok(content)
} else {
Err("Unknown content".to_string())
Err(CorroError {
error_type: CorroErrorType::ParseError,
message: "Unknown payload".to_owned(),
})
}
}
Err(why) => Err(why.to_string()),
Err(why) => Err(CorroError {
error_type: CorroErrorType::NatsError,
message: why.to_string(),
}),
}
}
}
+9
View File
@@ -0,0 +1,9 @@
[package]
name = "serenity-libs"
version = "0.1.0"
edition = "2024"
[dependencies]
async-trait = { version = "0.1.89" }
serenity = { version = "0.12.5", default-features = false }
types = { path = "../../libs/types" }
+49
View File
@@ -0,0 +1,49 @@
use async_trait::async_trait;
use serenity::all::{
CommandInteraction,
Context,
CreateInteractionResponse,
CreateInteractionResponseMessage,
EditInteractionResponse,
};
use types::error::{CorroError, CorroErrorType};
#[async_trait]
pub trait CustomInteraction {
async fn create_text_response(&self, ctx: &Context, content: String) -> Result<(), CorroError>;
async fn edit_text_response(&self, ctx: &Context, content: String) -> Result<(), CorroError>;
}
#[async_trait]
impl CustomInteraction for CommandInteraction {
async fn create_text_response(&self, ctx: &Context, content: String) -> Result<(), CorroError> {
match self
.create_response(
&ctx,
CreateInteractionResponse::Message(
CreateInteractionResponseMessage::new().content(&content),
),
)
.await
{
Ok(_) => Ok(()),
Err(why) => Err(CorroError {
error_type: CorroErrorType::SerenityError,
message: why.to_string(),
}),
}
}
async fn edit_text_response(&self, ctx: &Context, content: String) -> Result<(), CorroError> {
match self
.edit_response(&ctx, EditInteractionResponse::new().content(&content))
.await
{
Ok(_) => Ok(()),
Err(why) => Err(CorroError {
error_type: CorroErrorType::SerenityError,
message: why.to_string(),
}),
}
}
}
+1
View File
@@ -0,0 +1 @@
pub mod functions;
+17
View File
@@ -0,0 +1,17 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub enum CorroErrorType {
JobError,
ParseError,
NatsError,
YtdlpError,
CommandError,
SerenityError,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CorroError {
pub error_type: CorroErrorType,
pub message: String,
}
+2 -2
View File
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use url::Url;
use uuid::Uuid;
use crate::queue::YoutubeSong;
use crate::{error::CorroError, queue::YoutubeSong};
#[derive(Debug, Deserialize, Serialize)]
pub enum JobsResponseMap {
@@ -23,7 +23,7 @@ pub enum JobsMap {
#[derive(Serialize, Deserialize, Debug)]
pub struct JobResponse<T = JobsResponseMap> {
pub content: Option<T>,
pub error: Option<String>,
pub error: Option<CorroError>,
}
#[derive(Serialize, Deserialize, Debug)]
+1
View File
@@ -1,3 +1,4 @@
pub mod error;
pub mod jobs;
pub mod misc;
pub mod queue;