Skip to content

Commit 58c8c54

Browse files
committed
feature: intial support of FLDict
1 parent 98a8445 commit 58c8c54

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

couchbase-lite/src/value.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::{
22
error::Error,
33
ffi::{
4-
FLArray, FLArray_Count, FLArray_Get, FLArray_IsEmpty, FLValue, FLValueType,
5-
FLValue_AsArray, FLValue_AsBool, FLValue_AsDouble, FLValue_AsInt, FLValue_AsString,
6-
FLValue_AsUnsigned, FLValue_GetType, FLValue_IsDouble, FLValue_IsInteger,
7-
FLValue_IsUnsigned,
4+
FLArray, FLArray_Count, FLArray_Get, FLArray_IsEmpty, FLDict, FLDict_Count, FLDict_IsEmpty,
5+
FLValue, FLValueType, FLValue_AsArray, FLValue_AsBool, FLValue_AsDict, FLValue_AsDouble,
6+
FLValue_AsInt, FLValue_AsString, FLValue_AsUnsigned, FLValue_GetType, FLValue_IsDouble,
7+
FLValue_IsInteger, FLValue_IsUnsigned,
88
},
99
fl_slice::fl_slice_to_str_unchecked,
1010
Result,
@@ -20,6 +20,7 @@ pub enum ValueRef<'a> {
2020
Double(f64),
2121
String(&'a str),
2222
Array(ValueRefArray),
23+
Dict(ValueRefDict),
2324
}
2425

2526
impl ValueRef<'_> {
@@ -37,28 +38,29 @@ impl ValueRef<'_> {
3738
}
3839
}
3940

40-
impl<'a> Into<ValueRef<'a>> for FLValue {
41-
fn into(self) -> ValueRef<'a> {
41+
impl<'a> From<FLValue> for ValueRef<'a> {
42+
fn from(value: FLValue) -> ValueRef<'a> {
4243
use FLValueType::*;
43-
match unsafe { FLValue_GetType(self) } {
44+
match unsafe { FLValue_GetType(value) } {
4445
kFLUndefined | kFLNull => ValueRef::Null,
45-
kFLBoolean => ValueRef::Bool(unsafe { FLValue_AsBool(self) }),
46+
kFLBoolean => ValueRef::Bool(unsafe { FLValue_AsBool(value) }),
4647
kFLNumber => {
47-
if unsafe { FLValue_IsInteger(self) } {
48-
ValueRef::SignedInt(unsafe { FLValue_AsInt(self) })
49-
} else if unsafe { FLValue_IsUnsigned(self) } {
50-
ValueRef::UnsignedInt(unsafe { FLValue_AsUnsigned(self) })
48+
if unsafe { FLValue_IsInteger(value) } {
49+
ValueRef::SignedInt(unsafe { FLValue_AsInt(value) })
50+
} else if unsafe { FLValue_IsUnsigned(value) } {
51+
ValueRef::UnsignedInt(unsafe { FLValue_AsUnsigned(value) })
5152
} else {
52-
assert!(unsafe { FLValue_IsDouble(self) });
53-
ValueRef::Double(unsafe { FLValue_AsDouble(self) })
53+
assert!(unsafe { FLValue_IsDouble(value) });
54+
ValueRef::Double(unsafe { FLValue_AsDouble(value) })
5455
}
5556
}
5657
kFLString => {
57-
let s = unsafe { fl_slice_to_str_unchecked(FLValue_AsString(self)) };
58+
let s = unsafe { fl_slice_to_str_unchecked(FLValue_AsString(value)) };
5859
ValueRef::String(s)
5960
}
60-
kFLArray => ValueRef::Array(ValueRefArray(unsafe { FLValue_AsArray(self) })),
61-
kFLData | kFLDict => unimplemented!(),
61+
kFLArray => ValueRef::Array(ValueRefArray(unsafe { FLValue_AsArray(value) })),
62+
kFLDict => ValueRef::Dict(ValueRefDict(unsafe { FLValue_AsDict(value) })),
63+
kFLData => unimplemented!(),
6264
}
6365
}
6466
}
@@ -82,6 +84,19 @@ impl ValueRefArray {
8284
}
8385
}
8486

87+
#[derive(Debug, Clone, Copy)]
88+
#[repr(transparent)]
89+
pub struct ValueRefDict(FLDict);
90+
91+
impl ValueRefDict {
92+
pub fn len(&self) -> u32 {
93+
unsafe { FLDict_Count(self.0) }
94+
}
95+
pub fn is_empty(&self) -> bool {
96+
unsafe { FLDict_IsEmpty(self.0) }
97+
}
98+
}
99+
85100
pub trait FromValueRef<'a>: Sized {
86101
fn column_result(value: ValueRef<'a>) -> Result<Self>;
87102
}

0 commit comments

Comments
 (0)