out.rs

1.1 kB · rust · 32 lines

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