pub trait Post {
// Required methods
fn title(&self) -> &str;
fn url(&self) -> &str;
fn body(&self) -> Option<&str>;
fn meta(&self) -> HashMap<String, String>;
}
Expand description
Trait that types must implement to be used as posts in tinysearch
This trait allows users to use their own post types without needing to convert to a specific struct, as long as they can provide the required fields through these methods.
§Example
use tinysearch::Post;
use std::collections::HashMap;
#[derive(Debug)]
struct BlogPost {
title: String,
permalink: String,
content: String,
author: String,
}
impl Post for BlogPost {
fn title(&self) -> &str {
&self.title
}
fn url(&self) -> &str {
&self.permalink
}
fn body(&self) -> Option<&str> {
Some(&self.content)
}
fn meta(&self) -> HashMap<String, String> {
let mut meta = HashMap::new();
meta.insert("author".to_string(), self.author.clone());
meta
}
}
Required Methods§
Sourcefn title(&self) -> &str
fn title(&self) -> &str
Get the post title
The title is used both for display in search results and as part of the searchable content. Title matches are weighted higher than body matches.
Sourcefn url(&self) -> &str
fn url(&self) -> &str
Get the post URL or identifier
This should be a unique identifier for the post, typically a URL path or permalink that can be used to navigate to the post.