From 5fea07c7687791c00eeed19e26169fe5fee0993f Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 10 Oct 2025 09:26:03 +0300 Subject: [PATCH] treewide: move rom's parser logic to cognos Signed-off-by: NotAShelf Change-Id: I6a6a6964960ab80b5555a6cca7b20e11c8ac0ea2 --- cognos/src/lib.rs | 11 ++++++- cognos/src/state.rs | 40 +++++++++++++++++++++--- rom/src/cli.rs | 3 +- rom/src/display.rs | 22 -------------- rom/src/monitor.rs | 5 +-- rom/src/state.rs | 74 +++------------------------------------------ rom/src/update.rs | 21 ++----------- 7 files changed, 57 insertions(+), 119 deletions(-) diff --git a/cognos/src/lib.rs b/cognos/src/lib.rs index d9eda4d..1bda354 100644 --- a/cognos/src/lib.rs +++ b/cognos/src/lib.rs @@ -9,4 +9,13 @@ pub use aterm::{ parse_drv_file, }; pub use internal_json::{Actions, Activities, Id, Verbosity}; -pub use state::{BuildInfo, BuildStatus, Derivation, Host, State}; +pub use state::{BuildInfo, BuildStatus, Derivation, Host, OutputName, State, ProgressState}; + +/// Process a list of actions and return the resulting state +pub fn process_actions(actions: Vec) -> State { + let mut state = State { progress: ProgressState::JustStarted }; + for action in actions { + state.imbibe(action); + } + state +} diff --git a/cognos/src/state.rs b/cognos/src/state.rs index 56139e8..27c38fa 100644 --- a/cognos/src/state.rs +++ b/cognos/src/state.rs @@ -18,12 +18,14 @@ pub enum BuildStatus { Failed, } -pub enum Progress { +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum ProgressState { JustStarted, InputReceived, Finished, } +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum OutputName { Out, Doc, @@ -36,9 +38,37 @@ pub enum OutputName { Other(String), } +impl OutputName { + #[must_use] + pub fn parse(name: &str) -> Self { + match name.to_lowercase().as_str() { + "out" => Self::Out, + "doc" => Self::Doc, + "dev" => Self::Dev, + "bin" => Self::Bin, + "info" => Self::Info, + "lib" => Self::Lib, + "man" => Self::Man, + "dist" => Self::Dist, + _ => Self::Other(name.to_string()), + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum Host { - Local, - Host(String), + Localhost, + Remote(String), +} + +impl Host { + #[must_use] + pub fn name(&self) -> &str { + match self { + Self::Localhost => "localhost", + Self::Remote(name) => name, + } + } } pub struct Derivation { @@ -65,9 +95,9 @@ pub struct Dependencies { // #[derive(Default)] pub struct State { - progress: Progress, + pub progress: ProgressState, } impl State { pub fn imbibe(&mut self, update: Actions) {} -} +} \ No newline at end of file diff --git a/rom/src/cli.rs b/rom/src/cli.rs index 937ebd9..64b1765 100644 --- a/rom/src/cli.rs +++ b/rom/src/cli.rs @@ -6,6 +6,7 @@ use std::{ }; use clap::Parser; +use cognos::ProgressState; #[derive(Debug, Parser)] #[command(name = "rom", version, about = "ROM - A Nix build output monitor")] @@ -582,7 +583,7 @@ fn run_monitored_command( if !silent { if has_activity - || state.progress_state != crate::state::ProgressState::JustStarted + || state.progress_state != ProgressState::JustStarted { // Clear any previous timer display if last_timer_display.is_some() { diff --git a/rom/src/display.rs b/rom/src/display.rs index 999d2d6..4859d46 100644 --- a/rom/src/display.rs +++ b/rom/src/display.rs @@ -66,7 +66,6 @@ pub struct Display { writer: W, config: DisplayConfig, last_lines: usize, - using_alt_screen: bool, } struct TreeNode { @@ -80,7 +79,6 @@ impl Display { writer, config, last_lines: 0, - using_alt_screen: false, }) } @@ -860,27 +858,7 @@ impl Display { lines } - fn is_active_or_has_active_descendants( - &self, - state: &State, - drv_id: DerivationId, - ) -> bool { - if let Some(info) = state.get_derivation_info(drv_id) { - match info.build_status { - BuildStatus::Building(_) => return true, - BuildStatus::Failed { .. } => return true, - _ => {}, - } - // Check children - for input in &info.input_derivations { - if self.is_active_or_has_active_descendants(state, input.derivation) { - return true; - } - } - } - false - } fn build_active_forest( &self, diff --git a/rom/src/monitor.rs b/rom/src/monitor.rs index e188c00..8d93e15 100644 --- a/rom/src/monitor.rs +++ b/rom/src/monitor.rs @@ -12,6 +12,7 @@ use crate::{ types::{Config, InputMode}, update, }; +use cognos::Host; /// Main monitor that processes nix output and displays progress pub struct Monitor { @@ -152,7 +153,7 @@ impl Monitor { let build_info = crate::state::BuildInfo { start: now, - host: crate::state::Host::Localhost, + host: Host::Localhost, estimate: None, activity_id: None, }; @@ -175,7 +176,7 @@ impl Monitor { let transfer = crate::state::TransferInfo { start: now, - host: crate::state::Host::Localhost, + host: Host::Localhost, activity_id: 0, // No activity ID in human mode bytes_transferred: 0, total_bytes: None, diff --git a/rom/src/state.rs b/rom/src/state.rs index 3cfae00..4ae014c 100644 --- a/rom/src/state.rs +++ b/rom/src/state.rs @@ -6,7 +6,7 @@ use std::{ time::{Duration, SystemTime}, }; -use cognos::Id; +use cognos::{Host, Id, OutputName, ProgressState}; use indexmap::IndexMap; /// Unique identifier for store paths @@ -18,35 +18,9 @@ pub type DerivationId = usize; /// Unique identifier for activities pub type ActivityId = Id; -/// Overall progress state -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum ProgressState { - JustStarted, - InputReceived, - Finished, -} -/// Build host information -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum Host { - Localhost, - Remote(String), -} -impl Host { - #[must_use] - pub const fn is_local(&self) -> bool { - matches!(self, Self::Localhost) - } - #[must_use] - pub fn name(&self) -> &str { - match self { - Self::Localhost => "localhost", - Self::Remote(name) => name, - } - } -} /// Store path representation #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -111,36 +85,8 @@ impl Derivation { } } -/// Output name for derivations -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum OutputName { - Out, - Doc, - Dev, - Bin, - Info, - Lib, - Man, - Dist, - Other(String), -} -impl OutputName { - #[must_use] - pub fn parse(name: &str) -> Self { - match name.to_lowercase().as_str() { - "out" => Self::Out, - "doc" => Self::Doc, - "dev" => Self::Dev, - "bin" => Self::Bin, - "info" => Self::Info, - "lib" => Self::Lib, - "man" => Self::Man, - "dist" => Self::Dist, - _ => Self::Other(name.to_string()), - } - } -} + /// Transfer information (download/upload) #[derive(Debug, Clone)] @@ -603,7 +549,7 @@ impl State { // Create output set let mut output_set = HashSet::new(); for output in outputs { - output_set.insert(parse_output_name(&output)); + output_set.insert(OutputName::parse(&output)); } // Add to parent's input derivations @@ -728,19 +674,7 @@ pub fn current_time() -> f64 { .as_secs_f64() } -fn parse_output_name(name: &str) -> OutputName { - match name { - "out" => OutputName::Out, - "doc" => OutputName::Doc, - "dev" => OutputName::Dev, - "bin" => OutputName::Bin, - "info" => OutputName::Info, - "lib" => OutputName::Lib, - "man" => OutputName::Man, - "dist" => OutputName::Dist, - _ => OutputName::Other(name.to_string()), - } -} + #[cfg(test)] mod tests { diff --git a/rom/src/update.rs b/rom/src/update.rs index 728555c..a9a2290 100644 --- a/rom/src/update.rs +++ b/rom/src/update.rs @@ -1,6 +1,6 @@ //! State update logic for processing nix messages -use cognos::{Actions, Activities, Id, Verbosity}; +use cognos::{Actions, Activities, Host, Id, ProgressState, Verbosity}; use tracing::{debug, trace}; use crate::state::{ @@ -14,10 +14,7 @@ use crate::state::{ DerivationId, FailType, FailedBuildInfo, - Host, InputDerivation, - OutputName, - ProgressState, State, StorePath, StorePathId, @@ -887,17 +884,5 @@ pub fn finish_state(state: &mut State) { } } -/// Parse output name string to `OutputName` enum -fn parse_output_name(s: &str) -> Option { - match s { - "out" => Some(OutputName::Out), - "doc" => Some(OutputName::Doc), - "dev" => Some(OutputName::Dev), - "bin" => Some(OutputName::Bin), - "info" => Some(OutputName::Info), - "lib" => Some(OutputName::Lib), - "man" => Some(OutputName::Man), - "dist" => Some(OutputName::Dist), - other => Some(OutputName::Other(other.to_string())), - } -} + +