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
use crate::{Date, DateTime, Time};
use raystack_core::{
    Coord, Hayson, Marker, Na, Number, Ref, RemoveMarker, Symbol, Uri, Xstr,
};
use serde_json::Value;

/// An extension trait for the `serde_json::Value` enum,
/// containing helper functions which make it easier to
/// parse specific Haystack types from the underlying Hayson encoding
/// (a JSON value in a specific format, see https://github.com/j2inn/hayson).
pub trait ValueExt {
    /// Convert the JSON value to a Haystack Coord.
    fn as_hs_coord(&self) -> Option<Coord>;
    /// Convert the JSON value to a Haystack Date.
    fn as_hs_date(&self) -> Option<Date>;
    /// Convert the JSON value to a Haystack DateTime.
    fn as_hs_date_time(&self) -> Option<DateTime>;
    /// Convert the JSON value to a Haystack Marker.
    fn as_hs_marker(&self) -> Option<Marker>;
    /// Convert the JSON value to a Haystack NA.
    fn as_hs_na(&self) -> Option<Na>;
    /// Convert the JSON value to a Haystack Number.
    fn as_hs_number(&self) -> Option<Number>;
    /// Convert the JSON value to a Haystack Ref.
    fn as_hs_ref(&self) -> Option<Ref>;
    /// Convert the JSON value to a Haystack Remove Marker.
    fn as_hs_remove_marker(&self) -> Option<RemoveMarker>;
    /// Parse the JSON value as a Haystack Str.
    fn as_hs_str(&self) -> Option<&str>;
    /// Convert the JSON value to a Haystack Symbol.
    fn as_hs_symbol(&self) -> Option<Symbol>;
    /// Convert the JSON value to a Haystack Time.
    fn as_hs_time(&self) -> Option<Time>;
    /// Returns the Haystack URI value as a Haystack Uri.
    fn as_hs_uri(&self) -> Option<Uri>;
    /// Return the Haystack XStr value as a Haystack Xstr.
    fn as_hs_xstr(&self) -> Option<Xstr>;
    /// Returns true if the JSON value represents a Haystack
    /// Coord.
    fn is_hs_coord(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// Date.
    fn is_hs_date(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// DateTime.
    fn is_hs_date_time(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// marker.
    fn is_hs_marker(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// NA value.
    fn is_hs_na(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// Number.
    fn is_hs_number(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// Ref.
    fn is_hs_ref(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// remove marker.
    fn is_hs_remove_marker(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// Str.
    fn is_hs_str(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// Symbol.
    fn is_hs_symbol(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// Time.
    fn is_hs_time(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// URI.
    fn is_hs_uri(&self) -> bool;
    /// Returns true if the JSON value represents a Haystack
    /// XStr.
    fn is_hs_xstr(&self) -> bool;
}

impl ValueExt for Value {
    fn as_hs_coord(&self) -> Option<Coord> {
        Coord::from_hayson(self).ok()
    }

    fn as_hs_date(&self) -> Option<Date> {
        Date::from_hayson(self).ok()
    }

    fn as_hs_date_time(&self) -> Option<DateTime> {
        DateTime::from_hayson(self).ok()
    }

    fn as_hs_marker(&self) -> Option<Marker> {
        Marker::from_hayson(self).ok()
    }

    fn as_hs_na(&self) -> Option<Na> {
        Na::from_hayson(self).ok()
    }

    fn as_hs_number(&self) -> Option<Number> {
        Number::from_hayson(self).ok()
    }

    fn as_hs_ref(&self) -> Option<Ref> {
        Ref::from_hayson(self).ok()
    }

    fn as_hs_remove_marker(&self) -> Option<RemoveMarker> {
        RemoveMarker::from_hayson(self).ok()
    }

    fn as_hs_str(&self) -> Option<&str> {
        self.as_str()
    }

    fn as_hs_symbol(&self) -> Option<Symbol> {
        Symbol::from_hayson(self).ok()
    }

    fn as_hs_time(&self) -> Option<Time> {
        Time::from_hayson(self).ok()
    }

    fn as_hs_uri(&self) -> Option<Uri> {
        Uri::from_hayson(self).ok()
    }

    fn as_hs_xstr(&self) -> Option<Xstr> {
        Xstr::from_hayson(self).ok()
    }

    fn is_hs_coord(&self) -> bool {
        self.as_hs_coord().is_some()
    }

    fn is_hs_date(&self) -> bool {
        self.as_hs_date().is_some()
    }

    fn is_hs_date_time(&self) -> bool {
        self.as_hs_date_time().is_some()
    }

    fn is_hs_marker(&self) -> bool {
        self.as_hs_marker().is_some()
    }

    fn is_hs_na(&self) -> bool {
        self.as_hs_na().is_some()
    }

    fn is_hs_number(&self) -> bool {
        self.as_hs_number().is_some()
    }

    fn is_hs_ref(&self) -> bool {
        self.as_hs_ref().is_some()
    }

    fn is_hs_remove_marker(&self) -> bool {
        self.as_hs_remove_marker().is_some()
    }

    fn is_hs_str(&self) -> bool {
        self.as_hs_str().is_some()
    }

    fn is_hs_symbol(&self) -> bool {
        self.as_hs_symbol().is_some()
    }

    fn is_hs_time(&self) -> bool {
        self.as_hs_time().is_some()
    }

    fn is_hs_uri(&self) -> bool {
        self.as_hs_uri().is_some()
    }

    fn is_hs_xstr(&self) -> bool {
        self.as_hs_xstr().is_some()
    }
}