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
use std::path::{Path, PathBuf};
use crate::{File, Marker, DOT_LOCK_SUFFIX};
fn strip_lock_suffix(lock_path: &Path) -> PathBuf {
lock_path.with_extension(lock_path.extension().map_or("".to_string(), |ext| {
let ext = ext.to_string_lossy();
ext.split_at(ext.len().saturating_sub(DOT_LOCK_SUFFIX.len()))
.0
.to_string()
}))
}
impl File {
pub fn with_mut<T>(&mut self, f: impl FnOnce(&mut std::fs::File) -> std::io::Result<T>) -> std::io::Result<T> {
self.inner.with_mut(|tf| f(tf.as_file_mut())).and_then(|res| res)
}
pub fn close(self) -> std::io::Result<Marker> {
Ok(Marker {
inner: self.inner.close()?,
created_from_file: true,
lock_path: self.lock_path,
})
}
pub fn lock_path(&self) -> &Path {
&self.lock_path
}
pub fn resource_path(&self) -> PathBuf {
strip_lock_suffix(&self.lock_path)
}
}
mod io_impls {
use std::{io, io::SeekFrom};
use super::File;
impl io::Write for File {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.with_mut(|f| f.write(buf))?
}
fn flush(&mut self) -> io::Result<()> {
self.inner.with_mut(|f| f.flush())?
}
}
impl io::Seek for File {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.inner.with_mut(|f| f.seek(pos))?
}
}
impl io::Read for File {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.with_mut(|f| f.read(buf))?
}
}
}
impl Marker {
pub fn lock_path(&self) -> &Path {
&self.lock_path
}
pub fn resource_path(&self) -> PathBuf {
strip_lock_suffix(&self.lock_path)
}
}