sonic_channel/channels/search.rs
1use super::{ChannelMode, SonicChannel, SonicStream};
2use crate::commands::*;
3use crate::result::Result;
4use std::net::ToSocketAddrs;
5
6/// The Sonic Channel Search mode is used for querying the search index.
7/// Once in this mode, you cannot switch to other modes or gain access
8/// to commands from other modes.
9///
10/// ### Available commands
11///
12/// In this mode you can use `query`, `suggest`, `ping` and `quit` commands.
13///
14/// **Note:** This mode requires enabling the `search` feature.
15#[derive(Debug)]
16pub struct SearchChannel(SonicStream);
17
18impl SonicChannel for SearchChannel {
19 type Channel = SearchChannel;
20
21 fn stream(&self) -> &SonicStream {
22 &self.0
23 }
24
25 fn start<A, S>(addr: A, password: S) -> Result<Self::Channel>
26 where
27 A: ToSocketAddrs,
28 S: ToString,
29 {
30 SonicStream::connect_with_start(ChannelMode::Search, addr, password).map(Self)
31 }
32}
33
34impl SearchChannel {
35 init_command!(
36 /// Stop connection.
37 ///
38 /// ```rust,no_run
39 /// # use sonic_channel::*;
40 /// # fn main() -> result::Result<()> {
41 /// let channel = SearchChannel::start(
42 /// "localhost:1491",
43 /// "SecretPassword",
44 /// )?;
45 ///
46 /// channel.quit()?;
47 /// # Ok(())
48 /// # }
49 use QuitCommand for fn quit();
50 );
51
52 init_command!(
53 /// Ping server.
54 ///
55 /// ```rust,no_run
56 /// # use sonic_channel::*;
57 /// # fn main() -> result::Result<()> {
58 /// let channel = SearchChannel::start(
59 /// "localhost:1491",
60 /// "SecretPassword",
61 /// )?;
62 ///
63 /// channel.ping()?;
64 /// # Ok(())
65 /// # }
66 use PingCommand for fn ping();
67 );
68}
69
70impl SearchChannel {
71 init_command!(
72 /// Query objects in database.
73 ///
74 /// Note: This method requires enabling the `search` feature and start
75 /// connection in Search mode.
76 ///
77 /// ```rust,no_run
78 /// # use sonic_channel::*;
79 /// # fn main() -> result::Result<()> {
80 /// let search_channel = SearchChannel::start(
81 /// "localhost:1491",
82 /// "SecretPassword",
83 /// )?;
84 ///
85 /// let result = search_channel.query(QueryRequest::new(
86 /// Dest::col("search"),
87 /// "Beef",
88 /// ))?;
89 /// dbg!(result);
90 ///
91 /// let result = search_channel.query(
92 /// QueryRequest::new(Dest::col("search"), "Beef").limit(10)
93 /// )?;
94 /// dbg!(result);
95 /// # Ok(())
96 /// # }
97 /// ```
98 use QueryCommand for fn query(
99 req: QueryRequest,
100 );
101 );
102
103 init_command!(
104 /// Suggest auto-completes words.
105 ///
106 /// Note: This method requires enabling the `search` feature and start
107 /// connection in Search mode.
108 ///
109 /// ```rust,no_run
110 /// # use sonic_channel::*;
111 /// # fn main() -> result::Result<()> {
112 /// let search_channel = SearchChannel::start(
113 /// "localhost:1491",
114 /// "SecretPassword",
115 /// )?;
116 ///
117 /// let result = search_channel.suggest(
118 /// SuggestRequest::new(Dest::col("search"), "Beef")
119 /// )?;
120 /// dbg!(result);
121 ///
122 /// let result = search_channel.suggest(
123 /// SuggestRequest::new(Dest::col("search"), "Beef").limit(2)
124 /// )?;
125 /// dbg!(result);
126 /// # Ok(())
127 /// # }
128 /// ```
129 use SuggestCommand for fn suggest(
130 req: SuggestRequest,
131 );
132 );
133
134 init_command!(
135 /// Enumerates all words in an index.
136 ///
137 /// Note: This method requires enabling the `search` feature and start
138 /// connection in Search mode.
139 ///
140 /// ```rust,no_run
141 /// # use sonic_channel::*;
142 /// # fn main() -> result::Result<()> {
143 /// let search_channel = SearchChannel::start(
144 /// "localhost:1491",
145 /// "SecretPassword",
146 /// )?;
147 ///
148 /// let result = search_channel.list(
149 /// ListRequest::new(Dest::col("search"))
150 /// )?;
151 /// dbg!(result);
152 ///
153 /// let result = search_channel.list(
154 /// ListRequest::new(Dest::col("search")).limit(2)
155 /// )?;
156 /// dbg!(result);
157 /// # Ok(())
158 /// # }
159 /// ```
160 use ListCommand for fn list(
161 req: ListRequest,
162 );
163 );
164}