Skip to main content

json_tools_rs/
types.rs

1use std::borrow::Cow;
2
3use crate::error::JsonToolsError;
4
5/// Input type for JSON flattening operations with Cow optimization
6#[derive(Debug, Clone)]
7#[repr(u8)] // OPTIMIZATION: Smaller discriminant for better cache locality
8pub enum JsonInput<'a> {
9    /// Single JSON string with Cow for efficient memory usage
10    Single(Cow<'a, str>),
11    /// Multiple JSON strings (borrowing)
12    Multiple(&'a [&'a str]),
13    /// Multiple JSON strings (owned)
14    MultipleOwned(Vec<Cow<'a, str>>),
15}
16
17impl<'a> From<&'a str> for JsonInput<'a> {
18    fn from(json: &'a str) -> Self {
19        JsonInput::Single(Cow::Borrowed(json))
20    }
21}
22
23impl<'a> From<&'a String> for JsonInput<'a> {
24    fn from(json: &'a String) -> Self {
25        JsonInput::Single(Cow::Borrowed(json.as_str()))
26    }
27}
28
29impl<'a> From<&'a [&'a str]> for JsonInput<'a> {
30    fn from(json_list: &'a [&'a str]) -> Self {
31        JsonInput::Multiple(json_list)
32    }
33}
34
35impl<'a> From<Vec<&'a str>> for JsonInput<'a> {
36    fn from(json_list: Vec<&'a str>) -> Self {
37        JsonInput::MultipleOwned(json_list.into_iter().map(Cow::Borrowed).collect())
38    }
39}
40
41impl<'a> From<Vec<String>> for JsonInput<'a> {
42    fn from(json_list: Vec<String>) -> Self {
43        JsonInput::MultipleOwned(json_list.into_iter().map(Cow::Owned).collect())
44    }
45}
46
47impl<'a> From<&'a [String]> for JsonInput<'a> {
48    fn from(json_list: &'a [String]) -> Self {
49        JsonInput::MultipleOwned(
50            json_list
51                .iter()
52                .map(|s| Cow::Borrowed(s.as_str()))
53                .collect(),
54        )
55    }
56}
57
58/// Output type for JSON flattening operations
59#[derive(Debug, Clone)]
60#[repr(u8)] // OPTIMIZATION: Smaller discriminant for better cache locality
61pub enum JsonOutput {
62    /// Single flattened JSON string
63    Single(String),
64    /// Multiple flattened JSON strings
65    Multiple(Vec<String>),
66}
67
68impl JsonOutput {
69    /// Extract single result, panicking if multiple results.
70    ///
71    /// # Panics
72    /// Panics if this is a `Multiple` variant. Use [`try_into_single`](Self::try_into_single)
73    /// for a non-panicking alternative.
74    #[must_use]
75    #[deprecated(
76        since = "0.10.0",
77        note = "Use try_into_single() instead to avoid panics"
78    )]
79    pub fn into_single(self) -> String {
80        match self {
81            JsonOutput::Single(result) => result,
82            JsonOutput::Multiple(_) => panic!("Expected single result but got multiple"),
83        }
84    }
85
86    /// Extract multiple results, panicking if single result.
87    ///
88    /// # Panics
89    /// Panics if this is a `Single` variant. Use [`try_into_multiple`](Self::try_into_multiple)
90    /// for a non-panicking alternative.
91    #[must_use]
92    #[deprecated(
93        since = "0.10.0",
94        note = "Use try_into_multiple() instead to avoid panics"
95    )]
96    pub fn into_multiple(self) -> Vec<String> {
97        match self {
98            JsonOutput::Single(_) => panic!("Expected multiple results but got single"),
99            JsonOutput::Multiple(results) => results,
100        }
101    }
102
103    /// Try to extract single result, returning an error if multiple results.
104    pub fn try_into_single(self) -> Result<String, JsonToolsError> {
105        match self {
106            JsonOutput::Single(result) => Ok(result),
107            JsonOutput::Multiple(_) => Err(JsonToolsError::input_validation_error(
108                "Expected single result but got multiple",
109            )),
110        }
111    }
112
113    /// Try to extract multiple results, returning an error if single result.
114    pub fn try_into_multiple(self) -> Result<Vec<String>, JsonToolsError> {
115        match self {
116            JsonOutput::Single(_) => Err(JsonToolsError::input_validation_error(
117                "Expected multiple results but got single",
118            )),
119            JsonOutput::Multiple(results) => Ok(results),
120        }
121    }
122
123    /// Get results as vector (single result becomes vec with one element)
124    #[must_use]
125    pub fn into_vec(self) -> Vec<String> {
126        match self {
127            JsonOutput::Single(result) => vec![result],
128            JsonOutput::Multiple(results) => results,
129        }
130    }
131}