Skip to main content

yog_cli/
book_page.rs

1//! A mod's own kinds of book page: `.yui` components with `book_page`,
2//! compiled into `yog_formats::book::page_type::PageType`s for the books that use
3//! them (see that module).
4//!
5//! ```xml
6//! <Component name="PatternPage" book_page="hexcasting:pattern">
7//!     <Property name="name" type="book_text"/>
8//!     <Property name="patterns" type="data" default="[]"/>
9//!     <BookHeader text="{x:Bind name}"/>
10//!     <Drawn drawer="hexcasting:pattern" h="64" data="{x:Bind patterns}" visible="{x:Bind patterns}"/>
11//! </Component>
12//! ```
13
14use std::path::Path;
15
16use yog_formats::book::page_type::{PageType, compile, is_book_page};
17
18use crate::yui::{self, Element};
19
20/// Whether `root` is a book's own UI — a kind of page (`book_page`) or a
21/// recipe layout (`recipe_layout`) — not a screen or a component.
22pub fn is_book_ui(root: &Element) -> bool {
23    is_book_page(root) || yog_formats::book::layout::is_recipe_layout(root)
24}
25
26/// The mod's page types: every `book_page` component under `src/`.
27pub fn find(root: &Path, ns: &str, errors: &mut Vec<String>) -> Vec<PageType> {
28    let mut out = Vec::new();
29    for file in yui::find_yui_files(&root.join("src")) {
30        let Ok(text) = std::fs::read_to_string(&file) else { continue };
31        let Ok(el) = yui::parse_xml(&text) else { continue };
32        if !is_book_page(&el) {
33            continue;
34        }
35        let shown = file.strip_prefix(root).unwrap_or(&file).display().to_string();
36        match compile(&el, ns) {
37            Ok(t) => out.push(t),
38            Err(e) => errors.push(format!("{shown}: {e}")),
39        }
40    }
41    out
42}
43
44/// The mod's recipe layouts: every `recipe_layout` component under `src/`.
45pub fn find_layouts(root: &Path, ns: &str, errors: &mut Vec<String>) -> Vec<(String, yog_formats::book::layout::RecipeLayout)> {
46    let mut out = Vec::new();
47    for file in yui::find_yui_files(&root.join("src")) {
48        let Ok(text) = std::fs::read_to_string(&file) else { continue };
49        let Ok(el) = yui::parse_xml(&text) else { continue };
50        if !yog_formats::book::layout::is_recipe_layout(&el) {
51            continue;
52        }
53        let shown = file.strip_prefix(root).unwrap_or(&file).display().to_string();
54        match yog_formats::book::layout::compile(&el, ns) {
55            Ok(l) => out.push((shown, l)),
56            Err(e) => errors.push(format!("{shown}: {e}")),
57        }
58    }
59    out
60}