serde_osc/de/
osc_type.rs

1use serde::de::{Deserializer, Visitor};
2use error::{Error, ResultE};
3
4/// Struct to deserialize a single element from the OSC message sequence.
5/// (e.g. just the address, or the first argument, etc).
6#[derive(Debug)]
7pub enum OscType {
8    I32(i32),
9    F32(f32),
10    String(String),
11    Blob(Vec<u8>),
12}
13
14
15impl<'de> Deserializer<'de> for OscType {
16    type Error = Error;
17    // deserializes a single item from the message, consuming self.
18    fn deserialize_any<V>(self, visitor: V) -> ResultE<V::Value>
19    where
20        V: Visitor<'de>
21    {
22        match self {
23            OscType::I32(i) => visitor.visit_i32(i),
24            OscType::F32(f) => visitor.visit_f32(f),
25            OscType::String(s) => visitor.visit_string(s),
26            // TODO: If the user is attempting to deserialize a Vec<u8>, this
27            //   will error! We should make use of the deserialize_seq function
28            //   in this case.
29            OscType::Blob(b) => visitor.visit_byte_buf(b),
30        }
31    }
32
33    // OSC messages are strongly typed, so we don't make use of any type hints.
34    // More info: https://github.com/serde-rs/serde/blob/b7d6c5d9f7b3085a4d40a446eeb95976d2337e07/serde/src/macros.rs#L106
35    forward_to_deserialize_any! {
36        bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
37        seq bytes byte_buf map unit_struct newtype_struct
38        tuple_struct struct identifier tuple enum ignored_any
39    }
40}
41
42