Debian Codegraph @

third_party/rust/suggest/src/pocket.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 */
5
6 use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef};
7 use rusqlite::{Result as RusqliteResult, ToSql};
8
9 /// Classification of Pocket confidence keywords, where High Confidence
10 /// require an exact match to keyword prefix and suffix.
11 /// While Low Confidence, requires a match on prefix and be a
12 /// substring for the suffix.
13 #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
14 #[repr(u8)]
15 pub(crate) enum KeywordConfidence {
16 Low = 0,
17 High = 1,
18 }
19
20 impl FromSql for KeywordConfidence {
21 fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
22 let v = value.as_i64()?;
23 u8::try_from(v)
24 .ok()
25 .and_then(KeywordConfidence::from_u8)
26 .ok_or_else(|| FromSqlError::OutOfRange(v))
27 }
28 }
29
30 impl KeywordConfidence {
31 #[inline]
32 pub(crate) fn from_u8(v: u8) -> Option<Self> {
33 match v {
34 0 => Some(KeywordConfidence::Low),
35 1 => Some(KeywordConfidence::High),
36 _ => None,
37 }
38 }
39 }
40
41 impl ToSql for KeywordConfidence {
42 fn to_sql(&self) -> RusqliteResult<ToSqlOutput<'_>> {
43 Ok(ToSqlOutput::from(*self as u8))
44 }
45 }
46
47 /// Split the keyword by the first whitespace into the prefix and the suffix.
48 /// Return an empty string as the suffix if there is no whitespace.
49 ///
50 /// # Examples
51 ///
52 /// ```
53 /// # use suggest::pocket::split_keyword;
54 /// assert_eq!(split_keyword("foo"), ("foo", ""));
55 /// assert_eq!(split_keyword("foo bar baz"), ("foo", "bar baz"));
56 /// ```
57 pub fn split_keyword(keyword: &str) -> (&str, &str) {
58 keyword.split_once(' ').unwrap_or((keyword, ""))
59 }