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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use crate::{
error::{Error, Result},
ffi::{
FLArray, FLArray_Count, FLArray_Get, FLArray_IsEmpty, FLDict, FLDict_Count, FLDict_Get,
FLDict_IsEmpty, FLSlice, FLValue, FLValueType, FLValue_AsArray, FLValue_AsBool,
FLValue_AsDict, FLValue_AsDouble, FLValue_AsInt, FLValue_AsString, FLValue_AsUnsigned,
FLValue_GetType, FLValue_IsDouble, FLValue_IsInteger, FLValue_IsUnsigned,
},
};
use std::convert::TryFrom;
#[derive(Debug, Clone, Copy)]
pub enum ValueRef<'a> {
Null,
Bool(bool),
SignedInt(i64),
UnsignedInt(u64),
Double(f64),
String(&'a str),
Array(ValueRefArray),
Dict(ValueRefDict),
}
impl ValueRef<'_> {
#[inline]
pub fn as_str(&self) -> Result<&str> {
FromValueRef::column_result(*self)
}
#[inline]
pub fn as_u64(&self) -> Result<u64> {
FromValueRef::column_result(*self)
}
#[inline]
pub fn is_null(&self) -> bool {
matches!(self, ValueRef::Null)
}
pub(crate) unsafe fn new(value: FLValue) -> Self {
use FLValueType::*;
match FLValue_GetType(value) {
kFLUndefined | kFLNull => ValueRef::Null,
kFLBoolean => ValueRef::Bool(FLValue_AsBool(value)),
kFLNumber => {
if FLValue_IsInteger(value) {
ValueRef::SignedInt(FLValue_AsInt(value))
} else if FLValue_IsUnsigned(value) {
ValueRef::UnsignedInt(FLValue_AsUnsigned(value))
} else {
assert!(FLValue_IsDouble(value));
ValueRef::Double(FLValue_AsDouble(value))
}
}
kFLString => {
let s: &str = FLValue_AsString(value).try_into().expect("not valid utf-8");
ValueRef::String(s)
}
kFLArray => ValueRef::Array(ValueRefArray(FLValue_AsArray(value))),
kFLDict => ValueRef::Dict(ValueRefDict(FLValue_AsDict(value))),
kFLData => unimplemented!(),
}
}
}
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub struct ValueRefArray(FLArray);
impl ValueRefArray {
pub fn len(&self) -> u32 {
unsafe { FLArray_Count(self.0) }
}
pub fn is_empty(&self) -> bool {
unsafe { FLArray_IsEmpty(self.0) }
}
pub(crate) unsafe fn get_raw(&self, idx: u32) -> FLValue {
FLArray_Get(self.0, idx)
}
pub fn get(&self, idx: u32) -> ValueRef {
unsafe { ValueRef::new(self.get_raw(idx)) }
}
}
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub struct ValueRefDict(FLDict);
impl ValueRefDict {
pub fn len(&self) -> u32 {
unsafe { FLDict_Count(self.0) }
}
pub fn is_empty(&self) -> bool {
unsafe { FLDict_IsEmpty(self.0) }
}
pub(crate) unsafe fn get_raw(&self, key: FLSlice) -> FLValue {
FLDict_Get(self.0, key)
}
pub fn get(&self, key: FLSlice) -> ValueRef {
unsafe { ValueRef::new(self.get_raw(key)) }
}
}
pub trait FromValueRef<'a>: Sized {
fn column_result(val: ValueRef<'a>) -> Result<Self>;
}
impl<'a> FromValueRef<'a> for &'a str {
fn column_result(val: ValueRef<'a>) -> Result<Self> {
if let ValueRef::String(x) = val {
Ok(x)
} else {
Err(Error::LogicError(format!(
"Wrong ValueRef type, expect String, got {val:?}"
)))
}
}
}
impl<'a> FromValueRef<'a> for u16 {
fn column_result(val: ValueRef<'a>) -> Result<Self> {
match val {
ValueRef::SignedInt(x) => u16::try_from(x).map_err(|err| {
Error::LogicError(format!(
"ValueRef(Signed) {x} to u16 conversation error: {err}"
))
}),
ValueRef::UnsignedInt(x) => u16::try_from(x).map_err(|err| {
Error::LogicError(format!(
"ValueRef(Unsigned) {x} to u16 conversation error: {err}"
))
}),
_ => Err(Error::LogicError(format!(
"Wrong ValueRef type, expect SignedInt|UnsignedInt (u16) got {val:?}"
))),
}
}
}
impl<'a> FromValueRef<'a> for u32 {
fn column_result(val: ValueRef<'a>) -> Result<Self> {
match val {
ValueRef::SignedInt(x) => u32::try_from(x).map_err(|err| {
Error::LogicError(format!(
"ValueRef(Signed) {x} to u32 conversation error: {err}"
))
}),
ValueRef::UnsignedInt(x) => u32::try_from(x).map_err(|err| {
Error::LogicError(format!(
"ValueRef(Unsigned) {x} to u32 conversation error: {err}"
))
}),
_ => Err(Error::LogicError(format!(
"Wrong ValueRef type, expect SignedInt|UnsignedInt (u32) got {val:?}"
))),
}
}
}
impl<'a> FromValueRef<'a> for u64 {
fn column_result(val: ValueRef<'a>) -> Result<Self> {
match val {
ValueRef::SignedInt(x) => u64::try_from(x).map_err(|err| {
Error::LogicError(format!(
"ValueRef(Signed) {x} to u32 conversation error: {err}"
))
}),
ValueRef::UnsignedInt(x) => Ok(x),
_ => Err(Error::LogicError(format!(
"Wrong ValueRef type, expect SignedInt|UnsignedInt (u64) got {val:?}"
))),
}
}
}
impl<'a> FromValueRef<'a> for i64 {
fn column_result(val: ValueRef<'a>) -> Result<Self> {
match val {
ValueRef::SignedInt(x) => Ok(x),
ValueRef::UnsignedInt(x) => i64::try_from(x).map_err(|err| {
Error::LogicError(format!(
"ValueRef (UnsignedInt) to i64 conversation failed: {err}"
))
}),
_ => Err(Error::LogicError(format!(
"Wrong ValueRef type, expect SignedInt|UnsignedInt (i64) got {val:?}"
))),
}
}
}
impl<'a> FromValueRef<'a> for usize {
fn column_result(val: ValueRef<'a>) -> Result<Self> {
match val {
ValueRef::SignedInt(x) => usize::try_from(x).map_err(|err| {
Error::LogicError(format!(
"ValueRef(Signed) {x} to usize conversation error: {err}"
))
}),
ValueRef::UnsignedInt(x) => usize::try_from(x).map_err(|err| {
Error::LogicError(format!(
"ValueRef(Unsigned) {x} to usize conversation error: {err}"
))
}),
_ => Err(Error::LogicError(format!(
"Wrong ValueRef type, expect SignedInt|UnsignedInt (usize) got {val:?}"
))),
}
}
}