liblitho 0.2.0

cli tool to flash/clone the images to storage devices
Documentation
//! Fallback platform for unsupported targets.

mod privilege;

use crate::devices::{DeviceError, DeviceInfo, DeviceMount};
use crate::platform::traits::{
    DeviceInventory, DevicePathOps, DeviceReader, DeviceSafety, DeviceWriter, Platform, VolumeOps,
};
use anyhow::Result;
use std::io::{Read, Write};

pub struct UnsupportedPlatform;

impl DeviceInventory for UnsupportedPlatform {
    fn list_storage_devices() -> Result<Vec<DeviceInfo>> {
        Ok(Vec::new())
    }

    fn device_size_bytes(_: &str) -> Option<u64> {
        None
    }

    fn is_removable(_: &str) -> Result<bool> {
        anyhow::bail!("is_removable is not supported on this platform")
    }
}

impl DevicePathOps for UnsupportedPlatform {
    fn validate_whole_disk_path(_: &str) -> Result<(), DeviceError> {
        Err(DeviceError::unsupported(
            "Block device validation is not supported on this platform.",
        ))
    }

    fn paths_equivalent(a: &str, b: &str) -> bool {
        a == b
    }
}

impl DeviceSafety for UnsupportedPlatform {
    fn refuse_system_disk(_: &str) -> Result<(), DeviceError> {
        Ok(())
    }

    fn list_mounts(_: &str) -> Result<Vec<DeviceMount>, DeviceError> {
        Ok(Vec::new())
    }
}

impl VolumeOps for UnsupportedPlatform {
    type Guard = ();

    fn prepare_for_io(_: &str, _: bool) -> Result<Self::Guard, DeviceError> {
        Ok(())
    }

    fn unmount_volumes(_: &str) -> Result<Vec<String>, DeviceError> {
        Err(DeviceError::unsupported(
            "Automatic unmount is not supported on this platform",
        ))
    }
}

pub struct UnsupportedReader;
pub struct UnsupportedWriter;

impl DeviceReader for UnsupportedReader {
    fn open(_: &str) -> Result<Self> {
        anyhow::bail!("Unsupported platform")
    }
    fn device_size(&self) -> Result<u64> {
        anyhow::bail!("Unsupported platform")
    }
}

impl Read for UnsupportedReader {
    fn read(&mut self, _: &mut [u8]) -> std::io::Result<usize> {
        Err(std::io::Error::new(
            std::io::ErrorKind::Unsupported,
            "Unsupported platform",
        ))
    }
}

impl DeviceWriter for UnsupportedWriter {
    fn open(_: &str) -> Result<Self> {
        anyhow::bail!("Unsupported platform")
    }
    fn flush_and_sync(&mut self) -> Result<()> {
        anyhow::bail!("Unsupported platform")
    }
    fn device_size(&self) -> Result<u64> {
        anyhow::bail!("Unsupported platform")
    }
}

impl Write for UnsupportedWriter {
    fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
        Err(std::io::Error::new(
            std::io::ErrorKind::Unsupported,
            "Unsupported platform",
        ))
    }
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

impl Platform for UnsupportedPlatform {
    type Reader = UnsupportedReader;
    type BufferedReader = UnsupportedReader;
    type Writer = UnsupportedWriter;
}