Skip to content

Commit 3dc1841

Browse files
committed
Better ownership & Remove unwrap
1 parent 3a9a098 commit 3dc1841

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

src/index.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use crate::{
66
CBLCollection_GetIndexNames, CBLCollection_CreateArrayIndex, CBLArrayIndexConfiguration,
77
CBLQueryIndex, CBLQueryIndex_Name, CBLQueryIndex_Collection, CBLCollection_GetIndex,
88
},
9-
error::{Result, failure},
10-
slice::{from_str, from_c_str},
9+
error::{Error, Result, failure},
10+
slice::{from_str, from_c_str, Slice},
1111
QueryLanguage, Array,
1212
collection::Collection,
13-
check_error, retain,
13+
check_error, retain, CouchbaseLiteError,
1414
};
1515
use std::ffi::CString;
1616

@@ -44,8 +44,8 @@ impl ValueIndexConfiguration {
4444
#[derive(Debug)]
4545
pub struct ArrayIndexConfiguration {
4646
cbl_ref: CBLArrayIndexConfiguration,
47-
_path: CString,
48-
_expressions: CString,
47+
_path: Slice<CString>,
48+
_expressions: Slice<CString>,
4949
}
5050

5151
impl CblRef for ArrayIndexConfiguration {
@@ -67,19 +67,24 @@ impl ArrayIndexConfiguration {
6767
indexed. The expressions could be specified in a JSON Array or in N1QL syntax
6868
using comma delimiter. If the array specified by the path contains scalar values,
6969
the expressions should be left unset or set to null. */
70-
pub fn new(query_language: QueryLanguage, path: &str, expressions: &str) -> Self {
71-
let path_c = CString::new(path).unwrap();
72-
let expressions_c = CString::new(expressions).unwrap();
70+
pub fn new(query_language: QueryLanguage, path: &str, expressions: &str) -> Result<Self> {
71+
let path_c = CString::new(path)
72+
.map_err(|_| Error::cbl_error(CouchbaseLiteError::InvalidParameter))?;
73+
let expressions_c = CString::new(expressions)
74+
.map_err(|_| Error::cbl_error(CouchbaseLiteError::InvalidParameter))?;
7375

74-
Self {
76+
let path_s = from_c_str(path_c, path.len());
77+
let expressions_s = from_c_str(expressions_c, expressions.len());
78+
79+
Ok(Self {
7580
cbl_ref: CBLArrayIndexConfiguration {
7681
expressionLanguage: query_language as u32,
77-
path: from_c_str(&path_c, path.len()).get_ref(),
78-
expressions: from_c_str(&expressions_c, expressions.len()).get_ref(),
82+
path: path_s.get_ref(),
83+
expressions: expressions_s.get_ref(),
7984
},
80-
_path: path_c,
81-
_expressions: expressions_c,
82-
}
85+
_path: path_s,
86+
_expressions: expressions_s,
87+
})
8388
}
8489
}
8590

src/slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
};
2222

2323
use std::borrow::Cow;
24-
use std::ffi::{CStr, c_void};
24+
use std::ffi::{CStr, CString, c_void};
2525
use std::fmt::{Debug, Formatter};
2626
use std::ptr::{self, drop_in_place};
2727
use std::str;
@@ -98,7 +98,7 @@ pub fn from_str(s: &str) -> Slice<&str> {
9898
)
9999
}
100100

101-
pub fn from_c_str(s: &CStr, len: usize) -> Slice<&CStr> {
101+
pub fn from_c_str(s: CString, len: usize) -> Slice<CString> {
102102
Slice::wrap(
103103
FLSlice {
104104
buf: s.as_ptr().cast::<c_void>(),

tests/query_tests.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ fn array_index() {
282282
default_collection.save_document(&mut doc).unwrap();
283283

284284
// Index with one level of unnest
285-
let index_configuration = ArrayIndexConfiguration::new(QueryLanguage::N1QL, "likes", "");
285+
let index_configuration =
286+
ArrayIndexConfiguration::new(QueryLanguage::N1QL, "likes", "").unwrap();
286287

287288
let result = default_collection
288289
.create_array_index("one_level", &index_configuration)
@@ -307,7 +308,7 @@ fn array_index() {
307308

308309
// Index with two levels of unnest
309310
let index_configuration =
310-
ArrayIndexConfiguration::new(QueryLanguage::N1QL, "contacts[].phones", "type");
311+
ArrayIndexConfiguration::new(QueryLanguage::N1QL, "contacts[].phones", "type").unwrap();
311312

312313
assert!(default_collection
313314
.create_array_index("two_levels", &index_configuration,)
@@ -324,8 +325,6 @@ fn array_index() {
324325
)
325326
.unwrap();
326327

327-
println!("Explain: {}", query.explain().unwrap());
328-
329328
let index = get_index_name_from_explain(&query.explain().unwrap()).unwrap();
330329
assert_eq!(index, "two_levels");
331330

0 commit comments

Comments
 (0)