liblitho 0.2.0

cli tool to flash/clone the images to storage devices
Documentation
//! Platform abstraction: traits + Linux / Windows / macOS implementations.
//!
//! See `docs/platform-segregation-plan.md`.

pub mod traits;

#[cfg(target_os = "linux")]
pub mod linux;
#[cfg(target_os = "macos")]
pub mod macos;
#[cfg(target_os = "windows")]
pub mod windows;
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
pub mod unsupported;

pub use traits::{
    DeviceInventory, DevicePathOps, DeviceReader, DeviceSafety, DeviceWriter, ElevationCapability,
    Platform, PrivilegeOps, VolumeOps,
};

#[cfg(target_os = "linux")]
pub type Active = linux::LinuxPlatform;
#[cfg(target_os = "windows")]
pub type Active = windows::WindowsPlatform;
#[cfg(target_os = "macos")]
pub type Active = macos::MacPlatform;
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
pub type Active = unsupported::UnsupportedPlatform;

use anyhow::Result;

/// Factory for platform-specific device open handles.
pub struct PlatformDevice;

impl PlatformDevice {
    pub fn new_reader(device_path: &str) -> Result<Box<dyn DeviceReader>> {
        Ok(Box::new(Active::open_reader(device_path)?))
    }

    /// Clone / verify reads (buffered I/O).
    pub fn new_clone_reader(device_path: &str) -> Result<Box<dyn DeviceReader>> {
        Self::new_verify_reader(device_path)
    }

    pub fn new_verify_reader(device_path: &str) -> Result<Box<dyn DeviceReader>> {
        Ok(Box::new(Active::open_verify_reader(device_path)?))
    }

    pub fn new_writer(device_path: &str) -> Result<Box<dyn DeviceWriter>> {
        Ok(Box::new(Active::open_writer(device_path)?))
    }
}

// --- Privilege façade (stable free functions for TUI/CLI) ---

/// Short name of the elevation backend (for logs).
pub fn elevation_method() -> &'static str {
    Active::elevation_method()
}

/// True when the process already has the privileges required for block I/O.
pub fn is_elevated() -> bool {
    Active::is_elevated()
}

/// Inspect whether elevation can be requested on this machine.
pub fn elevation_capability() -> ElevationCapability {
    Active::elevation_capability()
}

/// Check that elevation can proceed before tearing down the terminal.
pub fn validate_elevation_prerequisites() -> Result<(), String> {
    Active::validate_elevation_prerequisites()
}

/// Relaunch with the same flash/clone selections, elevated.
pub fn relaunch_elevated(mode: &str, device: &str, image: &str) -> Result<(), String> {
    Active::relaunch_elevated(mode, device, image)
}

/// Body lines for the elevation confirmation dialog.
pub fn elevation_dialog_lines() -> [&'static str; 3] {
    Active::elevation_dialog_lines()
}

/// Portable helper for session logging (`geteuid` on Linux/macOS, `0` elsewhere).
pub fn current_euid_or_0() -> u32 {
    Active::current_euid_or_0()
}

/// CLI args passed to an elevated relaunch of `litho-tui`.
pub(crate) fn elevated_cli_args(mode: &str, device: &str, image: &str) -> Vec<String> {
    vec![
        "--mode".into(),
        mode.into(),
        "--device".into(),
        device.into(),
        "--image".into(),
        image.into(),
    ]
}