lyra-engine/lyra-resource/src/util.rs

42 lines
1.4 KiB
Rust
Raw Normal View History

use base64::Engine;
use thiserror::Error;
use std::io;
2023-10-23 01:49:31 +00:00
#[allow(dead_code)]
#[derive(Error, Debug)]
pub enum UriReadError {
#[error("IOError: '{0}'")]
IoError(io::Error),
// From is implemented for this field in each loader module
#[error("Base64 decoding error: '{0}'")]
Base64Decode(base64::DecodeError),
#[error("Some data was missing from the uri")]
None
}
/// Read a buffer's uri string into a byte buffer.
///
/// * `containing_path`: The path of the containing folder of the buffers "parent",
/// the parent being where this buffer is defined in,
/// i.e. parent="resources/models/player.gltf", containing="resource/models"
pub(crate) fn gltf_read_buffer_uri(containing_path: &str, uri: &str) -> Result<Vec<u8>, UriReadError> {
if let Some((mime, data)) = uri.strip_prefix("data")
2023-10-23 01:49:31 +00:00
.and_then(|uri| uri.split_once(',')) {
let (_mime, is_base64) = match mime.strip_suffix(";base64") {
Some(mime) => (mime, true),
None => (mime, false),
};
if is_base64 {
base64::engine::general_purpose::STANDARD.decode(data)
2023-10-23 01:49:31 +00:00
.map_err(UriReadError::Base64Decode)
} else {
Ok(data.as_bytes().to_vec())
}
} else {
let full_path = format!("{containing_path}/{uri}");
2023-10-23 01:49:31 +00:00
std::fs::read(full_path).map_err(UriReadError::IoError)
}
}