1use std::collections::HashMap;
6use std::fmt;
7use std::path::PathBuf;
8
9use anyhow::Result;
10use serde::{Deserialize, Serialize};
11use thiserror::Error;
12
13use crate::impl_serializable_error;
14
15#[derive(Error, Debug, Serialize, Deserialize)]
17pub enum EmuError {
18 #[error("Invalid argument name: {0}")]
19 InvalidArgumetName(String),
20 #[error("Argument name: {0} has invalid value: {1}")]
21 InvalidArgumentValue(String, String),
22 #[error("Start failed with cause: {0}")]
23 StartFailureCause(String),
24 #[error("Stop failed with cause: {0}")]
25 StopFailureCause(String),
26 #[error("Can't restore resource to initial state: {0}")]
27 ResetError(String),
28 #[error("Runtime error: {0}")]
29 RuntimeError(String),
30}
31impl_serializable_error!(EmuError);
32
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub enum EmuValue {
35 Empty,
36 String(String),
37 FilePath(PathBuf),
38 StringList(Vec<String>),
39 FilePathList(Vec<PathBuf>),
40}
41
42impl From<String> for EmuValue {
43 fn from(s: String) -> Self {
44 EmuValue::String(s)
45 }
46}
47
48impl From<Vec<String>> for EmuValue {
49 fn from(str_array: Vec<String>) -> Self {
50 EmuValue::StringList(str_array)
51 }
52}
53
54impl From<PathBuf> for EmuValue {
55 fn from(p: PathBuf) -> Self {
56 EmuValue::FilePath(p)
57 }
58}
59
60impl From<Vec<PathBuf>> for EmuValue {
61 fn from(path_array: Vec<PathBuf>) -> Self {
62 EmuValue::FilePathList(path_array)
63 }
64}
65
66impl fmt::Display for EmuValue {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 match self {
69 EmuValue::Empty => write!(f, " "),
70 EmuValue::String(value) => write!(f, "{}", value),
71 EmuValue::FilePath(value) => write!(f, "{}", value.display()),
72 EmuValue::StringList(value_list) => write!(f, "{}", value_list.join(",")),
73 EmuValue::FilePathList(value_list) => {
74 for item in value_list.iter() {
75 write!(f, "{} ", item.display())?;
76 }
77 Ok(())
78 }
79 }
80 }
81}
82
83#[derive(PartialEq, Eq, Clone, Copy, Debug, Serialize, Deserialize)]
84pub enum EmuState {
85 Off,
86 On,
87 Busy,
88 Error,
89}
90
91impl fmt::Display for EmuState {
92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93 match self {
94 EmuState::Off => write!(f, "Off"),
95 EmuState::On => write!(f, "On"),
96 EmuState::Busy => write!(f, "Busy"),
97 EmuState::Error => write!(f, "Error"),
98 }
99 }
100}
101
102pub trait Emulator {
104 fn get_state(&self) -> Result<EmuState>;
106
107 fn start(&self, factory_reset: bool, args: &HashMap<String, EmuValue>) -> Result<()>;
128
129 fn stop(&self) -> Result<()>;
131}