Work on Network stage and toml config.

This commit is contained in:
2026-04-22 14:21:24 +02:00
parent 33d986df3c
commit 01617374a1
9 changed files with 389 additions and 126 deletions
+90 -18
View File
@@ -27,12 +27,15 @@
use iced::widget;
use iced::Alignment;
use std::collections::{HashMap};
use std::path::{PathBuf,Path};
use std::str::FromStr;
use std::process::ExitCode;
use iced::{Element, Task};
use crate::stage::{StageAction, KiraConfig};
use crate::stages::license;
use crate::stages::network;
use crate::stages::welcome;
use crate::stages::welcome::WelcomeStage;
@@ -47,12 +50,15 @@ enum Views {
Start,
Welcome(welcome::WelcomeStage),
License(license::LicenseStage),
Network(stages::network::NetworkStage),
}
enum Message {
Start,
Welcome(welcome::Message),
License(license::Message)
License(license::Message),
Network(stages::network::Message)
}
struct KiraState {
@@ -61,30 +67,70 @@ struct KiraState {
config: KiraConfig,
}
const CONFIG_PATH_STR: &str = "kira_config.toml";
fn load_kira_config(config_path: &str) -> Result<toml::Table, Box<dyn std::error::Error>> {
use std::fs;
use toml::Table;
// 1. Read the file content into a String
let content = fs::read_to_string(config_path)?;
// 2. Parse the string into a Table
let table: Table = content.parse()?;
println!("{:?}", table);
Ok(table)
}
impl KiraState {
fn new(toml_config: toml::Table) -> Self {
Self {
fn boot() -> (Self, Task<Message>) {
(Self {
current_view: Views::Start,
toml_config: toml_config,
toml_config: toml::Table::new(),
config: KiraConfig { config_trail: Vec::new() }
}
}, Task::done(Message::Start))
}
}
// impl KiraState {
// fn new(toml_config: toml::Table) -> Self {
// Self {
// current_view: Views::Start,
// toml_config: toml_config,
// config: KiraConfig { config_trail: Vec::new() }
// }
// }
// }
fn view(k_state: &KiraState) -> Element<'_, Message> {
match &k_state.current_view {
Views::Start => Element::new(widget::space()) ,
Views::Welcome(wellcome_stage) => wellcome_stage.view().map(Message::Welcome),
Views::License(license_stage) => license_stage.view().map(Message::License),
Views::Network(network_stage) => network_stage.view().map(Message::Network),
}
}
fn update(k_state: &mut KiraState, message: Message) -> Task<Message> {
match message {
Message::Start => {
match load_kira_config(CONFIG_PATH_STR) {
Ok(conf) => {
println!("Config loaded!");
k_state.toml_config = conf;
k_state.current_view = Views::Welcome(WelcomeStage::new());
Task::none()
},
Err(ex) => {
println!("Error reading config {}: {}", CONFIG_PATH_STR, ex);
iced::exit()
}
}
},
Message::Welcome(wlc_msg) => {
if let Views::Welcome(wlc_view) = &mut k_state.current_view {
let action = wlc_view.update(wlc_msg);
@@ -101,7 +147,8 @@ fn update(k_state: &mut KiraState, message: Message) -> Task<Message> {
match action {
StageAction::Next(license_res) => {
k_state.config.config_trail.push(license_res);
iced::exit()
k_state.current_view = Views::Network(network::NetworkStage::new(&k_state.toml_config));
Task::none()
},
StageAction::Abort(_) => iced::exit(),
StageAction::Back => iced::exit(),
@@ -111,6 +158,15 @@ fn update(k_state: &mut KiraState, message: Message) -> Task<Message> {
else {
Task::none()
}
},
Message::Network(network_message) => {
if let Views::Network(network_view) = &mut k_state.current_view {
Task::none()
}
else {
Task::none()
}
}
}
}
@@ -119,18 +175,34 @@ fn update(k_state: &mut KiraState, message: Message) -> Task<Message> {
// pub fn main_interface() -> iced::Result {
// iced::run(WellcomeStage::update, WellcomeStage::view)
// }
pub fn main() -> iced::Result {
use toml;
let app_init = || {
let mut k_state = KiraState::new(toml::Table::new());
k_state.current_view = Views::Welcome(WelcomeStage::new());
k_state
};
pub fn main() -> ExitCode {
iced::application(app_init, update, view)
// let app_init = || {
// let mut k_state = KiraState::default()
// k_state.current_view = Views::Welcome(WelcomeStage::new());
// k_stateapp_init
// };
let iced_result = iced::application(KiraState::boot, update, view)
.window(iced::window::Settings {
icon: Some(
iced::window::icon::from_file_data(
include_bytes!("icon.png"),
None,
)
.expect("icon should be a valid PNG"),
),
..Default::default()
})
.centered()
.theme(theme::main_theme())
.run()
.run();
match iced_result {
Ok(()) => ExitCode::SUCCESS,
Err(_) => ExitCode::from(42), // Custom error code
}
}