1use std::borrow::Cow;
2
3use crate::error::JsonToolsError;
4
5#[derive(Debug, Clone)]
7#[repr(u8)] pub enum JsonInput<'a> {
9 Single(Cow<'a, str>),
11 Multiple(&'a [&'a str]),
13 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#[derive(Debug, Clone)]
60#[repr(u8)] pub enum JsonOutput {
62 Single(String),
64 Multiple(Vec<String>),
66}
67
68impl JsonOutput {
69 #[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 #[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 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 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 #[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}