1
use std::{ fs::OpenOptions, path::PathBuf };
2

            
3
use anyhow::Result;
4

            
5
use crate::RwBuilder;
6

            
7
/// Type for building readers and writers on top of a file handle.
8
/// It is itself an `RwBuilder`, but can't be created through one.
9
/// This is why we call it a source.
10
#[derive(Debug)]
11
pub struct Builder {
12
    /// The path of the file for which readers and writers can be created.
13
    path: PathBuf,
14
}
15

            
16
impl Builder {
17
    /// Factory function to create a builder holding on to a file path
18
    #[must_use]
19
1
    pub const fn new(path: PathBuf) -> Self {
20
1
        Self { path }
21
1
    }
22
}
23

            
24
impl RwBuilder for Builder {
25
    type Reader = std::fs::File;
26
    type Writer = std::fs::File;
27

            
28
1
    fn reader(&self) -> Result<Self::Reader> {
29
1
        let options = OpenOptions::new().read(true).open(&self.path)?;
30
1
        Ok(options)
31
1
    }
32

            
33
1
    fn writer(&self) -> Result<Self::Writer> {
34
1
        let options = OpenOptions::new().create(true).truncate(true).write(true).open(&self.path)?;
35
1
        Ok(options)
36
1
    }
37
}