out.rs

1.1 kB · rust · 31 lines

1use crate::board::Board;2use crate::ink;3use mrlyrs::core::error::{value_error, Result};4use std::path::PathBuf;56// OUTPUT78/// Returns the workspace root, one level above this crate.9pub fn root() -> PathBuf {10    PathBuf::from(env!("CARGO_MANIFEST_DIR"))11        .parent()12        .map(PathBuf::from)13        .unwrap_or_else(|| PathBuf::from("."))14}1516/// Writes the board to files/figures/<name>-<theme>.png, dark or light, and announces the one line it printed.17pub fn save(name: &str, board: &Board) -> Result<PathBuf> {18    let name = format!("{name}-{}", ink::name());19    let folder = root().join("files").join("figures");20    std::fs::create_dir_all(&folder)21        .map_err(|e| mrlyrs::Error::Value(format!("cannot make {folder:?}: {e}")))?;22    let path = folder.join(format!("{name}.png"));23    let bytes = board.png()?;24    if bytes.is_empty() {25        return value_error("the png came back empty.");26    }27    std::fs::write(&path, bytes)28        .map_err(|e| mrlyrs::Error::Value(format!("cannot write {path:?}: {e}")))?;29    println!("figure {name} {}x{}", board.width, board.height);30    Ok(path)31}