Skip to main content

cursive_image/image/
source.rs

1use super::stream::*;
2
3use std::{fs::*, io, path::*};
4
5//
6// ImageSource
7//
8
9/// Image source.
10pub enum ImageSource {
11    /// Owned data, optionally compressed.
12    Owned(Vec<u8>, bool),
13
14    /// Local file, optionally compressed.
15    LocalFile(PathBuf, bool),
16
17    /// Stream.
18    Stream(ImageStreamRef),
19}
20
21impl ImageSource {
22    /// Into owned.
23    pub fn into_owned(&mut self) -> io::Result<()> {
24        match self {
25            Self::LocalFile(path, compressed) => *self = Self::Owned(read(path)?, *compressed),
26
27            Self::Stream(stream) => {
28                let (mut reader, compressed) = stream.open()?;
29                let mut data = Vec::default();
30                reader.read_to_end(&mut data)?;
31                *self = Self::Owned(data, compressed);
32            }
33
34            _ => {}
35        }
36
37        Ok(())
38    }
39}