pub use iced_futures as futures;
pub use iced_program as program;
pub use iced_renderer as renderer;
pub use iced_runtime as runtime;
pub use iced_runtime::core;
pub use iced_selector as selector;
pub mod emulator;
pub mod ice;
pub mod instruction;
pub mod simulator;
mod error;
pub use emulator::Emulator;
pub use error::Error;
pub use ice::Ice;
pub use instruction::Instruction;
pub use selector::Selector;
pub use simulator::{Simulator, simulator};
use crate::core::Size;
use crate::core::time::{Duration, Instant};
use crate::core::window;
use std::path::Path;
pub fn run(
program: impl program::Program + 'static,
tests_dir: impl AsRef<Path>,
) -> Result<(), Error> {
use crate::futures::futures::StreamExt;
use crate::futures::futures::channel::mpsc;
use crate::futures::futures::executor;
use std::ffi::OsStr;
use std::fs;
let files = fs::read_dir(tests_dir)?;
let mut tests = Vec::new();
for file in files {
let file = file?;
if file.path().extension().and_then(OsStr::to_str) != Some("ice") {
continue;
}
let content = fs::read_to_string(file.path())?;
match Ice::parse(&content) {
Ok(ice) => {
let preset = if let Some(preset) = &ice.preset {
let Some(preset) = program
.presets()
.iter()
.find(|candidate| candidate.name() == preset)
else {
return Err(Error::PresetNotFound {
name: preset.to_owned(),
available: program
.presets()
.iter()
.map(program::Preset::name)
.map(str::to_owned)
.collect(),
});
};
Some(preset)
} else {
None
};
tests.push((file, ice, preset));
}
Err(error) => {
return Err(Error::IceParsingFailed {
file: file.path().to_path_buf(),
error,
});
}
}
}
for (file, ice, preset) in tests {
let (sender, mut receiver) = mpsc::channel(1);
let mut emulator = Emulator::with_preset(
sender,
&program,
ice.mode,
ice.viewport,
preset,
);
let mut instructions = ice.instructions.into_iter();
loop {
let event = executor::block_on(receiver.next())
.expect("emulator runtime should never stop on its own");
match event {
emulator::Event::Action(action) => {
emulator.perform(&program, action);
}
emulator::Event::Failed(instruction) => {
return Err(Error::IceTestingFailed {
file: file.path().to_path_buf(),
instruction,
});
}
emulator::Event::Ready => {
let Some(instruction) = instructions.next() else {
break;
};
emulator.run(&program, instruction);
}
}
}
}
Ok(())
}
pub fn screenshot<P: program::Program + 'static>(
program: &P,
theme: &P::Theme,
viewport: impl Into<Size>,
scale_factor: f32,
duration: Duration,
) -> window::Screenshot {
use crate::runtime::futures::futures::channel::mpsc;
let (sender, mut receiver) = mpsc::channel(100);
let mut emulator = Emulator::new(
sender,
program,
emulator::Mode::Immediate,
viewport.into(),
);
let start = Instant::now();
loop {
if let Some(event) = receiver.try_next().ok().flatten() {
match event {
emulator::Event::Action(action) => {
emulator.perform(program, action);
}
emulator::Event::Failed(_) => {
unreachable!(
"no instructions should be executed during a screenshot"
);
}
emulator::Event::Ready => {}
}
}
if start.elapsed() >= duration {
break;
}
std::thread::sleep(Duration::from_millis(1));
}
emulator.screenshot(program, theme, scale_factor)
}