yog-book 0.1.0

In-game book/documentation system for Yog mods (Patchouli-like).
Documentation
//! yog-book — in-game book/documentation system for Yog mods (Patchouli-like).

use yog_registry::ItemDef;

/// Represents a book category (e.g. " Crafts", " Mechanics").
#[derive(Debug, Clone)]
pub struct BookCategory {
    pub id: String,
    pub name: String,
    pub description: Option<String>,
}

/// A single page inside a book entry. Can be text or a displayed item.
#[derive(Debug, Clone)]
pub enum BookPage {
    Text { text: String },
    Item { item: ItemDef },
}

/// One entry in a book (one "page" in the book's table of contents).
#[derive(Debug, Clone)]
pub struct BookEntry {
    pub id: String,
    pub name: String,
    pub category: String,
    pub pages: Vec<BookPage>,
}

/// The top-level book definition.
#[derive(Debug, Clone, Default)]
pub struct Book {
    pub id: String,
    pub name: String,
    pub author: Option<String>,
    pub categories: Vec<BookCategory>,
    pub entries: Vec<BookEntry>,
}

impl Book {
    pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            author: None,
            categories: Vec::new(),
            entries: Vec::new(),
        }
    }

    pub fn author(mut self, author: impl Into<String>) -> Self {
        self.author = Some(author.into());
        self
    }

    pub fn add_category(mut self, category: BookCategory) -> Self {
        self.categories.push(category);
        self
    }

    pub fn add_entry(mut self, entry: BookEntry) -> Self {
        self.entries.push(entry);
        self
    }
}

#[derive(Debug, Default)]
pub struct BookRegistry {
    books: std::collections::HashMap<String, Book>,
}

impl BookRegistry {
    pub fn register(&mut self, book: Book) {
        self.books.insert(book.id.clone(), book);
    }

    pub fn get(&self, id: &str) -> Option<&Book> {
        self.books.get(id)
    }
}