Post

Trait Post 

Source
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§

Source

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.

Source

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.

Source

fn body(&self) -> Option<&str>

Get the post body content, if any

The body content is tokenized and indexed for full-text search. Return None if the post has no body content (e.g., for title-only posts).

Source

fn meta(&self) -> HashMap<String, String>

Get metadata for the post as key-value pairs

Metadata is also indexed and searchable, useful for things like author names, tags, categories, or other structured data you want to be findable. Return an empty HashMap if no metadata should be indexed.

Implementors§