Skip to main content

yog_cli/
lib.rs

1//! The `yog` CLI's internals: `yog.toml` parsing, the generated
2//! `.yog-build/` workspace, `.yres` resources, `content/` blocks and items,
3//! `.yui` screens and `#[yog_export]` crates.
4//!
5//! Not for external use — a library only so the `yog` binary, its tests and
6//! its benches share the code. No API stability, not published separately.
7
8pub mod bench;
9pub mod book_page;
10pub mod cargo_toml;
11mod consts;
12pub mod content;
13pub mod design;
14pub mod exports;
15pub mod files;
16pub mod manifest;
17pub mod media;
18pub mod recipe_area;
19pub(crate) mod rust_code;
20pub mod serve;
21pub mod yres;
22pub mod yui;
23pub mod yui_component;
24pub mod yui_grid;
25pub mod yui_resources;
26pub mod yui_vm;
27
28/// `path` with `/` between its parts, whatever the system's separator.
29pub fn slash_path(path: &std::path::Path) -> String {
30    path.to_string_lossy().replace('\\', yog_ids::sep::PATH)
31}
32
33/// `path` as a project shows it: relative to its `root`, `/`-separated.
34pub(crate) fn shown_path(root: &std::path::Path, path: &std::path::Path) -> String {
35    slash_path(path.strip_prefix(root).unwrap_or(path))
36}
37
38/// Writes generated `code` to `path` (creating its directory) unless it's
39/// already there — unchanged files keep their mtime, so cargo doesn't
40/// rebuild for nothing.
41pub(crate) fn write_if_changed(
42    path: &std::path::Path,
43    code: impl AsRef<[u8]>,
44) -> Result<(), String> {
45    let code = code.as_ref();
46    if std::fs::read(path).ok().as_deref() == Some(code) {
47        return Ok(());
48    }
49    if let Some(dir) = path.parent() {
50        std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
51    }
52    std::fs::write(path, code).map_err(|e| format!("{}: {e}", path.display()))
53}