1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::io::{stdin, Read};
use crate::errors::{ImportError, MiniViewError};
pub fn import_image_from_stdin_bytes_block() -> Result<image::DynamicImage, MiniViewError> {
let mut buffer = Vec::new();
stdin()
.lock()
.read_to_end(&mut buffer)
.map_err(|_| MiniViewError::FailedToImport(ImportError::OnStdinUnableToRead))?;
if buffer.is_empty() {
return Err(MiniViewError::FailedToImport(
ImportError::OnStdinBytesStreamWasEmpty,
));
}
imagecrate::load_from_memory(&buffer).map_err(|_| {
MiniViewError::FailedToImport(ImportError::OnStdinBytesUnableToGuessOrLoadFormat)
})
}
pub fn read_path_from_stdin_block() -> Result<String, MiniViewError> {
let mut path = String::new();
stdin()
.lock()
.read_to_string(&mut path)
.map_err(|_| MiniViewError::FailedToImport(ImportError::OnStdinUnableToRead))?;
if path.is_empty() {
return Err(MiniViewError::FailedToImport(
ImportError::OnStdinPathWasEmpty,
));
}
Ok(path.trim().to_string())
}