chore: rename executor -> evaluator and integrate into app

This commit is contained in:
multisn8 2024-01-21 03:22:46 +01:00
parent de9ca81b65
commit 3c529c3a1a
Signed by: multisamplednight
GPG key ID: 6D525AA147CBDAE2
16 changed files with 229 additions and 115 deletions

View file

@ -0,0 +1,44 @@
pub mod read {
use image::{io::Reader as ImageReader, DynamicImage};
use ir::instruction::read::{Read, SourceType};
pub fn read(Read { source, .. }: Read) -> DynamicImage {
// TODO: actual error handling
let img = ImageReader::open(match source {
SourceType::File(path) => path,
})
.expect("something went wrong :(((");
img.decode().expect("couldn't decode image")
}
}
pub mod write {
use image::{DynamicImage, ImageFormat};
use ir::instruction::write::{TargetFormat, TargetType, Write};
pub fn write(Write { target, format }: Write, input_data: &DynamicImage) {
input_data
.save_with_format(
match target {
TargetType::File(path) => path,
},
match format {
TargetFormat::Jpeg => ImageFormat::Jpeg,
TargetFormat::Png => ImageFormat::Png,
},
)
.expect("couldn't save file — come back later and handle me properly please uwu");
}
}
pub mod filters {
pub mod invert {
use image::DynamicImage;
pub fn invert(mut input_data: DynamicImage) -> DynamicImage {
input_data.invert();
input_data
}
}
}

View file

@ -0,0 +1,57 @@
use std::mem;
use ir::{
instruction::{Filter, Kind},
GraphIr, Instruction, InstructionRef,
};
use crate::value::Dynamic;
mod instr;
#[derive(Debug, Default)]
pub struct Evaluator {
ir: GraphIr,
}
impl crate::Evaluator for Evaluator {
fn feed(&mut self, ir: GraphIr) {
// TODO: should add instead of replace, see note in Evaluator trait above this method
self.ir = ir;
}
fn eval_full(&mut self) {
let queue: Vec<Instruction> = self
.ir
.topological_sort()
.into_iter()
.map(Into::into)
.collect();
for instr in queue {
self.step(instr);
}
}
}
impl Evaluator {
#[allow(clippy::needless_pass_by_value)]
fn step(&mut self, instr: Instruction) {
let _ = match instr.kind {
Kind::Read(details) => Some(Dynamic::Image(instr::read::read(details))),
Kind::Write(details) => {
instr::write::write(details, todo!());
None
}
Kind::Math(_) => todo!(),
Kind::Blend(_) => todo!(),
Kind::Noise(_) => todo!(),
Kind::Filter(filter_instruction) => match filter_instruction {
Filter::Invert => Some(Dynamic::Image(instr::filters::invert::invert(
match todo!() {
Some(Dynamic::Image(img)) => img,
_ => panic!("invalid value type for invert"),
},
))),
},
};
}
}

View file

@ -0,0 +1 @@
pub mod debug;