crag/
query.rs

1/// Encodes custom search parameters to a query string
2#[derive(Debug, Default)]
3#[cfg_attr(feature = "cli", derive(clap::Args))]
4pub struct Query {
5    /// Query string that will be transmitted to the engine
6    #[cfg_attr(feature = "cli", arg(skip))]
7    pub query: String,
8
9    /// The number of responses to return when searching
10    #[cfg_attr(feature = "cli", arg(short, long, default_value = "10"))]
11    pub count: u32,
12
13    /// Set the locale when requesting the api response
14    #[cfg_attr(feature = "cli", arg(long, default_value = "en_us"))]
15    pub locale: String,
16}
17
18/// Implement From<Into<String>> for Query
19///
20/// Uses a default count of 10 and en_us locale
21impl<T: Into<String>> From<T> for Query {
22    fn from(query: T) -> Self {
23        Self {
24            query: query.into(),
25            count: 10,
26            locale: "en_us".to_string(),
27        }
28    }
29}
30
31impl Query {
32    /// Build a new Query from a query string, count, and locale
33    pub fn new<Q: Into<String>, L: Into<String>>(query: Q, count: u32, locale: L) -> Self {
34        Self {
35            query: query.into(),
36            count,
37            locale: locale.into(),
38        }
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn query_new() {
48        // new from a basic string
49        let query = Query::new("test", 10, "en_us");
50        assert_eq!(query.query, "test");
51    }
52
53    #[test]
54    fn from_string() {
55        // from a basic string
56        let query: Query = "test".into();
57        assert_eq!(query.query, "test");
58    }
59}