summaryrefslogtreecommitdiffstats
path: root/third_party/rust/suggest/src/pocket.rs
blob: cf7070c62a0f32419ecfe0120659346dde7a4db0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef};
use rusqlite::{Result as RusqliteResult, ToSql};

/// Classification of Pocket confidence keywords, where High Confidence
/// require an exact match to keyword prefix and suffix.
/// While Low Confidence, requires a match on prefix and be a
/// substring for the suffix.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[repr(u8)]
pub enum KeywordConfidence {
    Low = 0,
    High = 1,
}

impl FromSql for KeywordConfidence {
    fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
        let v = value.as_i64()?;
        u8::try_from(v)
            .ok()
            .and_then(KeywordConfidence::from_u8)
            .ok_or_else(|| FromSqlError::OutOfRange(v))
    }
}

impl KeywordConfidence {
    #[inline]
    pub(crate) fn from_u8(v: u8) -> Option<Self> {
        match v {
            0 => Some(KeywordConfidence::Low),
            1 => Some(KeywordConfidence::High),
            _ => None,
        }
    }
}

impl ToSql for KeywordConfidence {
    fn to_sql(&self) -> RusqliteResult<ToSqlOutput<'_>> {
        Ok(ToSqlOutput::from(*self as u8))
    }
}

/// Split the keyword by the first whitespace into the prefix and the suffix.
/// Return an empty string as the suffix if there is no whitespace.
///
/// # Examples
///
/// ```
/// # use suggest::pocket::split_keyword;
/// assert_eq!(split_keyword("foo"), ("foo", ""));
/// assert_eq!(split_keyword("foo bar baz"), ("foo", "bar baz"));
/// ```
pub fn split_keyword(keyword: &str) -> (&str, &str) {
    keyword.split_once(' ').unwrap_or((keyword, ""))
}