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