Refactoring for StageResult related to issue #1

This commit is contained in:
2026-05-16 21:10:50 +02:00
parent 4000e0cfac
commit cf916eb508
8 changed files with 190 additions and 217 deletions
+100 -32
View File
@@ -1,23 +1,22 @@
// <Kira Installer - universal Linux installer.> // <Kira Installer - universal Linux installer.>
// Copyright (C) <2026> <Kira Foundation> // Copyright (C) <2026> <Kira Foundation>
// This program is free software: you can redistribute it and/or modify // 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 // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // 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/>.
// 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; use std::collections::HashMap;
@@ -29,15 +28,98 @@ pub enum ConfigValue {
F64(f64), F64(f64),
Bool(bool), Bool(bool),
Vector(Vec<ConfigValue>), Vector(Vec<ConfigValue>),
Dictionary(HashMap<String, ConfigValue>) Dictionary(HashMap<String, ConfigValue>),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct StageResult { pub struct StageResult {
pub name: String, pub name: String,
pub config: Option<HashMap<String, ConfigValue>>, 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)] #[derive(Debug, Clone)]
@@ -47,17 +129,3 @@ pub enum StageAction {
Next(StageResult), Next(StageResult),
Abort, 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()))
}
}
+5 -18
View File
@@ -276,25 +276,12 @@ impl KeyboardStage {
if let Some(model) = &self.model if let Some(model) = &self.model
&& let Some(k_l) = &self.keyboard_layout && let Some(k_l) = &self.keyboard_layout
{ {
StageResult { StageResult::new("keyboard")
name: "keyboard".to_string(), .add_val_string("model", model.model.clone())
config: Some(HashMap::from([ .add_val_string("layout", k_l.to_result())
(
"model".to_string(),
ConfigValue::String(model.model.clone()),
),
("layout".to_string(), ConfigValue::String(k_l.to_result())),
])),
resuts: None,
error: None,
}
} else { } else {
StageResult { StageResult::new("keyboard")
name: "keyboard".to_string(), .add_error("Something go terrible wrong, there is no keyboard layouts.".into())
config: None,
resuts: None,
error: Some("Something go terrible wrong, there is no keyboard layouts.".into()),
}
} }
} }
+2 -10
View File
@@ -21,7 +21,6 @@
use iced::{Alignment, widget}; use iced::{Alignment, widget};
use rust_i18n::t; use rust_i18n::t;
use std::collections::HashMap;
use crate::stage; use crate::stage;
use crate::stage::StageResult; use crate::stage::StageResult;
@@ -44,15 +43,8 @@ pub enum Message {
impl LicenseStage { impl LicenseStage {
fn accepted() -> StageResult { fn accepted() -> StageResult {
StageResult { StageResult::new("license")
name: "license".into(), .add_val_bool("accepted", true)
config: Some(HashMap::from([(
"accepted".to_string(),
stage::ConfigValue::Bool(true),
)])),
resuts: None,
error: None,
}
} }
pub fn update(&mut self, message: Message) -> stage::StageAction { pub fn update(&mut self, message: Message) -> stage::StageAction {
+7 -27
View File
@@ -215,35 +215,15 @@ impl LocaleStage {
if let Some(lang_locale) = &self.lang_locale if let Some(lang_locale) = &self.lang_locale
&& let Some(formats_locale) = &self.formats_locale && let Some(formats_locale) = &self.formats_locale
{ {
StageResult { StageResult::new("locale")
name: "locale".to_string(), .add_val_string("lang_locale", lang_locale.code.clone())
config: Some(HashMap::from([ .add_val_string("formats_locale", formats_locale.code.clone())
(
"lang_locale".to_string(),
ConfigValue::String(lang_locale.code.clone()),
),
(
"formats_locale".to_string(),
ConfigValue::String(formats_locale.code.clone()),
),
])),
resuts: None,
error: None,
}
} else { } else {
let loc = LocaleData::default(); let loc = LocaleData::default();
StageResult { StageResult::new("locale")
name: "locale".to_string(), .add_val_string("lang_locale", loc.code.clone())
config: Some(HashMap::from([ .add_val_string("formats_locale", loc.code)
(
"lang_locale".to_string(),
ConfigValue::String(loc.code.clone()),
),
("formats_locale".to_string(), ConfigValue::String(loc.code)),
])),
resuts: None,
error: None,
}
} }
} }
+3 -15
View File
@@ -127,21 +127,9 @@ impl NetworkStage {
} }
fn gen_result(&self) -> StageResult { fn gen_result(&self) -> StageResult {
StageResult { StageResult::new("network")
name: "network".into(), .add_val_bool("internet_active", self.internet_active)
config: Some(HashMap::from([ .add_val_bool("use_net_settings", self.use_net_settings)
(
"internet_active".to_string(),
stage::ConfigValue::Bool(self.internet_active),
),
(
"use_net_settings".to_string(),
stage::ConfigValue::Bool(self.use_net_settings),
),
])),
resuts: None,
error: None,
}
} }
pub fn update(&mut self, message: Message) -> UpdateResult { pub fn update(&mut self, message: Message) -> UpdateResult {
+62 -60
View File
@@ -20,13 +20,10 @@
use crate::stage::{ConfigValue, StageAction, StageResult}; use crate::stage::{ConfigValue, StageAction, StageResult};
use iced::{ use crate::kira_color_bar;
Alignment, Color, Length, use iced::{Alignment, Color, Length, widget};
widget,
};
use rust_i18n::t; use rust_i18n::t;
use std::collections::HashMap; use std::collections::HashMap;
use crate::kira_color_bar;
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SwapMode { pub enum SwapMode {
@@ -243,30 +240,47 @@ pub fn get_dev_part_blocking(dev_name: &str) -> Result<Vec<PartInfo>, String> {
} }
} }
use toml::Table; use toml::Table;
impl PartitionStage { impl PartitionStage {
pub fn gen_disk_layout_estimate(&self) -> Vec<PartInfo> { pub fn gen_disk_layout_estimate(&self) -> Vec<PartInfo> {
let dev = self.device.clone().unwrap(); let dev = self.device.clone().unwrap();
let swap_size: u64 = match self.swap_mode { let swap_size: u64 = match self.swap_mode {
Some(SwapMode::NoSwap) => 0, Some(SwapMode::NoSwap) => 0,
Some(SwapMode::SwapHibernate) => 666, Some(SwapMode::SwapHibernate) => 666,
Some(SwapMode::SwapNoHibernate) => 1024, Some(SwapMode::SwapNoHibernate) => 1024,
_ => 1024 _ => 1024,
}; };
let mut res: Vec<PartInfo> = Vec::new(); let mut res: Vec<PartInfo> = Vec::new();
// UEFI partition // UEFI partition
res.push(PartInfo { name: "EFI".into(), part_type: "vfat".into(), size_mb: 256 }); res.push(PartInfo {
name: "EFI".into(),
part_type: "vfat".into(),
size_mb: 256,
});
// boot A and B // boot A and B
res.push(PartInfo { name: "kira_boot_a".into(), part_type: "xfs".into(), size_mb: 512 }); res.push(PartInfo {
res.push(PartInfo { name: "kira_boot_b".into(), part_type: "xfs".into(), size_mb: 512 }); name: "kira_boot_a".into(),
part_type: "xfs".into(),
size_mb: 512,
});
res.push(PartInfo {
name: "kira_boot_b".into(),
part_type: "xfs".into(),
size_mb: 512,
});
// user data // user data
res.push(PartInfo { name: "kira_data".into(), part_type: "xfs".into(), size_mb: dev.size_mb - 1024 - 256 - swap_size }); res.push(PartInfo {
name: "kira_data".into(),
part_type: "xfs".into(),
size_mb: dev.size_mb - 1024 - 256 - swap_size,
});
// swap // swap
if swap_size > 0 { if swap_size > 0 {
res.push(PartInfo { name: "".into(), part_type: "swap".into(), size_mb: swap_size }); res.push(PartInfo {
name: "".into(),
part_type: "swap".into(),
size_mb: swap_size,
});
} }
res res
@@ -306,36 +320,23 @@ impl PartitionStage {
} }
fn gen_result(&self) -> StageResult { fn gen_result(&self) -> StageResult {
StageResult { StageResult::new("partition")
name: "partition".to_string(), .add_val_string(
config: Some(HashMap::from([ "device",
( self.device
"device".to_string(), .as_ref()
ConfigValue::String( .and_then(|v| Some(v.name.clone()))
self.device .unwrap_or_else(|| "NONE".to_string()),
.as_ref() )
.and_then(|v| Some(v.name.clone())) .add_val_string(
.unwrap_or_else(|| "NONE".to_string()), "swap_mode",
), self.swap_mode
), .clone()
( .unwrap_or(SwapMode::NoSwap)
"swap_mode".to_string(), .to_string(),
ConfigValue::String( )
self.swap_mode .add_val_bool("zram", self.use_zram)
.clone() .add_val_bool("secure_boot", self.secure_boot)
.unwrap_or(SwapMode::NoSwap)
.to_string(),
),
),
("zram".to_string(), ConfigValue::Bool(self.use_zram)),
(
"secure_boot".to_string(),
ConfigValue::Bool(self.secure_boot),
),
])),
resuts: None,
error: None,
}
} }
pub fn update(&mut self, message: Message) -> StageAction { pub fn update(&mut self, message: Message) -> StageAction {
@@ -375,34 +376,35 @@ impl PartitionStage {
} }
pub fn view(&self) -> iced::Element<'_, Message> { pub fn view(&self) -> iced::Element<'_, Message> {
let dev_parts_es: Option<Vec<(String, u16, Color)>> = if self.selected_disk_parts.is_some() { let dev_parts_es: Option<Vec<(String, u16, Color)>> = if self.selected_disk_parts.is_some()
Some(self.gen_disk_layout_estimate().iter() {
.map(|part_inf| part_inf.to_ct_params(self.device.clone().unwrap().size_mb)) Some(
.collect()) self.gen_disk_layout_estimate()
} .iter()
else { .map(|part_inf| part_inf.to_ct_params(self.device.clone().unwrap().size_mb))
.collect(),
)
} else {
None None
}; };
let dev_parts_text = match dev_parts_es { let dev_parts_text = match dev_parts_es {
Some(_) => widget::text(t!("partition.current_after_install")), Some(_) => widget::text(t!("partition.current_after_install")),
None => widget::text("") None => widget::text(""),
}; };
let dev_parts_content = kira_color_bar::color_bar(dev_parts_es, 48); let dev_parts_content = kira_color_bar::color_bar(dev_parts_es, 48);
let dev_parts_content = widget::column![dev_parts_text, dev_parts_content]; let dev_parts_content = widget::column![dev_parts_text, dev_parts_content];
let back_button = widget::button(widget::text(t!("button.back"))).on_press(Message::Back); let back_button = widget::button(widget::text(t!("button.back"))).on_press(Message::Back);
let next_button = widget::button(widget::text(t!("button.next"))).on_press(Message::Next); let next_button = widget::button(widget::text(t!("button.next"))).on_press(Message::Next);
let selected_dev_text = match self.selected_disk_parts { let selected_dev_text = match self.selected_disk_parts {
Some(_) => widget::text(t!("partition.current_dev_layout")), Some(_) => widget::text(t!("partition.current_dev_layout")),
None => widget::text("") None => widget::text(""),
}; };
let selected_dev_content = kira_color_bar::color_bar(self.selected_disk_parts.clone(), 48); let selected_dev_content = kira_color_bar::color_bar(self.selected_disk_parts.clone(), 48);
let selected_dev_content = widget::column![selected_dev_text, selected_dev_content]; let selected_dev_content = widget::column![selected_dev_text, selected_dev_content];
widget::column![ widget::column![
widget::container( widget::container(
widget::column![ widget::column![
@@ -430,8 +432,8 @@ impl PartitionStage {
widget::checkbox(self.secure_boot) widget::checkbox(self.secure_boot)
.label(t!("partition.use_secure_boot")) .label(t!("partition.use_secure_boot"))
.on_toggle(Message::ToggleSecureBoot), .on_toggle(Message::ToggleSecureBoot),
selected_dev_content, selected_dev_content,
dev_parts_content, dev_parts_content,
] ]
.padding(10) .padding(10)
.spacing(10) .spacing(10)
+5 -25
View File
@@ -146,32 +146,12 @@ impl TimeZoneStage {
fn gen_result(&self) -> StageResult { fn gen_result(&self) -> StageResult {
if let Some(time_zone) = &self.selected_zone { if let Some(time_zone) = &self.selected_zone {
StageResult { StageResult::new("time_zone")
name: "time_zone".to_string(), .add_val_string("name", time_zone.to_string())
config: Some(HashMap::from([ .add_val_string("region", time_zone.region.clone())
( .add_val_string("zone", time_zone.zone.clone())
"name".to_string(),
ConfigValue::String(time_zone.to_string()),
),
(
"region".to_string(),
ConfigValue::String(time_zone.region.clone()),
),
(
"zone".to_string(),
ConfigValue::String(time_zone.zone.clone()),
),
])),
resuts: None,
error: None,
}
} else { } else {
StageResult { StageResult::new("time_zone")
name: "time_zone".to_string(),
config: None,
resuts: None,
error: None,
}
} }
} }
+6 -30
View File
@@ -70,37 +70,13 @@ impl WelcomeStage {
fn gen_result(&self) -> StageResult { fn gen_result(&self) -> StageResult {
if let Some(loc) = &self.locale { if let Some(loc) = &self.locale {
StageResult { StageResult::new("welcome")
name: "welcome".to_string(), .add_val_string("loc_code", loc.code.clone())
config: Some(HashMap::from([ .add_val_string("loc_name", loc.name.clone())
(
"loc_code".to_string(),
ConfigValue::String(loc.code.clone()),
),
(
"loc_name".to_string(),
ConfigValue::String(loc.name.clone()),
),
])),
resuts: None,
error: None,
}
} else { } else {
StageResult { StageResult::new("welcome")
name: "welcome".to_string(), .add_val_string("loc_code", "en".to_string())
config: Some(HashMap::from([ .add_val_string("loc_name", "English".to_string())
(
"loc_code".to_string(),
ConfigValue::String("en".to_string()),
),
(
"loc_name".to_string(),
ConfigValue::String("English".to_string()),
),
])),
resuts: None,
error: None,
}
} }
} }