zirv_queue/utils/deserialize.rs
1use crate::traits::Queueable;
2
3/// Attempts to deserialize a JSON string into a boxed [`Queueable`] trait object.
4///
5/// This function uses Serde (with [`typetag::serde`]) to deserialize the given
6/// `payload` string into a `Box<dyn Queueable>`, allowing dynamic dispatch to specific
7/// job implementations at runtime.
8///
9/// # Parameters
10///
11/// - `payload`: A JSON-encoded string representing a serialized [`Queueable`].
12///
13/// # Returns
14///
15/// - `Ok(Box<dyn Queueable>)` if the deserialization is successful.
16/// - `Err(serde_json::Error)` if deserialization fails (e.g., invalid JSON format or missing fields).
17///
18/// # Example
19///
20/// ```rust,ignore
21/// use crate::traits::Queueable;
22///
23/// // Assume `serialized_job` is a valid JSON representation of a type implementing `Queueable`.
24/// let serialized_job = r#"{
25/// "type": "MyConcreteJob",
26/// "some_field": "some_value"
27/// }"#;
28///
29/// match deserialize(serialized_job) {
30/// Ok(job) => {
31/// // Now you can call job.handle(), etc.
32/// println!("Job deserialized successfully!");
33/// }
34/// Err(e) => eprintln!("Failed to deserialize job: {}", e),
35/// }
36/// ```
37pub fn deserialize(payload: &str) -> Result<Box<dyn Queueable>, serde_json::Error> {
38 serde_json::from_str::<Box<dyn Queueable>>(payload)
39}