cleaner job send function

This commit is contained in:
2026-03-14 00:07:06 +01:00
parent 8ed01f2049
commit b63982714b
13 changed files with 186 additions and 89 deletions
+3
View File
@@ -5,3 +5,6 @@ edition = "2024"
[dependencies]
async-nats = { version = "0.46.0" }
async-trait = { version = "0.1.89" }
postcard = { version = "1.1.3", features = ["use-std"] }
types = { path = "../../libs/types" }
+43 -1
View File
@@ -1 +1,43 @@
pub fn()
use async_nats::Client;
use async_trait::async_trait;
use postcard::{from_bytes, to_stdvec};
use types::jobs::{JobResponse, JobsMap, JobsResponseMap};
#[async_trait]
pub trait JobClient {
async fn send_job(&self, job: JobsMap) -> Result<JobsResponseMap, String>;
}
#[async_trait]
impl JobClient for Client {
async fn send_job(&self, job: JobsMap) -> Result<JobsResponseMap, String> {
let subject = match &job {
JobsMap::Download(_job) => "download",
JobsMap::Play(_job) => "play",
JobsMap::Search(_job) => "search",
};
match self
.request(
format!("corro-dj.{subject}"),
to_stdvec(&job).unwrap().into(),
)
.await
{
Ok(response) => {
println!("{:?}", &response.payload);
let parsed: JobResponse = from_bytes(&response.payload).unwrap();
if let Some(error) = parsed.error {
Err(error)
} else if let Some(content) = parsed.content {
Ok(content)
} else {
Err("Unknown content".to_string())
}
}
Err(why) => Err(why.to_string()),
}
}
}