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
8// What the binary, tests and benches reach is documented (the rest is `pub(crate)`).
9#![warn(missing_docs)]
10
11pub mod bench;
12pub(crate) mod book_page;
13pub mod cargo_toml;
14mod consts;
15pub mod content;
16pub mod design;
17pub mod diagnostics;
18pub mod exports;
19pub mod files;
20pub mod manifest;
21pub mod mappings;
22pub(crate) mod media;
23pub(crate) mod recipe_area;
24pub(crate) mod rust_code;
25pub mod serve;
26pub mod yres;
27pub mod yui;
28pub mod yui_component;
29pub mod yui_grid;
30pub(crate) mod yui_resources;
31pub(crate) mod yui_vm;
32
33/// `path` with `/` between its parts, whatever the system's separator.
34pub fn slash_path(path: &std::path::Path) -> String {
35    path.to_string_lossy().replace('\\', yog_ids::sep::PATH)
36}
37
38/// `path` as a project shows it: relative to its `root`, `/`-separated.
39pub(crate) fn shown_path(root: &std::path::Path, path: &std::path::Path) -> String {
40    slash_path(path.strip_prefix(root).unwrap_or(path))
41}
42
43/// Writes generated `code` to `path` (creating its directory) unless it's
44/// already there — unchanged files keep their mtime, so cargo doesn't
45/// rebuild for nothing.
46pub(crate) fn write_if_changed(
47    path: &std::path::Path,
48    code: impl AsRef<[u8]>,
49) -> Result<(), String> {
50    let code = code.as_ref();
51    if std::fs::read(path).ok().as_deref() == Some(code) {
52        return Ok(());
53    }
54    if let Some(dir) = path.parent() {
55        std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
56    }
57    std::fs::write(path, code).map_err(|e| format!("{}: {e}", path.display()))
58}