rincon_core/api/types/
mod.rs

1//! Miscellaneous types defined for the rincon core API.
2
3#[cfg(test)]
4mod tests;
5
6use std::fmt;
7
8use serde::de::{Deserialize, Deserializer};
9use serde::ser::{Serialize, Serializer, SerializeSeq};
10use serde_json;
11
12/// The `Url` type used by this crate.
13pub type Url = ::url::Url;
14
15/// An entity represented either by its name only or the whole object.
16///
17/// This type is used mainly in the session API to represent an entity that is
18/// either known by its name or the whole object is loaded from the database
19/// into the session.
20#[derive(Debug)]
21pub enum Entity<T> {
22    /// An entity where currently only the name is known
23    Name(String),
24    /// An entity with the whole content loaded
25    Object(T),
26}
27
28/// Constant instance of the `Empty` struct.
29pub const EMPTY: Empty = Empty {};
30
31/// Represents an 'empty' payload of a method where no type information is
32/// available.
33#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
34pub struct Empty {}
35
36impl Default for Empty {
37    fn default() -> Self {
38        Empty {}
39    }
40}
41
42/// The type for JSON values used by this crate.
43pub type JsonValue = serde_json::Value;
44
45/// A new type for strings that contain JSON.
46#[derive(Debug, Clone, PartialEq, Eq, Hash)]
47pub struct JsonString(String);
48
49impl JsonString {
50    /// Creates a new `JsonString` from the given string.
51    ///
52    /// The given value should convert into a valid JSON string.
53    pub fn new<J>(value: J) -> Self
54        where J: Into<String>
55    {
56        JsonString(value.into())
57    }
58
59    /// Creates a new `JsonString` from the given string value.
60    ///
61    /// The given value should convert into a valid JSON string.
62    ///
63    /// It is not checked whether the given string is a valid JSON string.
64    pub fn from_string_unchecked(value: String) -> Self {
65        JsonString(value)
66    }
67
68    /// Creates a new `JsonString` from the given string slice.
69    ///
70    /// The given value should convert into a valid JSON string.
71    ///
72    /// It is not checked whether the given string slice is a valid JSON string.
73    pub fn from_str_unchecked(value: &str) -> Self {
74        JsonString(value.to_owned())
75    }
76
77    /// Converts this `JsonString` into a std string.
78    pub fn into_string(self) -> String {
79        self.0
80    }
81
82    /// Returns this `JsonString` as a reference to a std str.
83    pub fn as_str(&self) -> &str {
84        &self.0
85    }
86}
87
88impl fmt::Display for JsonString {
89    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90        write!(f, "{}", &self.0)
91    }
92}
93
94impl Serialize for JsonString {
95    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
96        where S: Serializer
97    {
98        use serde::ser::Error;
99        let json_value: JsonValue = serde_json::from_str(&self.0).map_err(S::Error::custom)?;
100        json_value.serialize(serializer)
101    }
102}
103
104impl<'de> Deserialize<'de> for JsonString {
105    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
106        where D: Deserializer<'de>
107    {
108        use serde::de::Error;
109        let json_value = JsonValue::deserialize(deserializer).map_err(D::Error::custom)?;
110        let json_string = serde_json::to_string(&json_value).map_err(D::Error::custom)?;
111        Ok(JsonString(json_string))
112    }
113}
114
115/// Defines the type of the value of a parameter for methods and queries.
116#[derive(Debug, Clone, PartialEq)]
117pub enum Value {
118    /// A string value
119    String(String),
120
121    /// A bool value
122    Bool(bool),
123
124    /// A 64bit float value
125    F64(f64),
126
127    /// A 32bit float value
128    F32(f32),
129
130    /// A isize signed integer value
131    ISize(isize),
132
133    /// A 64bit signed integer value
134    I64(i64),
135
136    /// A 32bit signed integer value
137    I32(i32),
138
139    /// A 16bit signed integer value
140    I16(i16),
141
142    /// A 8bit signed integer value
143    I8(i8),
144
145    /// A usize unsigned integer value
146    USize(usize),
147
148    /// A 64bit unsigned integer value
149    U64(u64),
150
151    /// A 32bit unsigned integer value
152    U32(u32),
153
154    /// A 16bit unsigned integer value
155    U16(u16),
156
157    /// A 8bit unsigned integer value
158    U8(u8),
159
160    /// A vec of string values
161    VecString(Vec<String>),
162
163    /// A vec of bool values
164    VecBool(Vec<bool>),
165
166    /// A vec of 64bit float values
167    VecF64(Vec<f64>),
168
169    /// A vec of 32bit float values
170    VecF32(Vec<f32>),
171
172    /// A vec of isize signed integer values
173    VecISize(Vec<isize>),
174
175    /// A vec of 64bit signed integer values
176    VecI64(Vec<i64>),
177
178    /// A vec of 32bit signed integer values
179    VecI32(Vec<i32>),
180
181    /// A vec of 16bit signed integer values
182    VecI16(Vec<i16>),
183
184    /// A vec of 8bit signed integer values
185    VecI8(Vec<i8>),
186
187    /// A vec of usize unsigned integer values
188    VecUSize(Vec<usize>),
189
190    /// A vec of 64bit unsigned integer values
191    VecU64(Vec<u64>),
192
193    /// A vec of 32bit unsigned integer values
194    VecU32(Vec<u32>),
195
196    /// A vec of 16bit unsigned integer values
197    VecU16(Vec<u16>),
198
199    /// A vec of 8bit unsigned integer values
200    VecU8(Vec<u8>),
201}
202
203impl Value {
204    /// Unwraps the value of the underlying type out of this `Value`.
205    pub fn unwrap<T>(&self) -> &T
206        where T: UnwrapValue
207    {
208        UnwrapValue::unwrap(self)
209    }
210}
211
212impl AsRef<str> for Value {
213    fn as_ref(&self) -> &str {
214        match *self {
215            Value::String(ref value) => value,
216            _ => unreachable!(),
217        }
218    }
219}
220
221/// Defines how to unwrap the value out of the `Value` enum.
222///
223/// This trait should be implemented for all types that can be wrapped inside
224/// the `Value` enum.
225pub trait UnwrapValue {
226    /// Returns the value of the underlying type of this `Value`.
227    fn unwrap(value: &Value) -> &Self;
228}
229
230impl UnwrapValue for String {
231    fn unwrap(value: &Value) -> &Self {
232        match *value {
233            Value::String(ref value) => value,
234            _ => unreachable!(),
235        }
236    }
237}
238
239impl UnwrapValue for bool {
240    fn unwrap(value: &Value) -> &Self {
241        match *value {
242            Value::Bool(ref value) => value,
243            _ => unreachable!(),
244        }
245    }
246}
247
248impl UnwrapValue for f64 {
249    fn unwrap(value: &Value) -> &Self {
250        match *value {
251            Value::F64(ref value) => value,
252            _ => unreachable!(),
253        }
254    }
255}
256
257impl UnwrapValue for f32 {
258    fn unwrap(value: &Value) -> &Self {
259        match *value {
260            Value::F32(ref value) => value,
261            _ => unreachable!(),
262        }
263    }
264}
265
266impl UnwrapValue for isize {
267    fn unwrap(value: &Value) -> &Self {
268        match *value {
269            Value::ISize(ref value) => value,
270            _ => unreachable!(),
271        }
272    }
273}
274
275impl UnwrapValue for i64 {
276    fn unwrap(value: &Value) -> &Self {
277        match *value {
278            Value::I64(ref value) => value,
279            _ => unreachable!(),
280        }
281    }
282}
283
284impl UnwrapValue for i32 {
285    fn unwrap(value: &Value) -> &Self {
286        match *value {
287            Value::I32(ref value) => value,
288            _ => unreachable!(),
289        }
290    }
291}
292
293impl UnwrapValue for i16 {
294    fn unwrap(value: &Value) -> &Self {
295        match *value {
296            Value::I16(ref value) => value,
297            _ => unreachable!(),
298        }
299    }
300}
301
302impl UnwrapValue for i8 {
303    fn unwrap(value: &Value) -> &Self {
304        match *value {
305            Value::I8(ref value) => value,
306            _ => unreachable!(),
307        }
308    }
309}
310
311impl UnwrapValue for usize {
312    fn unwrap(value: &Value) -> &Self {
313        match *value {
314            Value::USize(ref value) => value,
315            _ => unreachable!(),
316        }
317    }
318}
319
320impl UnwrapValue for u64 {
321    fn unwrap(value: &Value) -> &Self {
322        match *value {
323            Value::U64(ref value) => value,
324            _ => unreachable!(),
325        }
326    }
327}
328
329impl UnwrapValue for u32 {
330    fn unwrap(value: &Value) -> &Self {
331        match *value {
332            Value::U32(ref value) => value,
333            _ => unreachable!(),
334        }
335    }
336}
337
338impl UnwrapValue for u16 {
339    fn unwrap(value: &Value) -> &Self {
340        match *value {
341            Value::U16(ref value) => value,
342            _ => unreachable!(),
343        }
344    }
345}
346
347impl UnwrapValue for u8 {
348    fn unwrap(value: &Value) -> &Self {
349        match *value {
350            Value::U8(ref value) => value,
351            _ => unreachable!(),
352        }
353    }
354}
355impl UnwrapValue for Vec<String> {
356    fn unwrap(value: &Value) -> &Self {
357        match *value {
358            Value::VecString(ref value) => value,
359            _ => unreachable!(),
360        }
361    }
362}
363
364impl UnwrapValue for Vec<bool> {
365    fn unwrap(value: &Value) -> &Self {
366        match *value {
367            Value::VecBool(ref value) => value,
368            _ => unreachable!(),
369        }
370    }
371}
372
373impl UnwrapValue for Vec<f64> {
374    fn unwrap(value: &Value) -> &Self {
375        match *value {
376            Value::VecF64(ref value) => value,
377            _ => unreachable!(),
378        }
379    }
380}
381
382impl UnwrapValue for Vec<f32> {
383    fn unwrap(value: &Value) -> &Self {
384        match *value {
385            Value::VecF32(ref value) => value,
386            _ => unreachable!(),
387        }
388    }
389}
390
391impl UnwrapValue for Vec<isize> {
392    fn unwrap(value: &Value) -> &Self {
393        match *value {
394            Value::VecISize(ref value) => value,
395            _ => unreachable!(),
396        }
397    }
398}
399
400impl UnwrapValue for Vec<i64> {
401    fn unwrap(value: &Value) -> &Self {
402        match *value {
403            Value::VecI64(ref value) => value,
404            _ => unreachable!(),
405        }
406    }
407}
408
409impl UnwrapValue for Vec<i32> {
410    fn unwrap(value: &Value) -> &Self {
411        match *value {
412            Value::VecI32(ref value) => value,
413            _ => unreachable!(),
414        }
415    }
416}
417
418impl UnwrapValue for Vec<i16> {
419    fn unwrap(value: &Value) -> &Self {
420        match *value {
421            Value::VecI16(ref value) => value,
422            _ => unreachable!(),
423        }
424    }
425}
426
427impl UnwrapValue for Vec<i8> {
428    fn unwrap(value: &Value) -> &Self {
429        match *value {
430            Value::VecI8(ref value) => value,
431            _ => unreachable!(),
432        }
433    }
434}
435
436impl UnwrapValue for Vec<usize> {
437    fn unwrap(value: &Value) -> &Self {
438        match *value {
439            Value::VecUSize(ref value) => value,
440            _ => unreachable!(),
441        }
442    }
443}
444
445impl UnwrapValue for Vec<u64> {
446    fn unwrap(value: &Value) -> &Self {
447        match *value {
448            Value::VecU64(ref value) => value,
449            _ => unreachable!(),
450        }
451    }
452}
453
454impl UnwrapValue for Vec<u32> {
455    fn unwrap(value: &Value) -> &Self {
456        match *value {
457            Value::VecU32(ref value) => value,
458            _ => unreachable!(),
459        }
460    }
461}
462
463impl UnwrapValue for Vec<u16> {
464    fn unwrap(value: &Value) -> &Self {
465        match *value {
466            Value::VecU16(ref value) => value,
467            _ => unreachable!(),
468        }
469    }
470}
471
472impl UnwrapValue for Vec<u8> {
473    fn unwrap(value: &Value) -> &Self {
474        match *value {
475            Value::VecU8(ref value) => value,
476            _ => unreachable!(),
477        }
478    }
479}
480
481impl<'a> From<&'a str> for Value {
482    fn from(value: &str) -> Self {
483        Value::String(value.to_string())
484    }
485}
486
487impl From<String> for Value {
488    fn from(value: String) -> Self {
489        Value::String(value)
490    }
491}
492
493impl From<bool> for Value {
494    fn from(value: bool) -> Self {
495        Value::Bool(value)
496    }
497}
498
499impl From<f64> for Value {
500    fn from(value: f64) -> Self {
501        Value::F64(value)
502    }
503}
504
505impl From<f32> for Value {
506    fn from(value: f32) -> Self {
507        Value::F32(value)
508    }
509}
510
511impl From<isize> for Value {
512    fn from(value: isize) -> Self {
513        Value::ISize(value)
514    }
515}
516
517impl From<i64> for Value {
518    fn from(value: i64) -> Self {
519        Value::I64(value)
520    }
521}
522
523impl From<i32> for Value {
524    fn from(value: i32) -> Self {
525        Value::I32(value)
526    }
527}
528
529impl From<i16> for Value {
530    fn from(value: i16) -> Self {
531        Value::I16(value)
532    }
533}
534
535impl From<i8> for Value {
536    fn from(value: i8) -> Self {
537        Value::I8(value)
538    }
539}
540
541impl From<usize> for Value {
542    fn from(value: usize) -> Self {
543        Value::USize(value)
544    }
545}
546
547impl From<u64> for Value {
548    fn from(value: u64) -> Self {
549        Value::U64(value)
550    }
551}
552
553impl From<u32> for Value {
554    fn from(value: u32) -> Self {
555        Value::U32(value)
556    }
557}
558
559impl From<u16> for Value {
560    fn from(value: u16) -> Self {
561        Value::U16(value)
562    }
563}
564
565impl From<u8> for Value {
566    fn from(value: u8) -> Self {
567        Value::U8(value)
568    }
569}
570
571impl<'a> From<Vec<&'a str>> for Value {
572    fn from(value: Vec<&str>) -> Self {
573        Value::VecString(value.iter().map(|v| v.to_string()).collect())
574    }
575}
576
577impl From<Vec<String>> for Value {
578    fn from(value: Vec<String>) -> Self {
579        Value::VecString(value)
580    }
581}
582
583impl From<Vec<bool>> for Value {
584    fn from(value: Vec<bool>) -> Self {
585        Value::VecBool(value)
586    }
587}
588
589impl From<Vec<f64>> for Value {
590    fn from(value: Vec<f64>) -> Self {
591        Value::VecF64(value)
592    }
593}
594
595impl From<Vec<f32>> for Value {
596    fn from(value: Vec<f32>) -> Self {
597        Value::VecF32(value)
598    }
599}
600
601impl From<Vec<isize>> for Value {
602    fn from(value: Vec<isize>) -> Self {
603        Value::VecISize(value)
604    }
605}
606
607impl From<Vec<i64>> for Value {
608    fn from(value: Vec<i64>) -> Self {
609        Value::VecI64(value)
610    }
611}
612
613impl From<Vec<i32>> for Value {
614    fn from(value: Vec<i32>) -> Self {
615        Value::VecI32(value)
616    }
617}
618
619impl From<Vec<i16>> for Value {
620    fn from(value: Vec<i16>) -> Self {
621        Value::VecI16(value)
622    }
623}
624
625impl From<Vec<i8>> for Value {
626    fn from(value: Vec<i8>) -> Self {
627        Value::VecI8(value)
628    }
629}
630
631impl From<Vec<usize>> for Value {
632    fn from(value: Vec<usize>) -> Self {
633        Value::VecUSize(value)
634    }
635}
636
637impl From<Vec<u64>> for Value {
638    fn from(value: Vec<u64>) -> Self {
639        Value::VecU64(value)
640    }
641}
642
643impl From<Vec<u32>> for Value {
644    fn from(value: Vec<u32>) -> Self {
645        Value::VecU32(value)
646    }
647}
648
649impl From<Vec<u16>> for Value {
650    fn from(value: Vec<u16>) -> Self {
651        Value::VecU16(value)
652    }
653}
654
655impl From<Vec<u8>> for Value {
656    fn from(value: Vec<u8>) -> Self {
657        Value::VecU8(value)
658    }
659}
660
661impl fmt::Display for Value {
662    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
663        use self::Value::*;
664        match *self {
665            String(ref value) => format_value(value, f),
666            Bool(ref value) => format_value(value, f),
667            F64(ref value) => format_value(value, f),
668            F32(ref value) => format_value(value, f),
669            ISize(ref value) => format_value(value, f),
670            I64(ref value) => format_value(value, f),
671            I32(ref value) => format_value(value, f),
672            I16(ref value) => format_value(value, f),
673            I8(ref value) => format_value(value, f),
674            USize(ref value) => format_value(value, f),
675            U64(ref value) => format_value(value, f),
676            U32(ref value) => format_value(value, f),
677            U16(ref value) => format_value(value, f),
678            U8(ref value) => format_value(value, f),
679            VecString(ref value) => format_value_list(value, f),
680            VecBool(ref value) => format_value_list(value, f),
681            VecF64(ref value) => format_value_list(value, f),
682            VecF32(ref value) => format_value_list(value, f),
683            VecISize(ref value) => format_value_list(value, f),
684            VecI64(ref value) => format_value_list(value, f),
685            VecI32(ref value) => format_value_list(value, f),
686            VecI16(ref value) => format_value_list(value, f),
687            VecI8(ref value) => format_value_list(value, f),
688            VecUSize(ref value) => format_value_list(value, f),
689            VecU64(ref value) => format_value_list(value, f),
690            VecU32(ref value) => format_value_list(value, f),
691            VecU16(ref value) => format_value_list(value, f),
692            VecU8(ref value) => format_value_list(value, f),
693        }
694    }
695}
696
697fn format_value<T>(value: &T, f: &mut fmt::Formatter) -> fmt::Result
698    where T: ToString
699{
700    f.write_str(&value.to_string())
701}
702
703fn format_value_list<T>(values: &[T], f: &mut fmt::Formatter) -> fmt::Result
704    where T: ToString
705{
706    let mut iter = values.iter();
707    f.write_str("[")?;
708    if let Some(first) = iter.next() {
709        f.write_str(&first.to_string())?;
710    }
711    #[cfg_attr(feature = "cargo-clippy", allow(while_let_on_iterator))]
712    while let Some(value) = iter.next() {
713        f.write_str(",")?;
714        format_value(value, f)?;
715    }
716    f.write_str("]")
717}
718
719impl Serialize for Value {
720    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
721        where S: Serializer
722    {
723        use self::Value::*;
724        match *self {
725            String(ref value) => serializer.serialize_str(value),
726            Bool(value) => serializer.serialize_bool(value),
727            F64(value) => serializer.serialize_f64(value),
728            F32(value) => serializer.serialize_f32(value),
729            ISize(value) => serializer.serialize_i64(value as i64),
730            I64(value) => serializer.serialize_i64(value),
731            I32(value) => serializer.serialize_i32(value),
732            I16(value) => serializer.serialize_i16(value),
733            I8(value) => serializer.serialize_i8(value),
734            USize(value) => serializer.serialize_u64(value as u64),
735            U64(value) => serializer.serialize_u64(value),
736            U32(value) => serializer.serialize_u32(value),
737            U16(value) => serializer.serialize_u16(value),
738            U8(value) => serializer.serialize_u8(value),
739            VecString(ref value) => serialize_slice(value, serializer),
740            VecBool(ref value) => serialize_slice(value, serializer),
741            VecF64(ref value) => serialize_slice(value, serializer),
742            VecF32(ref value) => serialize_slice(value, serializer),
743            VecISize(ref value) => serialize_slice(value, serializer),
744            VecI64(ref value) => serialize_slice(value, serializer),
745            VecI32(ref value) => serialize_slice(value, serializer),
746            VecI16(ref value) => serialize_slice(value, serializer),
747            VecI8(ref value) => serialize_slice(value, serializer),
748            VecUSize(ref value) => serialize_slice(value, serializer),
749            VecU64(ref value) => serialize_slice(value, serializer),
750            VecU32(ref value) => serialize_slice(value, serializer),
751            VecU16(ref value) => serialize_slice(value, serializer),
752            VecU8(ref value) => serialize_slice(value, serializer),
753        }
754    }
755}
756
757fn serialize_slice<T, S>(value: &[T], serializer: S) -> Result<S::Ok, S::Error>
758    where T: Serialize, S: Serializer
759{
760    let mut seq = serializer.serialize_seq(Some(value.len()))?;
761    for element in value {
762        seq.serialize_element(element)?;
763    }
764    seq.end()
765}