geolith-tiles-builder 0.4.2

Builds tiles from OpenStreetMap and Overture Maps: protomaps-schema vector PMTiles, Terrain-RGB, raster archives and 3D Tiles from Gaussian splats
Documentation
//! File writes whose errors name the file.

use std::path::Path;

use geolith::error::{GeolithError, Result};

/// Write `bytes` to `path`; an error says which file and what the OS said.
///
/// # Errors
///
/// [`GeolithError::Context`] naming `path` around the I/O error.
pub(crate) fn write_file(path: &Path, bytes: impl AsRef<[u8]>) -> Result<()> {
    std::fs::write(path, bytes)
        .map_err(|e| GeolithError::from(e).context(format!("writing {}", path.display())))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_failed_write_names_the_file_and_the_os_error() {
        let dir = tempfile::tempdir().expect("tmpdir");
        let path = dir.path().join("no-such-dir").join("scene.spz");
        let err = write_file(&path, b"x").expect_err("the folder does not exist");
        let text = geolith::error::report(&err);
        assert!(text.contains("scene.spz"), "{text}");
        assert!(
            text.contains("No such file or directory") || text.contains("os error"),
            "{text}"
        );
    }

    #[test]
    fn a_write_that_fits_succeeds() {
        let dir = tempfile::tempdir().expect("tmpdir");
        let path = dir.path().join("a.bin");
        write_file(&path, b"abc").expect("write");
        assert_eq!(std::fs::read(&path).expect("read"), b"abc");
    }
}