[pve-devel] [PATCH installer 2/6] auto, tui: move low-level installer message struct to common crate

Christoph Heiss c.heiss at proxmox.com
Thu May 16 15:39:32 CEST 2024


Signed-off-by: Christoph Heiss <c.heiss at proxmox.com>
---
 .../src/bin/proxmox-auto-installer.rs         |  8 ++--
 proxmox-auto-installer/src/utils.rs           | 23 ---------
 proxmox-installer-common/src/setup.rs         | 23 +++++++++
 .../src/views/install_progress.rs             | 48 +++++--------------
 4 files changed, 38 insertions(+), 64 deletions(-)

diff --git a/proxmox-auto-installer/src/bin/proxmox-auto-installer.rs b/proxmox-auto-installer/src/bin/proxmox-auto-installer.rs
index 9fcec1e..e607a9c 100644
--- a/proxmox-auto-installer/src/bin/proxmox-auto-installer.rs
+++ b/proxmox-auto-installer/src/bin/proxmox-auto-installer.rs
@@ -8,14 +8,12 @@ use std::{
 };
 
 use proxmox_installer_common::setup::{
-    installer_setup, read_json, spawn_low_level_installer, LocaleInfo, RuntimeInfo, SetupInfo,
+    installer_setup, read_json, spawn_low_level_installer, LocaleInfo, LowLevelMessage,
+    RuntimeInfo, SetupInfo,
 };
 
 use proxmox_auto_installer::{
-    answer::Answer,
-    log::AutoInstLogger,
-    udevinfo::UdevInfo,
-    utils::{parse_answer, LowLevelMessage},
+    answer::Answer, log::AutoInstLogger, udevinfo::UdevInfo, utils::parse_answer,
 };
 
 static LOGGER: AutoInstLogger = AutoInstLogger;
diff --git a/proxmox-auto-installer/src/utils.rs b/proxmox-auto-installer/src/utils.rs
index 202ad41..7f3688d 100644
--- a/proxmox-auto-installer/src/utils.rs
+++ b/proxmox-auto-installer/src/utils.rs
@@ -380,26 +380,3 @@ pub fn parse_answer(
     }
     Ok(config)
 }
-
-#[derive(Clone, Debug, Deserialize, PartialEq)]
-#[serde(tag = "type", rename_all = "lowercase")]
-pub enum LowLevelMessage {
-    #[serde(rename = "message")]
-    Info {
-        message: String,
-    },
-    Error {
-        message: String,
-    },
-    Prompt {
-        query: String,
-    },
-    Finished {
-        state: String,
-        message: String,
-    },
-    Progress {
-        ratio: f32,
-        text: String,
-    },
-}
diff --git a/proxmox-installer-common/src/setup.rs b/proxmox-installer-common/src/setup.rs
index 64d05af..0c62bc3 100644
--- a/proxmox-installer-common/src/setup.rs
+++ b/proxmox-installer-common/src/setup.rs
@@ -558,3 +558,26 @@ where
         _ => Err(de::Error::custom("could not find file system: {de_fs}")),
     }
 }
+
+#[derive(Clone, Debug, Deserialize, PartialEq)]
+#[serde(tag = "type", rename_all = "lowercase")]
+pub enum LowLevelMessage {
+    #[serde(rename = "message")]
+    Info {
+        message: String,
+    },
+    Error {
+        message: String,
+    },
+    Prompt {
+        query: String,
+    },
+    Finished {
+        state: String,
+        message: String,
+    },
+    Progress {
+        ratio: f32,
+        text: String,
+    },
+}
diff --git a/proxmox-tui-installer/src/views/install_progress.rs b/proxmox-tui-installer/src/views/install_progress.rs
index 6453426..11b12f0 100644
--- a/proxmox-tui-installer/src/views/install_progress.rs
+++ b/proxmox-tui-installer/src/views/install_progress.rs
@@ -4,7 +4,6 @@ use cursive::{
     views::{Dialog, DummyView, LinearLayout, PaddedView, ProgressBar, TextView},
     CbSink, Cursive,
 };
-use serde::Deserialize;
 use std::{
     fs::File,
     io::{BufRead, BufReader, Write},
@@ -14,7 +13,7 @@ use std::{
 };
 
 use crate::{abort_install_button, prompt_dialog, InstallerState};
-use proxmox_installer_common::setup::{spawn_low_level_installer, InstallConfig};
+use proxmox_installer_common::setup::{spawn_low_level_installer, InstallConfig, LowLevelMessage};
 
 pub struct InstallProgressView {
     view: PaddedView<LinearLayout>,
@@ -105,7 +104,7 @@ impl InstallProgressView {
                     continue;
                 }
 
-                let msg = match serde_json::from_str::<UiMessage>(&line) {
+                let msg = match serde_json::from_str::<LowLevelMessage>(&line) {
                     Ok(msg) => msg,
                     Err(err) => {
                         // Not a fatal error, so don't abort the installation by returning
@@ -116,17 +115,17 @@ impl InstallProgressView {
                 };
 
                 let result = match msg.clone() {
-                    UiMessage::Info { message } => cb_sink.send(Box::new(|siv| {
+                    LowLevelMessage::Info { message } => cb_sink.send(Box::new(|siv| {
                         siv.add_layer(Dialog::info(message).title("Information"));
                     })),
-                    UiMessage::Error { message } => cb_sink.send(Box::new(|siv| {
+                    LowLevelMessage::Error { message } => cb_sink.send(Box::new(|siv| {
                         siv.add_layer(Dialog::info(message).title("Error"));
                     })),
-                    UiMessage::Prompt { query } => cb_sink.send({
+                    LowLevelMessage::Prompt { query } => cb_sink.send({
                         let writer = writer.clone();
                         Box::new(move |siv| Self::show_prompt(siv, &query, writer))
                     }),
-                    UiMessage::Progress { ratio, text } => {
+                    LowLevelMessage::Progress { ratio, text } => {
                         counter.set((ratio * 100.).floor() as usize);
                         cb_sink.send(Box::new(move |siv| {
                             siv.call_on_name(Self::PROGRESS_TEXT_VIEW_ID, |v: &mut TextView| {
@@ -134,7 +133,7 @@ impl InstallProgressView {
                             });
                         }))
                     }
-                    UiMessage::Finished { state, message } => {
+                    LowLevelMessage::Finished { state, message } => {
                         counter.set(100);
                         cb_sink.send(Box::new(move |siv| {
                             siv.call_on_name(Self::PROGRESS_TEXT_VIEW_ID, |v: &mut TextView| {
@@ -241,29 +240,6 @@ impl ViewWrapper for InstallProgressView {
     cursive::wrap_impl!(self.view: PaddedView<LinearLayout>);
 }
 
-#[derive(Clone, Debug, Deserialize, PartialEq)]
-#[serde(tag = "type", rename_all = "lowercase")]
-enum UiMessage {
-    #[serde(rename = "message")]
-    Info {
-        message: String,
-    },
-    Error {
-        message: String,
-    },
-    Prompt {
-        query: String,
-    },
-    Finished {
-        state: String,
-        message: String,
-    },
-    Progress {
-        ratio: f32,
-        text: String,
-    },
-}
-
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -292,7 +268,7 @@ mod tests {
             let mut line = String::new();
             reader.read_line(&mut line).expect("a line");
 
-            match serde_json::from_str::<UiMessage>(&line) {
+            match serde_json::from_str::<LowLevelMessage>(&line) {
                 Ok(msg) => Some(msg),
                 Err(err) => panic!("unexpected error: '{err}'"),
             }
@@ -300,7 +276,7 @@ mod tests {
 
         assert_eq!(
             next_msg(),
-            Some(UiMessage::Prompt {
+            Some(LowLevelMessage::Prompt {
                 query: "Reply anything?".to_owned()
             }),
         );
@@ -314,7 +290,7 @@ mod tests {
 
         assert_eq!(
             next_msg(),
-            Some(UiMessage::Info {
+            Some(LowLevelMessage::Info {
                 message: "Test Message - got ok".to_owned()
             }),
         );
@@ -322,7 +298,7 @@ mod tests {
         for i in (1..=1000).step_by(3) {
             assert_eq!(
                 next_msg(),
-                Some(UiMessage::Progress {
+                Some(LowLevelMessage::Progress {
                     ratio: (i as f32) / 1000.,
                     text: format!("foo {i}"),
                 }),
@@ -331,7 +307,7 @@ mod tests {
 
         assert_eq!(
             next_msg(),
-            Some(UiMessage::Finished {
+            Some(LowLevelMessage::Finished {
                 state: "ok".to_owned(),
                 message: "Installation finished - reboot now?".to_owned(),
             }),
-- 
2.44.0





More information about the pve-devel mailing list