gausplat_loader/source/file/
mod.rs

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! File source module.

pub mod files;

pub use crate::error::Error;
pub use crate::function::Opener;
pub use files::*;

use std::{
    fs,
    io::{self, BufReader, BufWriter, Read, Seek, Write},
    ops::{Deref, DerefMut},
    path::{Path, PathBuf},
};

/// Duplex file stream.
#[derive(Clone, Debug, PartialEq)]
pub struct File<F> {
    /// Inner stream.
    pub inner: F,
    /// File path.
    pub path: PathBuf,
}

impl File<fs::File> {
    /// Truncate the file.
    ///
    /// It sets the length of the file to `0`.
    #[inline]
    pub fn truncate(&mut self) -> Result<&mut Self, Error> {
        self.inner.set_len(0)?;
        Ok(self)
    }
}

impl<R: Read> File<R> {
    /// Read all bytes from the file.
    #[inline]
    pub fn read_all(&mut self) -> Result<Vec<u8>, Error> {
        let mut bytes = vec![];
        BufReader::new(&mut self.inner).read_to_end(&mut bytes)?;
        Ok(bytes)
    }
}

impl<W: Write> File<W> {
    /// Write all bytes to the file.
    #[inline]
    pub fn write_all(
        &mut self,
        bytes: &[u8],
    ) -> Result<&mut Self, Error> {
        BufWriter::new(&mut self.inner).write_all(bytes)?;
        Ok(self)
    }
}

impl<R: Read> Read for File<R> {
    #[inline]
    fn read(
        &mut self,
        buf: &mut [u8],
    ) -> io::Result<usize> {
        self.inner.read(buf)
    }
}

impl<S: Seek> Seek for File<S> {
    #[inline]
    fn seek(
        &mut self,
        pos: io::SeekFrom,
    ) -> io::Result<u64> {
        self.inner.seek(pos)
    }
}

impl<W: Write> Write for File<W> {
    #[inline]
    fn write(
        &mut self,
        buf: &[u8],
    ) -> io::Result<usize> {
        self.inner.write(buf)
    }

    #[inline]
    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }
}

impl<F: Default> Default for File<F> {
    #[inline]
    fn default() -> Self {
        Self {
            inner: Default::default(),
            path: Default::default(),
        }
    }
}

impl<F> Deref for File<F> {
    type Target = F;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<F> DerefMut for File<F> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl Opener for File<fs::File> {
    /// The file is opened in read and write mode.
    ///
    /// This won't truncate the previous file.
    /// One should call [`File::truncate`] to do so.
    #[inline]
    fn open(path: impl AsRef<Path>) -> Result<Self, Error> {
        let inner = fs::OpenOptions::new()
            .create(true)
            .read(true)
            .truncate(false)
            .write(true)
            .open(&path)?;
        let path = path.as_ref().to_owned();

        Ok(Self { inner, path })
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn open_and_read_all() {
        use super::*;

        let source = "examples/data/hello-world/ascii.txt";
        let mut file = File::open(source).unwrap();

        let target = b"Hello, World!";
        let output = file.read_all().unwrap();
        assert_eq!(output, target);
    }

    #[test]
    fn open_on_symlink() {
        use super::*;

        let source = "examples/data/hello-world.symlink/ascii.symlink.txt";
        let mut file = File::open(source).unwrap();

        let target = b"Hello, World!";
        let output = file.read_all().unwrap();
        assert_eq!(output, target);
    }

    #[test]
    fn open_on_directory() {
        use super::*;

        let source = "examples/data/hello-world/";
        File::open(source).unwrap_err();
    }

    #[test]
    fn read() {
        use super::*;

        let source = &include_bytes!("../../../examples/data/hello-world/ascii.txt")[..];
        let mut file = File {
            path: Default::default(),
            inner: std::io::Cursor::new(source),
        };

        let target = source;
        let output = file.read_all().unwrap();
        assert_eq!(output, target);

        let target = 0;
        let output = file.read(&mut [][..]).unwrap();
        assert_eq!(output, target);
    }

    #[test]
    fn truncate() {
        use super::*;
        use std::env::temp_dir;

        let path = temp_dir().join("gausplat-loader::tests::truncate.tmp");
        let target = true;
        let output = File::open(&path)
            .unwrap()
            .truncate()
            .unwrap()
            .read_all()
            .unwrap()
            .is_empty();
        assert_eq!(output, target);
    }

    #[test]
    fn write_and_rewind() {
        use super::*;

        let source = &include_bytes!("../../../examples/data/hello-world/ascii.txt")[..];
        let mut file = File::<std::io::Cursor<Vec<u8>>>::default();

        let target = source;
        file.write_all(source).unwrap();
        file.rewind().unwrap();
        let output = file.deref().to_owned().into_inner();
        assert_eq!(output, target);
        let output = file.deref_mut().to_owned().into_inner();
        assert_eq!(output, target);

        let target = 0;
        let output = file.write(&[][..]).unwrap();
        assert_eq!(output, target);

        file.flush().unwrap();
    }
}