use std::collections::HashMap;
use std::collections::VecDeque;
pub trait Interpreter<TCommand>
{
type ErrorType;
fn execute(&mut self, resid: usize, cmd: TCommand) -> Result<(), Self::ErrorType>;
fn progress(&mut self, curcmd: usize, maxcmd: usize) -> Result<(), Self::ErrorType>;
}
pub struct CommandQueue<TCommand>
{
queue: VecDeque<(usize, TCommand)>,
cur_resource_id: usize
}
impl <TCommand> CommandQueue<TCommand>
{
pub fn new(cur_resource_id: usize) -> CommandQueue<TCommand>
{
return CommandQueue
{
queue: VecDeque::new(),
cur_resource_id: cur_resource_id
};
}
pub fn run<TInterpreter: Interpreter<TCommand>>(&mut self, interpreter: &mut TInterpreter) -> Result<(), TInterpreter::ErrorType>
{
let mut cur = 0;
let max = self.queue.len();
while let Some((resid, cmd)) = self.queue.pop_front()
{
interpreter.execute(resid, cmd)?;
interpreter.progress(cur, max)?;
cur += 1;
}
return Ok(());
}
pub fn push(&mut self, cmd: TCommand) -> usize
{
self.cur_resource_id += 1;
self.queue.push_back((self.cur_resource_id, cmd));
return self.cur_resource_id;
}
pub fn get_last_resource_id(&self) -> usize
{
return self.cur_resource_id;
}
}
pub enum InitCommand
{
DownloadFile(String, String, HashMap<String, String>),
UnpackCached(String),
ExtractResource(&'static str),
UserInput(String, String)
}
pub enum InstallCommand
{
DownloadFile(String, String, HashMap<String, String>),
UnpackCached(String),
ExtractResource(&'static str),
UserInput(String, String),
InstallResource(&'static str, usize),
CreateFolder(String),
AddToPath(String),
InstallCached(String, usize),
AddShortcut(String)
}