mod3d_gltf/
utils.rs

1//a Imports
2use base64::engine::general_purpose as base64_decoder;
3use base64::Engine;
4
5use crate::{Error, Result};
6
7//a Buffer parsing functions
8//fp try_buf_parse_base64
9/// Attempt to parse a URI as a data::base64 octet stream
10///
11/// If it is not such a URI then return Ok(None); if it is, then parse
12/// it and return Ok(Some(Vec u8))) or Err()
13pub fn try_buf_parse_base64(uri: &str, byte_length: usize) -> Result<Option<Vec<u8>>> {
14    let Some(data) = uri.strip_prefix("data:application/octet-stream;base64,") else {
15        return Ok(None);
16    };
17    let bytes = base64_decoder::STANDARD.decode(data)?;
18    if bytes.len() < byte_length {
19        Err(Error::BufferTooShort)
20    } else {
21        Ok(Some(bytes))
22    }
23}
24
25//fp buf_parse_fail
26/// Return a result that indicates a failure to parse the URI
27pub fn buf_parse_fail<T>(_uri: &str, _byte_length: usize) -> Result<T> {
28    Err(Error::BufferRead)
29}