stuffr_core/source/
file.rs1use std::fs::File;
2use std::io::Read;
3use std::path::Path;
4
5use super::{SeekRead, Source, SourceCaps};
6use crate::error::Result;
7
8pub struct FileSource {
10 file: File,
11 len: u64,
12}
13
14impl FileSource {
15 pub fn open(path: &Path) -> Result<Self> {
16 Self::from_file(File::open(path)?)
17 }
18
19 pub fn from_file(file: File) -> Result<Self> {
20 let len = file.metadata()?.len();
21 Ok(Self { file, len })
22 }
23}
24
25impl Read for FileSource {
26 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
27 self.file.read(buf)
28 }
29}
30
31impl Source for FileSource {
32 fn caps(&self) -> SourceCaps {
33 SourceCaps {
34 seekable: true,
35 len: Some(self.len),
36 }
37 }
38
39 fn as_seek(&mut self) -> Option<&mut dyn SeekRead> {
40 Some(&mut self.file)
41 }
42}