zirv_queue/utils/
serialize.rs

1use crate::traits::Queueable;
2
3/// Serializes a `Queueable` trait object into a JSON string.
4///
5/// This function relies on the [`typetag::serde`] machinery to serialize the
6/// concrete type behind the `dyn Queueable`. The resulting JSON will include type
7/// information so it can be deserialized back into the correct struct.
8///
9/// # Parameters
10///
11/// - `job_ref`: A reference to any type that implements the [`Queueable`] trait.
12///
13/// # Returns
14///
15/// - `Ok(String)` containing the JSON-encoded representation of the job.
16/// - `Err(serde_json::Error)` if serialization fails (for instance, due to invalid data).
17///
18/// # Example
19///
20/// ```rust,ignore
21/// use crate::traits::Queueable;
22/// // Suppose `my_job` is some concrete type that implements `Queueable`.
23/// let my_job: &dyn Queueable = &SomeConcreteJob { /* fields */ };
24///
25/// // Serialize the job into a JSON string
26/// match serialize(my_job) {
27///     Ok(json_str) => println!("Serialized job: {}", json_str),
28///     Err(e) => eprintln!("Failed to serialize job: {}", e),
29/// }
30/// ```
31pub fn serialize(job_ref: &dyn Queueable) -> Result<String, serde_json::Error> {
32    serde_json::to_string(job_ref)
33}