Skip to main content

mzdata_param/
param_cow.rs

1use std::borrow::Cow;
2use std::str::{self, FromStr};
3
4
5use crate::{
6    AccessionIntCode, CURIE, ControlledVocabulary, Param, ParamLike, ParamValue, ParamValueParseError, Unit, Value, ValueRef
7};
8
9
10/// A statically allocate-able or non-owned data version of [`Param`]
11#[derive(Debug, Clone, Default, PartialEq, Eq)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize))]
13pub struct ParamCow<'a> {
14    pub name: Cow<'a, str>,
15    pub value: ValueRef<'a>,
16    pub accession: Option<AccessionIntCode>,
17    pub controlled_vocabulary: Option<ControlledVocabulary>,
18    pub unit: Unit,
19}
20
21impl<'a> ParamValue for ParamCow<'a> {
22    fn is_empty(&self) -> bool {
23        <ValueRef<'a> as ParamValue>::is_empty(&self.value)
24    }
25
26    fn is_i64(&self) -> bool {
27        <ValueRef<'a> as ParamValue>::is_i64(&self.value)
28    }
29
30    fn is_f64(&self) -> bool {
31        <ValueRef<'a> as ParamValue>::is_f64(&self.value)
32    }
33
34    fn is_buffer(&self) -> bool {
35        <ValueRef<'a> as ParamValue>::is_buffer(&self.value)
36    }
37
38    fn is_str(&self) -> bool {
39        <ValueRef<'a> as ParamValue>::is_str(&self.value)
40    }
41
42    fn to_f64(&self) -> Result<f64, ParamValueParseError> {
43        <ValueRef<'a> as ParamValue>::to_f64(&self.value)
44    }
45
46    fn to_i64(&self) -> Result<i64, ParamValueParseError> {
47        <ValueRef<'a> as ParamValue>::to_i64(&self.value)
48    }
49
50    fn to_str(&self) -> Cow<'_, str> {
51        <ValueRef<'a> as ParamValue>::to_str(&self.value)
52    }
53
54    fn to_buffer(&self) -> Result<Cow<'_, [u8]>, ParamValueParseError> {
55        <ValueRef<'a> as ParamValue>::to_buffer(&self.value)
56    }
57
58    fn parse<T: FromStr>(&self) -> Result<T, T::Err> {
59        <ValueRef<'a> as ParamValue>::parse(&self.value)
60    }
61
62    fn as_bytes(&self) -> Cow<'_, [u8]> {
63        <ValueRef<'a> as ParamValue>::as_bytes(&self.value)
64    }
65
66    fn as_ref(&self) -> ValueRef<'_> {
67        <ValueRef<'a> as ParamValue>::as_ref(&self.value)
68    }
69
70    fn data_len(&self) -> usize {
71        <ValueRef<'a> as ParamValue>::data_len(&self.value)
72    }
73
74    fn is_boolean(&self) -> bool {
75        <ValueRef<'a> as ParamValue>::is_boolean(&self.value)
76    }
77
78    fn to_bool(&self) -> Result<bool, ParamValueParseError> {
79        <ValueRef<'a> as ParamValue>::to_bool(&self.value)
80    }
81
82    fn is_list(&self) -> bool {
83        <ValueRef<'a> as ParamValue>::is_list(&self.value)
84    }
85
86    fn as_slice(&self) -> Cow<'_, [Value]> {
87        <ValueRef<'a> as ParamValue>::as_slice(&self.value)
88    }
89}
90
91impl ParamCow<'static> {
92    pub const fn const_new(
93        name: &'static str,
94        value: ValueRef<'static>,
95        accession: Option<AccessionIntCode>,
96        controlled_vocabulary: Option<ControlledVocabulary>,
97        unit: Unit,
98    ) -> Self {
99        Self {
100            name: Cow::Borrowed(name),
101            value,
102            accession,
103            controlled_vocabulary,
104            unit,
105        }
106    }
107}
108
109impl<'a> ParamCow<'a> {
110    pub fn new(
111        name: Cow<'a, str>,
112        value: ValueRef<'a>,
113        accession: Option<AccessionIntCode>,
114        controlled_vocabulary: Option<ControlledVocabulary>,
115        unit: Unit,
116    ) -> Self {
117        Self {
118            name,
119            value,
120            accession,
121            controlled_vocabulary,
122            unit,
123        }
124    }
125
126    pub fn parse<T: str::FromStr>(&self) -> Result<T, T::Err> {
127        self.value.parse::<T>()
128    }
129
130    pub const fn is_controlled(&self) -> bool {
131        self.accession.is_some()
132    }
133
134    pub const fn curie(&self) -> Option<CURIE> {
135        match (self.controlled_vocabulary, self.accession) {
136            (Some(cv), Some(acc)) => Some(CURIE::new(cv, acc)),
137            _ => None,
138        }
139    }
140}
141
142impl<'a> ParamLike for ParamCow<'a> {
143    fn name(&self) -> &str {
144        &self.name
145    }
146
147    fn value(&self) -> ValueRef<'a> {
148        self.value.clone()
149    }
150
151    fn accession(&self) -> Option<AccessionIntCode> {
152        self.accession
153    }
154
155    fn controlled_vocabulary(&self) -> Option<ControlledVocabulary> {
156        self.controlled_vocabulary
157    }
158
159    fn unit(&self) -> Unit {
160        self.unit
161    }
162}
163
164impl<'a> From<ParamCow<'a>> for Param {
165    fn from(value: ParamCow<'a>) -> Self {
166        Param {
167            name: value.name.into_owned(),
168            value: value.value.into(),
169            accession: value.accession,
170            controlled_vocabulary: value.controlled_vocabulary,
171            unit: value.unit,
172        }
173    }
174}
175
176impl PartialEq<CURIE> for ParamCow<'_> {
177    fn eq(&self, other: &CURIE) -> bool {
178        other.eq(self)
179    }
180}
181
182impl<'a> AsRef<ValueRef<'a>> for ParamCow<'a> {
183    fn as_ref(&self) -> &ValueRef<'a> {
184        &self.value
185    }
186}