Refactoring for StageResult related to issue #1
This commit is contained in:
+100
-32
@@ -1,23 +1,22 @@
|
||||
// <Kira Installer - universal Linux installer.>
|
||||
// Copyright (C) <2026> <Kira Foundation>
|
||||
// <Kira Installer - universal Linux installer.>
|
||||
// Copyright (C) <2026> <Kira Foundation>
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
/*
|
||||
This file contains basic enums and structs to be used with stages.
|
||||
*/
|
||||
This file contains basic enums and structs to be used with stages.
|
||||
*/
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -29,15 +28,98 @@ pub enum ConfigValue {
|
||||
F64(f64),
|
||||
Bool(bool),
|
||||
Vector(Vec<ConfigValue>),
|
||||
Dictionary(HashMap<String, ConfigValue>)
|
||||
Dictionary(HashMap<String, ConfigValue>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StageResult {
|
||||
pub name: String,
|
||||
pub config: Option<HashMap<String, ConfigValue>>,
|
||||
pub resuts: Option<HashMap<String, ConfigValue>>,
|
||||
pub error: Option<String>
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
impl StageResult {
|
||||
pub fn new(name: &str) -> Self {
|
||||
Self {
|
||||
name: name.into(),
|
||||
config: None,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_error(mut self, ex: String) -> Self {
|
||||
self.error = Some(ex);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_val(mut self, val_name: &str, conf_val: ConfigValue) -> Self {
|
||||
match &mut self.config {
|
||||
Some(c) => {
|
||||
c.insert(val_name.into(), conf_val);
|
||||
}
|
||||
None => {
|
||||
self.config = Some(HashMap::from([(val_name.into(), conf_val)]));
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_val_string(self, val_name: &str, v: String) -> Self {
|
||||
self.add_val(val_name, ConfigValue::String(v))
|
||||
}
|
||||
|
||||
pub fn add_val_bool(self, val_name: &str, v: bool) -> Self {
|
||||
self.add_val(val_name, ConfigValue::Bool(v))
|
||||
}
|
||||
|
||||
pub fn get_val(&self, val_name: &str) -> Option<ConfigValue> {
|
||||
let v = self
|
||||
.config
|
||||
.as_ref()
|
||||
.and_then(|hm| hm.get(val_name).cloned());
|
||||
return v;
|
||||
}
|
||||
|
||||
pub fn get_val_str(&self, val_name: &str) -> Option<String> {
|
||||
match self.get_val(val_name) {
|
||||
Some(ConfigValue::String(v)) => Some(v),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_val_bool(&self, val_name: &str) -> Option<bool> {
|
||||
match self.get_val(val_name) {
|
||||
Some(ConfigValue::Bool(v)) => Some(v),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct KiraConfig {
|
||||
pub config_trail: Vec<StageResult>,
|
||||
}
|
||||
|
||||
impl Default for KiraConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
config_trail: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl KiraConfig {
|
||||
pub fn get_stage(&self, name: &str) -> Option<StageResult> {
|
||||
self.config_trail
|
||||
.iter()
|
||||
.find(|v| v.name == name)
|
||||
.and_then(|v| Some(v.clone()))
|
||||
}
|
||||
pub fn pop_last(&mut self) -> Option<StageResult> {
|
||||
self.config_trail.pop()
|
||||
}
|
||||
pub fn push(&mut self, stage_result: StageResult) {
|
||||
self.config_trail.push(stage_result);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -47,17 +129,3 @@ pub enum StageAction {
|
||||
Next(StageResult),
|
||||
Abort,
|
||||
}
|
||||
|
||||
|
||||
pub struct KiraConfig {
|
||||
pub config_trail: Vec<StageResult>,
|
||||
}
|
||||
|
||||
impl KiraConfig {
|
||||
pub fn get_stage(&self, name: &str) -> Option<StageResult> {
|
||||
self.config_trail.iter().find(|v| v.name == name)
|
||||
.and_then(|v| Some(v.clone()))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user