cursive-image 0.0.6

Image view for the Cursive TUI library
Documentation
use super::stream::*;

use std::{fs::*, io, path::*};

//
// ImageSource
//

/// Image source.
pub enum ImageSource {
    /// Owned data, optionally compressed.
    Owned(Vec<u8>, bool),

    /// Local file, optionally compressed.
    LocalFile(PathBuf, bool),

    /// Stream.
    Stream(ImageStreamRef),
}

impl ImageSource {
    /// Into owned.
    pub fn into_owned(&mut self) -> io::Result<()> {
        match self {
            Self::LocalFile(path, compressed) => *self = Self::Owned(read(path)?, *compressed),

            Self::Stream(stream) => {
                let (mut reader, compressed) = stream.open()?;
                let mut data = Vec::default();
                reader.read_to_end(&mut data)?;
                *self = Self::Owned(data, compressed);
            }

            _ => {}
        }

        Ok(())
    }
}