Skip to main content

language_text_analysis/
term.rs

1//! The normalized [`Term`] token type.
2
3use std::fmt;
4
5use serde::{
6    Deserialize,
7    Serialize,
8};
9
10/// A single normalized search term — the output unit of the analysis
11/// pipeline and the key under which the full-text index stores
12/// postings.
13///
14/// A `Term` is always the product of [`Analyzer::analyze`]: normalized
15/// (lowercased, accent-stripped), tokenized, stop-word-filtered, and
16/// optionally stemmed. The same pipeline runs at index time and query
17/// time, so a query `Term` is byte-for-byte comparable with the indexed
18/// `Term` it should match.
19///
20/// Construct one only from already-analyzed text. [`from_normalized`]
21/// exists for the pipeline itself and for callers that have run the
22/// identical analysis; it does **not** normalize on your behalf.
23///
24/// [`Analyzer::analyze`]: crate::Analyzer::analyze
25/// [`from_normalized`]: Term::from_normalized
26#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
27pub struct Term(String);
28
29impl Term {
30    /// Wrap an already-normalized token.
31    ///
32    /// The pipeline guarantees the input has been normalized; this
33    /// constructor performs no normalization itself.
34    #[must_use]
35    pub fn from_normalized(token: impl Into<String>) -> Self {
36        Self(token.into())
37    }
38
39    /// The token text.
40    #[must_use]
41    pub fn as_str(&self) -> &str {
42        &self.0
43    }
44
45    /// Consume the term, yielding its inner string.
46    #[must_use]
47    pub fn into_inner(self) -> String {
48        self.0
49    }
50}
51
52impl fmt::Display for Term {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        f.write_str(&self.0)
55    }
56}