ft: deserialization

This commit is contained in:
2026-04-26 23:49:03 +02:00
parent 430bdf63bc
commit 076bcc7155
5 changed files with 55 additions and 27 deletions

View File

@@ -41,11 +41,9 @@ impl Default for RawCamera {
}
}
impl TryFrom<RawCamera> for Camera {
type Error = String;
fn try_from(raw: RawCamera) -> Result<Self, Self::Error> {
let c = Camera::new_full(
impl From<RawCamera> for Camera {
fn from(raw: RawCamera) -> Self {
Camera::new_full(
raw.image_width,
raw.image_height,
raw.anti_alias_rate,
@@ -54,8 +52,6 @@ impl TryFrom<RawCamera> for Camera {
raw.look_from,
raw.look_at,
raw.vup,
);
Ok(c)
)
}
}

View File

@@ -1,8 +1,6 @@
use std::{
fmt::{self, Debug},
sync::Arc,
};
use std::sync::Arc;
use log::warn;
use serde::Deserialize;
use crate::{
@@ -53,10 +51,10 @@ impl<'de> Deserialize<'de> for Scene {
let objs: Vec<Arc<dyn Hittable>> = conc
.objects
.into_iter()
.map(|h| h.into_arc(&mats))
.filter_map(|h| h.into_arc(&mats))
.collect();
Ok(Self {
camera: Camera::try_from(conc.camera).unwrap(),
camera: Camera::from(conc.camera),
materials: mats,
objects: objs,
})
@@ -77,13 +75,23 @@ enum HittableDef {
}
impl HittableDef {
fn into_arc(self, materials: &Vec<Arc<dyn Material>>) -> Arc<dyn Hittable> {
fn into_arc(self, materials: &Vec<Arc<dyn Material>>) -> Option<Arc<dyn Hittable>> {
// THOUGHT: i think this can be done better; in the map/filter call up there?
match self {
HittableDef::Sphere(s) => Arc::new(Sphere::new(
s.center,
s.radius,
materials.get(s.material as usize).unwrap().clone(),
)),
HittableDef::Sphere(s) => {
if s.material as usize >= materials.len() {
warn!(
"Sphere specified nonexistent material {}; skipping...",
s.material
);
return None;
}
Some(Arc::new(Sphere::new(
s.center,
s.radius,
materials.get(s.material as usize).unwrap().clone(),
)))
}
}
}
}