72 lines
1.9 KiB
Rust
72 lines
1.9 KiB
Rust
use abacus_core::Output;
|
|
use druid::{
|
|
im::{vector, Vector},
|
|
Data, Lens,
|
|
};
|
|
|
|
use crate::editor;
|
|
|
|
#[derive(Clone, Data, Lens, Debug)]
|
|
pub struct AppData {
|
|
#[data(same_fn = "PartialEq::eq")]
|
|
pub filename: Option<String>,
|
|
pub blocks: Vector<Block>,
|
|
}
|
|
|
|
impl Default for AppData {
|
|
fn default() -> Self {
|
|
Self {
|
|
filename: None,
|
|
blocks: vector![Block::new("Block #1", 0)],
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AppData {
|
|
pub fn process(&mut self) {
|
|
let mut engine = abacus_core::Engine::default();
|
|
for mut block in self.blocks.iter_mut() {
|
|
let start = std::time::Instant::now();
|
|
block.output = engine.process_script(&block.editor_data.content.to_string());
|
|
println!("block executed in {}", start.elapsed().as_millis());
|
|
}
|
|
}
|
|
pub fn process_block(&mut self, index: usize) {
|
|
let mut engine = abacus_core::Engine::default();
|
|
if let Some(block) = self.blocks.get_mut(index) {
|
|
let start = std::time::Instant::now();
|
|
block.output = engine.process_script(&block.editor_data.content.to_string());
|
|
println!("block executed in {}", start.elapsed().as_millis());
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Data, Lens, Default, PartialEq, Debug)]
|
|
pub struct Block {
|
|
pub name: String,
|
|
pub editor_data: editor::EditorData,
|
|
#[data(same_fn = "PartialEq::eq")]
|
|
pub output: Output,
|
|
pub index: usize,
|
|
}
|
|
|
|
impl Block {
|
|
pub fn new(name: &str, index: usize) -> Self {
|
|
Self {
|
|
name: name.to_string(),
|
|
editor_data: Default::default(),
|
|
output: Default::default(),
|
|
index,
|
|
}
|
|
}
|
|
|
|
pub fn new_with_content(name: &str, index: usize, content: &str) -> Self {
|
|
Self {
|
|
name: name.to_string(),
|
|
editor_data: editor::EditorData::new(content),
|
|
output: Default::default(),
|
|
index,
|
|
}
|
|
}
|
|
}
|