Skip to content

Commit 60b4cfe

Browse files
authored
Merge pull request #88 from trustgraph/feature/content_not_starts_with
content_not_starts_with
2 parents 4586c2e + 1d5c3c9 commit 60b4cfe

File tree

4 files changed

+316
-8
lines changed

4 files changed

+316
-8
lines changed

zomes/trust_atom/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub fn query(input: QueryInput) -> ExternResult<Vec<TrustAtom>> {
8686
input.target,
8787
input.content_full,
8888
input.content_starts_with,
89+
input.content_not_starts_with,
8990
input.value_starts_with,
9091
)
9192
}
@@ -96,6 +97,7 @@ pub fn query_mine(input: QueryMineInput) -> ExternResult<Vec<TrustAtom>> {
9697
input.target,
9798
input.content_full,
9899
input.content_starts_with,
100+
input.content_not_starts_with,
99101
input.value_starts_with,
100102
)
101103
}

zomes/trust_atom/src/trust_atom.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ pub fn query_mine(
180180
target: Option<AnyLinkableHash>,
181181
content_full: Option<String>,
182182
content_starts_with: Option<String>,
183+
content_not_starts_with: Option<String>,
183184
value_starts_with: Option<String>,
184185
) -> ExternResult<Vec<TrustAtom>> {
185186
let agent_address = AnyLinkableHash::from(agent_info()?.agent_initial_pubkey);
@@ -189,6 +190,7 @@ pub fn query_mine(
189190
target,
190191
content_full,
191192
content_starts_with,
193+
content_not_starts_with,
192194
value_starts_with,
193195
)?;
194196

@@ -204,6 +206,7 @@ pub fn query(
204206
target: Option<AnyLinkableHash>,
205207
content_full: Option<String>,
206208
content_starts_with: Option<String>,
209+
content_not_starts_with: Option<String>,
207210
value_starts_with: Option<String>,
208211
) -> ExternResult<Vec<TrustAtom>> {
209212
let (link_direction, link_base) = match (source, target) {
@@ -217,27 +220,56 @@ pub fn query(
217220
}
218221
};
219222

223+
if let Some(content_not_starts_with_string) = content_not_starts_with {
224+
if content_full.is_some() || content_starts_with.is_some() || value_starts_with.is_some() {
225+
return Err(wasm_error!(
226+
"Passing content_not_starts_with means content_full, content_starts_with, and value_starts_with must all be None"
227+
));
228+
}
229+
let links = get_links(link_base.clone(), LinkTypes::TrustAtom, None)?;
230+
let trust_atoms = convert_links_to_trust_atoms(links, &link_direction, link_base)?;
231+
let filtered_trust_atoms = trust_atoms
232+
.into_iter()
233+
.filter(|trust_atom| {
234+
let content_option = trust_atom.content.clone();
235+
if let Some(content) = content_option {
236+
if content.starts_with(content_not_starts_with_string.as_str()) {
237+
return false;
238+
}
239+
}
240+
true
241+
})
242+
.collect();
243+
return Ok(filtered_trust_atoms);
244+
}
245+
220246
let link_tag = match (content_full, content_starts_with, value_starts_with) {
221247
(Some(_content_full), Some(_content_starts_with), _) => {
222-
return Err(wasm_error!("Only one of `content_full` or `content_starts_with` can be used"))
223-
},
248+
return Err(wasm_error!(
249+
"Only one of `content_full` or `content_starts_with` can be used"
250+
))
251+
}
224252
(_, Some(_content_starts_with), Some(_value_starts_with)) => {
225253
return Err(wasm_error!(
226254
"Cannot use `value_starts_with` and `content_starts_with` arguments together; maybe try `content_full` instead?",
227-
))
228-
},
255+
));
256+
}
229257
(Some(content_full), None, Some(value_starts_with)) => Some(create_link_tag(
230258
&link_direction,
231259
&[Some(content_full), Some(value_starts_with)],
232260
)),
233-
(Some(content_full), None, None) => {
234-
Some(create_link_tag_metal(&link_direction, vec![content_full, UNICODE_NUL_STR.to_string()]))
235-
},
261+
(Some(content_full), None, None) => Some(create_link_tag_metal(
262+
&link_direction,
263+
vec![content_full, UNICODE_NUL_STR.to_string()],
264+
)),
236265
(None, Some(content_starts_with), None) => Some(create_link_tag(
237266
&link_direction,
238267
&[Some(content_starts_with)],
239268
)),
240-
(None, None, Some(value_starts_with)) => Some(create_link_tag(&link_direction, &[Some(value_starts_with)])),
269+
(None, None, Some(value_starts_with)) => Some(create_link_tag(
270+
&link_direction,
271+
&[None, Some(value_starts_with)],
272+
)),
241273
(None, None, None) => None,
242274
};
243275
let links = get_links(link_base.clone(), LinkTypes::TrustAtom, link_tag)?;

0 commit comments

Comments
 (0)