fix: take care of clippy warnings and add test image

This commit is contained in:
multisn8 2024-01-11 01:36:47 +01:00
parent ae9caa5732
commit b774493d73
Signed by: multisamplednight
GPG key ID: C81EF9B053977241
7 changed files with 37 additions and 36 deletions

View file

@ -2,8 +2,9 @@ pub mod read {
use image::{io::Reader as ImageReader, DynamicImage}; use image::{io::Reader as ImageReader, DynamicImage};
use rpl::instructions::read::{Read, SourceType}; use rpl::instructions::read::{Read, SourceType};
pub fn read(Read { source, format }: Read) -> DynamicImage { pub fn read(Read { source, format: _ }: Read) -> DynamicImage {
let mut img = ImageReader::open(match source { // TODO: actual error handling
let img = ImageReader::open(match source {
SourceType::File(path) => path, SourceType::File(path) => path,
}) })
.expect("something went wrong :((("); .expect("something went wrong :(((");
@ -13,19 +14,21 @@ pub mod read {
} }
pub mod write { pub mod write {
use image::{io::Reader as ImageReader, DynamicImage, ImageFormat}; use image::{DynamicImage, ImageFormat};
use rpl::instructions::write::{TargetFormat, TargetType, Write}; use rpl::instructions::write::{TargetFormat, TargetType, Write};
pub fn write(Write { target, format }: Write, input_data: DynamicImage) { pub fn write(Write { target, format }: Write, input_data: &DynamicImage) {
input_data.save_with_format( input_data
match target { .save_with_format(
TargetType::File(path) => path, match target {
}, TargetType::File(path) => path,
match format { },
TargetFormat::Jpeg => ImageFormat::Jpeg, match format {
TargetFormat::Png => ImageFormat::Png, TargetFormat::Jpeg => ImageFormat::Jpeg,
}, TargetFormat::Png => ImageFormat::Png,
); },
)
.expect("couldn't save file — come back later and handle me properly please uwu");
} }
} }

View file

@ -1,21 +1,21 @@
use rpl::instructions::{FilterInstruction, Instruction}; use rpl::instructions::{FilterInstruction, Instruction};
use crate::{value::DynamicValue, Executor}; use crate::value::Dynamic;
mod instructions; mod instructions;
pub struct DebugExecutor; pub struct Executor;
impl Executor for DebugExecutor { impl crate::Executor for Executor {
fn execute(instruction: Instruction, input: Option<DynamicValue>) -> Option<DynamicValue> { fn execute(instruction: Instruction, input: Option<Dynamic>) -> Option<Dynamic> {
match instruction { match instruction {
Instruction::Read(read_instruction) => Some(DynamicValue::Image( Instruction::Read(read_instruction) => {
instructions::read::read(read_instruction), Some(Dynamic::Image(instructions::read::read(read_instruction)))
)), }
Instruction::Write(write_instruction) => { Instruction::Write(write_instruction) => {
instructions::write::write( instructions::write::write(
write_instruction, write_instruction,
match input { match input {
Some(DynamicValue::Image(img)) => img, Some(Dynamic::Image(ref img)) => img,
_ => panic!("awawwawwa"), _ => panic!("awawwawwa"),
}, },
); );
@ -25,9 +25,9 @@ impl Executor for DebugExecutor {
Instruction::Blend(_) => todo!(), Instruction::Blend(_) => todo!(),
Instruction::Noise(_) => todo!(), Instruction::Noise(_) => todo!(),
Instruction::Filter(filter_instruction) => match filter_instruction { Instruction::Filter(filter_instruction) => match filter_instruction {
FilterInstruction::Invert => Some(DynamicValue::Image( FilterInstruction::Invert => Some(Dynamic::Image(
instructions::filters::invert::invert(match input { instructions::filters::invert::invert(match input {
Some(DynamicValue::Image(img)) => img, Some(Dynamic::Image(img)) => img,
_ => panic!("invalid value type for invert"), _ => panic!("invalid value type for invert"),
}), }),
)), )),

View file

@ -1,5 +1,5 @@
use rpl::instructions::Instruction; use rpl::instructions::Instruction;
use value::DynamicValue; use value::Dynamic;
mod debug; mod debug;
mod value; mod value;
@ -17,14 +17,14 @@ pub enum Executors {
} }
trait Executor { trait Executor {
fn execute(instruction: Instruction, input: Option<DynamicValue>) -> Option<DynamicValue>; fn execute(instruction: Instruction, input: Option<Dynamic>) -> Option<Dynamic>;
} }
pub fn execute_all(instructions: Vec<Instruction>) { pub fn execute_all(instructions: Vec<Instruction>) {
let mut tmp = None; let mut tmp = None;
for instruction in instructions { for instruction in instructions {
tmp = debug::DebugExecutor::execute(instruction, tmp); tmp = debug::Executor::execute(instruction, tmp);
} }
} }

View file

@ -1,5 +1,5 @@
use image::DynamicImage; use image::DynamicImage;
pub enum DynamicValue { pub enum Dynamic {
Image(DynamicImage), Image(DynamicImage),
} }

View file

@ -1,16 +1,14 @@
use instructions::Instruction; use instructions::Instruction;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::instructions::{
read::{SourceFormat, SourceType},
write::{TargetFormat, TargetType},
MathInstruction,
};
pub mod instructions; pub mod instructions;
/// # Panics
///
/// Panics if deserialization fails. lol.
#[must_use]
pub fn from_ron(raw: &str) -> Rpl { pub fn from_ron(raw: &str) -> Rpl {
ron::from_str(raw).unwrap() ron::from_str(raw).expect("come back later and handle me correctly")
} }
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] #[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]

View file

@ -1,12 +1,12 @@
( (
[ [
Read(( Read((
source: File("/home/jade/example/file.png"), source: File("testfiles/juan.jpg"),
format: Png format: Png
)), )),
Filter(Invert), Filter(Invert),
Write(( Write((
target: File("/home/jade/example/inverted.jpg"), target: File("testfiles/inverted.jpg"),
format: Jpeg format: Jpeg
)) ))
] ]

BIN
testfiles/juan.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB