Start ray format work

This commit is contained in:
AnActualEmerald 2022-08-26 14:04:38 -04:00
parent 0938481460
commit d294c3154e
Signed by: emerald
GPG Key ID: CC76D6B296CAC8B0
6 changed files with 120 additions and 13 deletions

View File

@ -8,5 +8,9 @@ authors = ["AnActualEmerald"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
compression = { version = "0.1.5", default-features = false, features = ["gzip"] }
anyhow = "1.0.62"
log = "0.4.17"
qoi = "0.4.0"
serde = "1.0.144"
serde_json = "1.0.85"
zip = "0.6.2"

45
ray_format/src/archive.rs Normal file
View File

@ -0,0 +1,45 @@
use anyhow::Context;
use anyhow::Result;
use std::io;
use std::io::Cursor;
use std::io::Write;
use zip::{write::FileOptions, ZipArchive, ZipWriter};
pub(crate) struct Archive {
buffer: Vec<u8>,
}
impl Archive {
pub(crate) fn new() -> Self {
let mut buffer = vec![];
ZipWriter::new(Cursor::new(&mut buffer));
Self { buffer }
}
pub(crate) fn buffer(&self) -> &[u8] {
self.buffer.as_ref()
}
pub(crate) fn add_file(&mut self, file_name: &str, data: &[u8]) -> Result<()> {
let mut zip = ZipWriter::new_append(Cursor::new(&mut self.buffer))?;
let options = FileOptions::default().compression_method(zip::CompressionMethod::Bzip2);
zip.start_file(file_name, options)?;
zip.write(data)?;
zip.finish()?;
Ok(())
}
pub(crate) fn get_file(&self, file_name: &str) -> Result<Vec<u8>> {
let mut buffer = vec![];
let mut zip = ZipArchive::new(Cursor::new(&self.buffer))?;
let mut f = zip
.by_name(file_name)
.context("Couldn't find a file by that name")?;
io::copy(&mut f, &mut Cursor::new(&mut buffer))?;
Ok(buffer)
}
}

View File

@ -1,14 +1,6 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
mod test;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
mod archive;
mod manage;
mod model;

1
ray_format/src/manage.rs Normal file
View File

@ -0,0 +1 @@

43
ray_format/src/model.rs Normal file
View File

@ -0,0 +1,43 @@
use anyhow::Result;
use std::{collections::HashMap, path::Path};
use crate::archive::Archive;
#[derive(Default, Clone, Debug)]
pub struct Ray {
frames: [Vec<u8>; 4],
extras: HashMap<String, Vec<u8>>,
meta: HashMap<String, String>,
}
impl Ray {
pub fn get_frame(&self, index: usize) -> Option<&Vec<u8>> {
self.frames.get(index)
}
pub fn set_frame(&mut self, index: usize, data: Vec<u8>) -> bool {
if let Some(i) = self.frames.get_mut(index) {
*i = data;
true
} else {
false
}
}
pub fn save(&self, path: &Path) -> Result<()> {
let mut archive = Archive::new();
for (i, f) in self.frames.iter().enumerate() {
if f.len() > 0 {
archive.add_file(&format!("{}", i), f)?
}
}
for (n, f) in self.extras.iter() {
archive.add_file(&n, f)?;
}
let meta = serde_json::to_string(&self.meta)?;
archive.add_file("meta.json", meta.as_bytes())?;
Ok(())
}
}

View File

@ -0,0 +1,22 @@
use crate::archive::Archive;
#[test]
fn add_file_to_archive() {
let mut archive = Archive::new();
let res = archive.add_file("hello", b"world");
assert!(res.is_ok());
}
#[test]
fn get_file_from_archive() {
let mut archive = Archive::new();
archive.add_file("hello", b"world").unwrap();
let file = archive.get_file("hello");
assert!(file.is_ok());
assert_eq!(
String::from("world"),
String::from_utf8(file.unwrap()).unwrap()
);
}