assemble_core/task/work_handler/serializer/
ron.rs1use crate::prelude::ProjectError;
2use crate::project::ProjectResult;
3use ron_serde::ser::PrettyConfig;
4use ron_serde::Value;
5use serde::de::DeserializeOwned;
6use serde::{Deserialize, Serialize};
7use std::io::Write;
8use std::str::FromStr;
9
10pub fn from_str<T: DeserializeOwned>(string: impl AsRef<str>) -> ProjectResult<T> {
12 ron_serde::from_str(string.as_ref()).map_err(|e| ProjectError::custom(e).into())
13}
14
15pub fn to_string<S: Serialize>(value: &S) -> ProjectResult<String> {
17 ron_serde::ser::to_string_pretty(value, PrettyConfig::new().struct_names(true))
18 .map_err(|e| ProjectError::custom(e).into())
19}
20
21pub fn to_writer<W: Write, S: Serialize>(writer: W, value: &S) -> ProjectResult<()> {
23 ron_serde::ser::to_writer_pretty(writer, value, PrettyConfig::new().struct_names(true))
24 .map_err(|e| ProjectError::custom(e).into())
25}
26
27#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
29#[serde(transparent)]
30pub struct Serializable(Value);
31
32pub type SerializableError = ron_serde::Error;
33
34impl Serializable {
35 pub fn new<T: Serialize>(value: T) -> ProjectResult<Self> {
37 Value::from_str(&to_string(&value)?)
38 .map(Serializable)
39 .map_err(|e| ProjectError::custom(e).into())
40 }
41
42 pub fn deserialize<T: DeserializeOwned>(&self) -> ProjectResult<T> {
44 self.0
45 .clone()
46 .into_rust()
47 .map_err(|e| ProjectError::custom(e).into())
48 }
49}