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
use errors::{Error, Result};
use std::fmt;
use {Loc, RpEnumOrdinal, RpNumber};
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type", content = "value", rename_all = "snake_case")]
pub enum RpValue {
String(String),
Number(RpNumber),
Identifier(String),
Array(Vec<Loc<RpValue>>),
}
impl RpValue {
pub fn as_str(&self) -> Result<&str> {
use self::RpValue::*;
match *self {
String(ref string) => Ok(string),
_ => Err("not a string".into()),
}
}
pub fn as_identifier(&self) -> Result<&str> {
use self::RpValue::*;
match *self {
Identifier(ref identifier) => Ok(identifier),
_ => Err("expected identifier".into()),
}
}
pub fn as_string(&self) -> Result<&str> {
use self::RpValue::*;
match *self {
String(ref string) => Ok(string),
_ => Err("expected string".into()),
}
}
pub fn is_string(&self) -> bool {
use self::RpValue::*;
match *self {
String(_) => true,
_ => false,
}
}
pub fn into_ordinal(self) -> Result<RpEnumOrdinal> {
let ordinal = match self {
RpValue::String(value) => RpEnumOrdinal::String(value),
_ => return Err(Error::new("Not an ordinal")),
};
Ok(ordinal)
}
}
impl fmt::Display for RpValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
RpValue::String(ref string) => write!(f, "\"{}\"", string),
RpValue::Number(ref number) => number.fmt(f),
RpValue::Identifier(ref identifier) => identifier.fmt(f),
RpValue::Array(ref values) => {
write!(f, "[")?;
let mut it = values.iter().peekable();
while let Some(v) = it.next() {
v.fmt(f)?;
if it.peek().is_some() {
write!(f, ", ")?;
}
}
write!(f, "]")?;
Ok(())
}
}
}
}