mzdata-param 0.66.4

A library to read mass spectrometry data formats and a data model for mass spectra
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
use std::borrow::Cow;
use std::fmt::Display;
use std::hash::Hash;
use std::str::{self, FromStr};
use std::{io, mem};

use thiserror::Error;

use crate::ValueRef;


macro_rules! param_value_int {
    ($val:ty) => {
        impl From<$val> for Value {
            fn from(value: $val) -> Self {
                Self::Int(value as i64)
            }
        }

        impl From<&$val> for Value {
            fn from(value: &$val) -> Self {
                Self::Int(*value as i64)
            }
        }

        impl From<Option<$val>> for Value {
            fn from(value: Option<$val>) -> Self {
                if let Some(v) = value {
                    Self::Int(v as i64)
                } else {
                    Self::Empty
                }
            }
        }
    };
}

macro_rules! param_value_float {
    ($val:ty) => {
        impl From<$val> for Value {
            fn from(value: $val) -> Self {
                Self::Float(value as f64)
            }
        }

        impl From<&$val> for Value {
            fn from(value: &$val) -> Self {
                Self::Float(*value as f64)
            }
        }

        impl From<Option<$val>> for Value {
            fn from(value: Option<$val>) -> Self {
                if let Some(v) = value {
                    Self::Float(v as f64)
                } else {
                    Self::Empty
                }
            }
        }
    };
}
/// An owned parameter value that may be a string, a number, or empty. It is intended to
/// be paired with the [`ParamValue`] trait.
///
/// The borrowed equivalent of this type is [`ValueRef`].
#[derive(Debug, Clone, PartialEq, PartialOrd, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Value {
    /// A text value of arbitrary length
    String(String),
    /// A floating point number
    Float(f64),
    /// A integral number
    Int(i64),
    /// Arbitrary binary data
    Buffer(Box<[u8]>),
    /// true/false value
    Boolean(bool),
    /// No value specified
    #[default]
    Empty,
    /// A list of heterogenous [`Value`]
    List(Box<[Value]>),
}

impl Eq for Value {}

impl From<String> for Value {
    fn from(value: String) -> Self {
        Value::new(value)
    }
}

impl From<&str> for Value {
    fn from(value: &str) -> Self {
        Value::wrap(value)
    }
}

impl From<Cow<'_, str>> for Value {
    fn from(value: Cow<'_, str>) -> Self {
        Value::wrap(&value)
    }
}

/// Access a parameter's value, with specific coercion rules
/// and eager type conversion.
pub trait ParamValue {
    /// Check if the value is empty
    fn is_empty(&self) -> bool;

    /// Check if the value is an integer
    fn is_i64(&self) -> bool;

    /// Check if the value is a floating point
    /// number explicitly. An integral number might
    /// still be usable as a floating point number
    fn is_f64(&self) -> bool;

    /// Check if the value is an arbitrary buffer
    fn is_buffer(&self) -> bool;

    /// Check if the value is stored as an explicit string.
    /// All variants can be coerced to a string.
    fn is_str(&self) -> bool;

    /// Check if the value is of either numeric type.
    fn is_numeric(&self) -> bool {
        self.is_i64() | self.is_f64()
    }

    /// Check if the value is a list
    fn is_list(&self) -> bool;

    /// Check if the value is a boolean
    fn is_boolean(&self) -> bool;

    /// Get the value as an `f64`, if possible
    fn to_f64(&self) -> Result<f64, ParamValueParseError>;

    /// Get the value as an `f32`, if possible
    fn to_f32(&self) -> Result<f32, ParamValueParseError> {
        let v = self.to_f64()?;
        Ok(v as f32)
    }

    /// Get the value as a `bool`, if possible
    fn to_bool(&self) -> Result<bool, ParamValueParseError>;

    /// Get the value as an `i64`, if possible
    fn to_i64(&self) -> Result<i64, ParamValueParseError>;

    /// Get the value as an `i32`, if possible
    fn to_i32(&self) -> Result<i32, ParamValueParseError> {
        let v = self.to_i64()?;
        Ok(v as i32)
    }

    /// Get the value as an `u64`, if possible
    fn to_u64(&self) -> Result<u64, ParamValueParseError> {
        let v = self.to_i64()?;
        Ok(v as u64)
    }

    /// Get the value as a string
    fn to_str(&self) -> Cow<'_, str>;

    /// Get the value as a string, possibly borrowed
    fn as_str(&self) -> Cow<'_, str> {
        self.to_str()
    }

    /// Get the value as a byte buffer, if possible.
    ///
    /// The intent here is distinct from [`ParamValue::as_bytes`]. The byte buffer
    /// represents the byte representation of the native value, while
    /// [`ParamValue::as_bytes`] is a byte string of the string representation.
    fn to_buffer(&self) -> Result<Cow<'_, [u8]>, ParamValueParseError>;

    /// Get the value as a slice
    fn as_slice(&self) -> Cow<'_, [Value]>;

    /// Convert the value's string representation to `T` if possible
    fn parse<T: FromStr>(&self) -> Result<T, T::Err>;

    /// Convert the value to a byte string, the bytes
    /// of the string representation.
    fn as_bytes(&self) -> Cow<'_, [u8]>;

    /// Get a reference to the stored value
    fn as_ref(&self) -> crate::ValueRef<'_>;

    /// Get the size of the stored data type
    fn data_len(&self) -> usize;
}

/// Errors that might occur while trying to convert to a particular value type
/// from whatever type might be stored in a [`ParamValue`]-like object.
#[derive(Debug, Clone, Error, PartialEq)]
pub enum ParamValueParseError {
    /// The value could not be interpreted as a floating point number.
    #[error("Failed to extract a float from {0:?}")]
    FailedToExtractFloat(Option<String>),
    /// The value could not be interpreted as an integer.
    #[error("Failed to extract a int from {0:?}")]
    FailedToExtractInt(Option<String>),
    /// The value could not be interpreted as a string.
    #[error("Failed to extract a string")]
    FailedToExtractString,
    /// The value could not be interpreted as a byte buffer.
    #[error("Failed to extract a buffer")]
    FailedToExtractBuffer,
}

/// A [`Value`] can be parsed from a string infallibly, even though an error type
/// is given, but unless one of the numerical parsers succeeds, the stored value will
/// just be a string.
impl FromStr for Value {
    type Err = ParamValueParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.is_empty() {
            return Ok(Self::Empty);
        }
        if let Ok(value) = s.parse::<i64>() {
            Ok(Self::Int(value))
        } else if let Ok(value) = s.parse::<f64>() {
            Ok(Self::Float(value))
        } else if let Ok(value) = s.parse::<bool>() {
            Ok(Self::Boolean(value))
        } else {
            Ok(Self::String(s.to_string()))
        }
    }
}

impl Display for Value {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Value::String(v) => f.write_str(v),
            Value::Float(v) => v.fmt(f),
            Value::Int(v) => v.fmt(f),
            Value::Buffer(v) => f.write_str(&String::from_utf8_lossy(v)),
            Value::Empty => f.write_str(""),
            Value::Boolean(v) => v.fmt(f),
            Value::List(v) => {
                f.write_str("[ ")?;
                if let Some(vi) = v.first() {
                    vi.fmt(f)?;
                }
                for vi in v.iter().skip(1) {
                    f.write_str(", ")?;
                    vi.fmt(f)?;
                }
                f.write_str(" ]")
            }
        }
    }
}

impl From<ParamValueParseError> for io::Error {
    fn from(value: ParamValueParseError) -> Self {
        Self::new(io::ErrorKind::InvalidData, value)
    }
}

impl Value {
    /// Convert a string value into a precise value type by trying
    /// successive types to parse, defaulting to storing the string
    /// as-is.
    ///
    /// This takes ownership of the string. To coerce from a borrowed
    /// string see [`Value::wrap`].
    pub fn new(s: String) -> Self {
        if s.is_empty() {
            Self::Empty
        } else if let Ok(value) = s.parse::<i64>() {
            Self::Int(value)
        } else if let Ok(value) = s.parse::<f64>() {
            Self::Float(value)
        } else if let Ok(value) = s.parse::<bool>() {
            Self::Boolean(value)
        } else {
            Self::String(s)
        }
    }

    /// Convert a borrowed string value into a precise value type by trying
    /// successive types to parse, defaulting to storing the string
    /// as-is.
    ///
    /// This only makes a copy of the string if it cannot be parsed into a
    /// numeric type.
    pub fn wrap(s: &str) -> Self {
        if s.is_empty() {
            Self::Empty
        } else if let Ok(value) = s.parse::<i64>() {
            Self::Int(value)
        } else if let Ok(value) = s.parse::<f64>() {
            Self::Float(value)
        } else {
            Self::String(s.to_string())
        }
    }

    /// See [`ParamValue::is_empty`].
    pub fn is_empty(&self) -> bool {
        matches!(self, Self::Empty)
    }

    /// See [`ParamValue::is_i64`].
    pub fn is_i64(&self) -> bool {
        matches!(self, Self::Int(_))
    }

    /// See [`ParamValue::is_f64`].
    pub fn is_f64(&self) -> bool {
        matches!(self, Self::Float(_))
    }

    /// See [`ParamValue::is_buffer`].
    pub fn is_buffer(&self) -> bool {
        matches!(self, Self::Buffer(_))
    }

    /// See [`ParamValue::is_str`].
    pub fn is_str(&self) -> bool {
        matches!(self, Self::String(_))
    }

    /// See [`ParamValue::is_list`].
    pub fn is_list(&self) -> bool {
        matches!(self, Self::List(_))
    }

    /// Store the value as a floating point number
    pub fn coerce_f64(&mut self) -> Result<(), ParamValueParseError> {
        let value = self.to_f64()?;
        *self = Self::Float(value);
        Ok(())
    }

    /// Store the value as an integer
    pub fn coerce_i64(&mut self) -> Result<(), ParamValueParseError> {
        let value = self.to_i64()?;
        *self = Self::Int(value);
        Ok(())
    }

    /// Store the value as a string
    pub fn coerce_str(&mut self) -> Result<(), ParamValueParseError> {
        let value = self.to_string();
        *self = Self::String(value);
        Ok(())
    }

    /// Discard the value, leaving this value [`Value::Empty`]
    pub fn coerce_empty(&mut self) {
        *self = Self::Empty;
    }

    /// Store the value as a byte buffer
    pub fn coerce_buffer(&mut self) -> Result<(), ParamValueParseError> {
        let buffer = self.to_buffer()?;
        *self = Self::Buffer(buffer.into());
        Ok(())
    }

    /// Store the value as a boolean
    pub fn coerce_bool(&mut self) -> Result<(), ParamValueParseError> {
        let value = self.to_bool()?;
        *self = Self::Boolean(value);
        Ok(())
    }

    /// Store the value as a list
    pub fn coerce_list(&mut self) -> Result<(), ParamValueParseError> {
        if !self.is_list() {
            let mut tmp = Self::Empty;
            core::mem::swap(&mut tmp, self);
            *self = Self::List([tmp].into());
        }
        Ok(())
    }

    /// Convert the value to a text string and then try to parse it into `T`.
    /// If the type is one of the common numeric types, prefer one of the provided
    /// methods with a `to_` prefix as they avoid the string conversions.
    pub fn parse<T: FromStr>(&self) -> Result<T, T::Err> {
        match self {
            Value::String(s) => s.parse(),
            Value::Float(v) => v.to_string().parse(),
            Value::Int(i) => i.to_string().parse(),
            Value::Buffer(b) => String::from_utf8_lossy(b).parse(),
            Value::Empty => "".parse(),
            Value::Boolean(b) => b.to_string().parse(),
            Value::List(_) => self.to_string().parse(),
        }
    }

    /// See [`ParamValue::to_bool`].
    pub fn to_bool(&self) -> Result<bool, ParamValueParseError> {
        if let Self::Boolean(val) = self {
            Ok(*val)
        } else if self.is_numeric() {
            Ok(self.to_i64()? != 0)
        } else if let Self::Empty = self {
            Ok(false)
        } else if let Ok(v) = self.parse() {
            Ok(v)
        } else {
            Err(ParamValueParseError::FailedToExtractInt(Some(
                self.to_string(),
            )))
        }
    }

    /// See [`ParamValue::to_f64`].
    pub fn to_f64(&self) -> Result<f64, ParamValueParseError> {
        if let Self::Float(val) = self {
            return Ok(*val);
        } else if let Self::Int(val) = self {
            return Ok(*val as f64);
        } else if let Self::String(val) = self {
            if let Ok(v) = val.parse() {
                return Ok(v);
            }
        }
        Err(ParamValueParseError::FailedToExtractFloat(Some(
            self.to_string(),
        )))
    }

    /// See [`ParamValue::to_i64`].
    pub fn to_i64(&self) -> Result<i64, ParamValueParseError> {
        if let Self::Int(val) = self {
            return Ok(*val);
        } else if let Self::Float(val) = self {
            return Ok(*val as i64);
        } else if let Self::String(val) = self {
            if let Ok(v) = val.parse() {
                return Ok(v);
            }
        }
        Err(ParamValueParseError::FailedToExtractInt(Some(
            self.to_string(),
        )))
    }

    /// See [`ParamValue::to_str`].
    pub fn to_str(&self) -> Cow<'_, str> {
        if let Self::String(val) = self {
            Cow::Borrowed(val)
        } else {
            Cow::Owned(self.to_string())
        }
    }

    /// See [`ParamValue::to_buffer`].
    pub fn to_buffer(&self) -> Result<Cow<'_, [u8]>, ParamValueParseError> {
        if let Self::Buffer(val) = self {
            Ok(Cow::Borrowed(val))
        } else if let Self::String(val) = self {
            Ok(Cow::Borrowed(val.as_bytes()))
        } else {
            Err(ParamValueParseError::FailedToExtractBuffer)
        }
    }

    /// See [`ParamValue::as_ref`].
    pub fn as_ref(&self) -> ValueRef<'_> {
        self.into()
    }

    /// View this [`Value`] as a [`slice`]
    pub fn as_slice(&self) -> &[Self] {
        if let Self::List(val) = self {
            val.as_ref()
        } else {
            core::slice::from_ref(self)
        }
    }
}

impl ParamValue for Value {
    fn is_empty(&self) -> bool {
        self.is_empty()
    }

    fn is_i64(&self) -> bool {
        self.is_i64()
    }

    fn is_f64(&self) -> bool {
        self.is_f64()
    }

    fn is_buffer(&self) -> bool {
        self.is_buffer()
    }

    fn is_str(&self) -> bool {
        self.is_str()
    }

    fn to_f64(&self) -> Result<f64, ParamValueParseError> {
        self.to_f64()
    }

    fn to_i64(&self) -> Result<i64, ParamValueParseError> {
        self.to_i64()
    }

    fn to_str(&self) -> Cow<'_, str> {
        self.to_str()
    }

    fn to_buffer(&self) -> Result<Cow<'_, [u8]>, ParamValueParseError> {
        self.to_buffer()
    }

    fn parse<T: FromStr>(&self) -> Result<T, T::Err> {
        self.parse()
    }

    fn as_bytes(&self) -> Cow<'_, [u8]> {
        match self {
            Self::String(v) => Cow::Borrowed(v.as_bytes()),
            Self::Buffer(v) => Cow::Borrowed(v.as_ref()),
            Self::Float(v) => Cow::Owned(v.to_string().into_bytes()),
            Self::Int(v) => Cow::Owned(v.to_string().into_bytes()),
            Self::Empty => Cow::Borrowed(b""),
            Self::Boolean(v) => Cow::Owned(v.to_string().into_bytes()),
            Self::List(_) => Cow::Owned(self.to_string().into_bytes()),
        }
    }

    fn as_ref(&self) -> ValueRef<'_> {
        self.into()
    }

    fn data_len(&self) -> usize {
        match self {
            Self::String(v) => v.len(),
            Self::Buffer(v) => v.len(),
            Self::Float(_) => 8,
            Self::Int(_) => 8,
            Self::Empty => 0,
            Self::Boolean(_) => mem::size_of::<bool>(),
            Self::List(v) => v.iter().map(|vi| vi.data_len()).sum(),
        }
    }

    fn is_boolean(&self) -> bool {
        matches!(self, Self::Boolean(_))
    }

    fn to_bool(&self) -> Result<bool, ParamValueParseError> {
        self.to_bool()
    }

    fn is_list(&self) -> bool {
        self.is_list()
    }

    fn as_slice(&self) -> Cow<'_, [Value]> {
        Cow::Borrowed(self.as_slice())
    }
}

impl<U: Into<Value>> FromIterator<U> for Value {
    fn from_iter<T: IntoIterator<Item = U>>(iter: T) -> Self {
        let values: Box<[Value]> = iter.into_iter().map(|v| v.into()).collect();
        Self::List(values)
    }
}

impl From<Box<[Value]>> for Value {
    fn from(value: Box<[Value]>) -> Self {
        Self::List(value)
    }
}

impl From<Vec<Value>> for Value {
    fn from(value: Vec<Value>) -> Self {
        Self::List(value.into_boxed_slice())
    }
}

impl PartialEq<String> for Value {
    fn eq(&self, other: &String) -> bool {
        self.as_str() == other.as_str()
    }
}

impl PartialEq<str> for Value {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<&str> for Value {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

impl PartialEq<i64> for Value {
    fn eq(&self, other: &i64) -> bool {
        if let Self::Int(val) = self {
            val == other
        } else {
            false
        }
    }
}

impl PartialEq<f64> for Value {
    fn eq(&self, other: &f64) -> bool {
        if let Self::Float(val) = self {
            val == other
        } else {
            false
        }
    }
}

impl PartialEq<bool> for Value {
    fn eq(&self, other: &bool) -> bool {
        if let Self::Boolean(val) = self {
            val == other
        } else {
            false
        }
    }
}

impl Hash for Value {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        core::mem::discriminant(self).hash(state);
        match self {
            Self::String(s) => s.hash(state),
            Self::Float(v) => v.to_bits().hash(state),
            Self::Int(v) => (*v).hash(state),
            Self::Buffer(v) => v.hash(state),
            Self::Empty => 0u8.hash(state),
            Self::Boolean(v) => v.hash(state),
            Self::List(v) => {
                v.iter().for_each(|vi| vi.hash(state));
            }
        }
    }
}

param_value_int!(i8);
param_value_int!(i16);
param_value_int!(i32);
param_value_int!(i64);

param_value_int!(u8);
param_value_int!(u16);
param_value_int!(u32);
param_value_int!(u64);
param_value_int!(usize);

param_value_float!(f32);
param_value_float!(f64);


/// When the `serde` feature is enabled, [`mzdata_param::Value`] can be converted from
/// `serde_json::Value` without going through the generic deserialization process.
#[cfg(feature = "serde")]
impl From<Value> for serde_json::Value {
    fn from(value: Value) -> Self {
        match value {
            Value::Boolean(val) => serde_json::Value::Bool(val),
            Value::Float(val) => {
                serde_json::Value::Number(serde_json::Number::from_f64(val).unwrap())
            }
            Value::Int(val) => {
                serde_json::Value::Number(serde_json::Number::from_i128(val as i128).unwrap())
            }
            Value::String(val) => serde_json::Value::String(val),
            Value::Buffer(val) => serde_json::to_value(&val).unwrap(),
            Value::Empty => serde_json::Value::Null,
            Value::List(val) => {
                let mut ve = Vec::new();
                for vi in val {
                    ve.push(vi.into());
                }
                serde_json::Value::Array(ve)
            }
        }
    }
}