carbone_sdk_rust/
types.rs

1use serde::{Deserialize, Serialize};
2
3use crate::errors::CarboneError;
4
5pub type Result<T> = std::result::Result<T, CarboneError>;
6
7// pub type Result<(T,U)> = std::result::Result<(T,U), CarboneError>;
8
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct ApiJsonToken(String);
11
12impl ApiJsonToken {
13    pub fn new(s: String) -> Result<Self> {
14        if s.len() >= 300 {
15            Ok(ApiJsonToken(s))
16        } else {
17            Err(CarboneError::Error("wrong token length".to_string()))
18        }
19    }
20
21    pub fn as_str(&self) -> &str {
22        &self.0
23    }
24}
25
26#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
27pub struct ApiVersion(String);
28
29impl ApiVersion {
30    pub fn new(s: String) -> Result<Self> {
31        if !s.is_empty() {
32            Ok(ApiVersion(s))
33        } else {
34            Err(CarboneError::Error("wrong token length".to_string()))
35        }
36    }
37
38    pub fn as_str(&self) -> &str {
39        &self.0
40    }
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
44pub struct Id(String);
45
46impl Id {
47    pub fn new<T: Into<String>>(id: T, type_name: &str) -> Result<Self> {
48        let id = id.into();
49        if !id.is_empty() {
50            Ok(Id(id))
51        } else {
52            Err(CarboneError::EmptyString(type_name.to_string()))
53        }
54    }
55    pub fn as_str(&self) -> &str {
56        &self.0
57    }
58}
59
60impl AsRef<str> for Id {
61    fn as_ref(&self) -> &str {
62        &self.0
63    }
64}
65
66#[derive(Debug, Clone, PartialEq, Eq)]
67pub struct JsonData {
68    render_options: String,
69}
70
71impl JsonData {
72    /// Create a new render_options.
73    ///
74    ///
75    /// # Example
76    ///
77    /// ```no_run
78    /// use std::env;
79    ///
80    /// use carbone_sdk_rust::types::JsonData;
81    /// use carbone_sdk_rust::errors::CarboneError;
82    ///
83    /// fn main() -> Result<(), CarboneError> {
84    ///
85    ///  let render_options_value = r#"
86    ///        "data" : {
87    ///            "firstname" : "John",
88    ///            "lastname" : "Wick"
89    ///        },
90    ///        "convertTo" : "odt"
91    ///    "#;
92    ///
93    ///    let render_options = JsonData::new(render_options_value.to_string())?;
94    ///
95    ///    assert_eq!(render_options.as_str(), render_options_value);
96    ///
97    ///     Ok(())
98    /// }
99    /// ```
100    pub fn new(s: String) -> Result<Self> {
101        if s.is_empty() {
102            return Err(CarboneError::EmptyString("json_data".to_string()));
103        }
104        Ok(Self { render_options: s })
105    }
106
107    pub fn as_str(&self) -> &str {
108        &self.render_options
109    }
110}