search_filter/
search_filter.rs1use opendaq::{Component, Instance, Interface, SearchFilter, TagsPrivate};
20
21fn show<T: Interface>(label: &str, components: Vec<T>) -> opendaq::Result<()> {
23 let ids = components
24 .iter()
25 .map(|c| c.to_base_object().cast::<Component>()?.local_id())
26 .collect::<opendaq::Result<Vec<_>>>()?;
27 let listing = if ids.is_empty() {
28 "(none)".to_string()
29 } else {
30 ids.join(", ")
31 };
32 println!("{label}\n => {listing}\n");
33 Ok(())
34}
35
36fn main() -> opendaq::Result<()> {
37 let instance = Instance::new()?;
38 instance.add_device("daqref://device0")?;
39 let root = instance.root_device()?.expect("root device");
40
41 let everything = SearchFilter::recursive(&SearchFilter::any()?)?;
44 for channel in root.channels_with(Some(&everything))? {
45 let tags = channel.tags()?.expect("tags").cast::<TagsPrivate>()?;
46 tags.add("analog")?;
47 if channel.local_id()? == "RefCh0" {
48 tags.add("primary")?;
49 }
50 }
51
52 show(
53 "channels, no filter (immediate children only)",
54 root.channels()?,
55 )?;
56
57 show(
58 "channels, recursive(any)",
59 root.channels_with(Some(&SearchFilter::recursive(&SearchFilter::any()?)?))?,
60 )?;
61
62 show(
63 "channels, recursive(id = RefCh1)",
64 root.channels_with(Some(&SearchFilter::recursive(&SearchFilter::local_id(
65 "RefCh1",
66 )?)?))?,
67 )?;
68
69 let ai0_or_ai1 = SearchFilter::or(
70 &SearchFilter::local_id("AI0")?,
71 &SearchFilter::local_id("AI1")?,
72 )?;
73 show(
74 "signals, recursive(id = AI0 OR id = AI1)",
75 root.signals_with(Some(&SearchFilter::recursive(&ai0_or_ai1)?))?,
76 )?;
77
78 let not_ref_ch1 = SearchFilter::not(&SearchFilter::local_id("RefCh1")?)?;
79 show(
80 "channels, recursive(NOT id = RefCh1)",
81 root.channels_with(Some(&SearchFilter::recursive(¬_ref_ch1)?))?,
82 )?;
83
84 let analog_and_not_ref_ch1 = SearchFilter::and(
85 &SearchFilter::required_tags(vec!["analog"])?,
86 &SearchFilter::not(&SearchFilter::local_id("RefCh1")?)?,
87 )?;
88 show(
89 "channels, recursive(tag = analog AND NOT id = RefCh1)",
90 root.channels_with(Some(&SearchFilter::recursive(&analog_and_not_ref_ch1)?))?,
91 )?;
92
93 Ok(())
94}