summaryrefslogtreecommitdiffstats
path: root/vendor/elasticlunr-rs/src/pipeline.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/elasticlunr-rs/src/pipeline.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/elasticlunr-rs/src/pipeline.rs')
-rw-r--r--vendor/elasticlunr-rs/src/pipeline.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/vendor/elasticlunr-rs/src/pipeline.rs b/vendor/elasticlunr-rs/src/pipeline.rs
new file mode 100644
index 000000000..a20de3f11
--- /dev/null
+++ b/vendor/elasticlunr-rs/src/pipeline.rs
@@ -0,0 +1,65 @@
+//! Defines the pipeline which processes text for inclusion in the index. Most users do not need
+//! to use this module directly.
+
+use serde::ser::{Serialize, SerializeSeq, Serializer};
+
+pub trait PipelineFn {
+ fn name(&self) -> String;
+
+ fn filter(&self, token: String) -> Option<String>;
+}
+
+#[derive(Clone)]
+pub struct FnWrapper(pub String, pub fn(String) -> Option<String>);
+
+impl PipelineFn for FnWrapper {
+ fn name(&self) -> String {
+ self.0.clone()
+ }
+
+ fn filter(&self, token: String) -> Option<String> {
+ (self.1)(token)
+ }
+}
+
+/// A sequence of `PipelineFn`s which are run on tokens to prepare them for searching.
+#[derive(Deserialize)]
+pub struct Pipeline {
+ #[serde(skip_deserializing)]
+ pub queue: Vec<Box<dyn PipelineFn>>,
+}
+
+impl Serialize for Pipeline {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ let mut seq = serializer.serialize_seq(Some(self.queue.len()))?;
+ for elem in &self.queue {
+ seq.serialize_element(&elem.name())?;
+ }
+ seq.end()
+ }
+}
+
+impl Pipeline {
+ /// Run the Pipeline against the given vector of tokens. The returned vector may be shorter
+ /// than the input if a pipeline function returns `None` for a token.
+ pub fn run(&self, tokens: Vec<String>) -> Vec<String> {
+ let mut ret = vec![];
+ for token in tokens {
+ let mut token = Some(token);
+ for func in &self.queue {
+ if let Some(t) = token {
+ token = func.filter(t);
+ } else {
+ break;
+ }
+ }
+ if let Some(t) = token {
+ ret.push(t);
+ }
+ }
+ ret
+ }
+}