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
// License: see LICENSE file at root directory of `master` branch

//! # Shortcuts for `Value::Null`

use {
    core::convert::TryFrom,

    crate::{Error, Result, Value},
};

/// # Shortcuts for [`Null`](#variant.Null)
impl Value {

    /// # Checks to see if this value is [`Null`][#Null]
    ///
    /// [#Null]: #variant.Null
    pub fn is_null(&self) -> bool {
        match self {
            Value::Null => true,
            _ => false,
        }
    }

    /// # Tries to convert this value into something
    ///
    /// If this is [`Null`][#Null], returns `default`.
    ///
    /// If your default value would be a result of a function call, you should use [`try_into_or_else()`][try_into_or_else()].
    ///
    /// ## Examples
    ///
    /// ```
    /// # #[cfg(feature="std")] {
    /// let mut value = sj::parse_bytes("[null]")?;
    /// assert!(value.take_at(&[0])?.try_into_or(true)?);
    /// # }
    /// # Ok::<_, sj::Error>(())
    /// ```
    ///
    /// [#Null]: #variant.Null
    /// [try_into_or_else()]: #method.try_into_or_else
    pub fn try_into_or<T>(self, default: T) -> Result<T> where T: TryFrom<Self, Error=Error> {
        match self {
            Value::Null => Ok(default),
            _ => T::try_from(self),
        }
    }

    /// # Tries to convert this value into something
    ///
    /// If this is [`Null`][#Null], calls `default()` and returns its result.
    ///
    /// [#Null]: #variant.Null
    pub fn try_into_or_else<T, F>(self, default: F) -> Result<T> where T: TryFrom<Self, Error=Error>, F: FnOnce() -> T {
        match self {
            Value::Null => Ok(default()),
            _ => T::try_from(self),
        }
    }

    /// # Tries to convert a reference of this value into something
    ///
    /// If this is [`Null`][#Null], returns `default`.
    ///
    /// If your default value would be a result of a function call, you should use [`try_ref_into_or_else()`][try_ref_into_or_else()].
    ///
    /// ## Examples
    ///
    /// ```
    /// # #[cfg(feature="std")] {
    /// let value = sj::parse_bytes("[null]")?;
    /// assert_eq!(value.at(&[0])?.try_ref_into_or(0)?, 0);
    /// # }
    /// # Ok::<_, sj::Error>(())
    /// ```
    ///
    /// [#Null]: #variant.Null
    /// [try_ref_into_or_else()]: #method.try_ref_into_or_else
    pub fn try_ref_into_or<'a, T>(&'a self, default: T) -> Result<T> where T: TryFrom<&'a Self, Error=Error> {
        match self {
            Value::Null => Ok(default),
            _ => T::try_from(self),
        }
    }

    /// # Tries to convert a reference of this value into something
    ///
    /// If this is [`Null`][#Null], calls `default()` and returns its result.
    ///
    /// [#Null]: #variant.Null
    pub fn try_ref_into_or_else<'a, T, F>(&'a self, default: F) -> Result<T> where T: TryFrom<&'a Self, Error=Error>, F: FnOnce() -> T {
        match self {
            Value::Null => Ok(default()),
            _ => T::try_from(self),
        }
    }

}