genpac 0.1.0

Sandbox for Gentoo ebuild development using bubblewrap
// Copyright (C) 2023 Gokul Das B
// SPDX-License-Identifier: GPL-3.0-or-later
//! Global parameters
//!
//! This module prepares and stores all the parameters that are relevant to many other
//! submodules. This includes:
//! - CLI options
//! - Configured options (from config.toml)
//!   - ID Map: Ready for mapping
//!   - Workspace: Ready after verification

use cfg::Vars;
pub use chroots::{ChrootUnverified, ChrootVerified};
pub use cli::Cli;
pub use idmaps::IDMapItem;
use idmaps::IDMaps;
use indicatif::MultiProgress;
use workspace::Workspace;

mod cfg;
mod chroots;
mod cli;
mod idmaps;
mod progress;
mod workspace;

// Typestates for Globals
pub trait LogStatus {}
pub trait ConfigStatus {}

/// Container for global parameters
///
/// This is a *typestate* container. The container passes through 3 states in its lifecycle:
/// 1. **New:** The container is new with only CLI options available
/// 2. **LogReady:** After logger intialization. Config data is still missing
/// 3. **Final:** After config file parsing. All global variables are ready
pub struct Globals<T1: LogStatus, T2: ConfigStatus> {
    cli: CliOptions,
    multi: T1,
    cfg: T2,
}

// Type aliases for the 3 states
type GlobalsNew = Globals<NotReady, NotReady>;
type GlobalsLogReady = Globals<MultiProgress, NotReady>;
pub type GlobalsFinal = Globals<MultiProgress, ConfigOptions>;

/// Options obtained from CLI arguments
struct CliOptions {
    dry_run: bool,
    log_level: log::LevelFilter,
}

/// Empty struct indicating non-readiness of any data slot
pub struct NotReady;
impl LogStatus for NotReady {}
impl ConfigStatus for NotReady {}

impl LogStatus for MultiProgress {}

/// Options obtained from config file
pub struct ConfigOptions {
    workspace: Workspace,
    config_text: String,
    vars: Vars,
    idmaps: IDMaps,
}
impl ConfigStatus for ConfigOptions {}

impl<T1: LogStatus, T2: ConfigStatus> Globals<T1, T2> {
    #[inline]
    pub fn dry_run(&self) -> bool {
        self.cli.dry_run
    }
}