Debian Codegraph @

src/file_generic.rs

raw symbols SCIP
1 use std::fmt;
2 use std::fs;
3 use std::marker::PhantomData;
4 use std::io;
5 use std::path::{Path, PathBuf};
6
7 use super::*;
8 use crate::file_error::FileError;
9
10 /// Wraps files.
11 ///
12 /// This is a generic implementation that may be replaced by
13 /// platform-specific versions.
14 pub struct File<'a, C: fmt::Debug + Sync + Send>(Generic<fs::File, C>, PathBuf, PhantomData<&'a ()>);
15
16 assert_send_and_sync!(File<'_, C>
17 where C: fmt::Debug);
18
19 impl<'a, C: fmt::Debug + Sync + Send> fmt::Display for File<'a, C> {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 write!(f, "File {:?}", self.1.display())
22 }
23 }
24
25 impl<'a, C: fmt::Debug + Sync + Send> fmt::Debug for File<'a, C> {
26 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27 f.debug_tuple("File")
28 .field(&self.0)
29 .field(&self.1)
30 .finish()
31 }
32 }
33
34 impl<'a> File<'a, ()> {
35 /// Wraps a [`fs::File`].
36 ///
37 /// The given `path` should be the path that has been used to
38 /// obtain `file` from. It is used in error messages to provide
39 /// context to the user.
40 ///
41 /// While this is slightly less convenient than [`Self::open`], it
42 /// allows one to inspect or manipulate the [`fs::File`] object
43 /// before handing it off. For example, one can inspect the
44 /// metadata.
45 pub fn new<P: AsRef<Path>>(file: fs::File, path: P) -> io::Result<Self> {
46 Self::new_with_cookie(file, path.as_ref(), ())
47 }
48
49 /// Opens the given file.
50 pub fn open<P: AsRef<Path>>(path: P) -> io::Result<Self> {
51 Self::with_cookie(path, ())
52 }
53 }
54
55 impl<'a, C: fmt::Debug + Sync + Send> File<'a, C> {
56 /// Like [`Self::new`], but sets a cookie.
57 ///
58 /// The given `path` should be the path that has been used to
59 /// obtain `file` from. It is used in error messages to provide
60 /// context to the user.
61 ///
62 /// While this is slightly less convenient than
63 /// [`Self::with_cookie`], it allows one to inspect or manipulate
64 /// the [`fs::File`] object before handing it off. For example,
65 /// one can inspect the metadata.
66 pub fn new_with_cookie<P: AsRef<Path>>(file: fs::File, path: P, cookie: C)
67 -> io::Result<Self> {
68 let path = path.as_ref();
69 Ok(File(Generic::with_cookie(file, None, cookie), path.into(), PhantomData))
70 }
71
72 /// Like [`Self::open`], but sets a cookie.
73 pub fn with_cookie<P: AsRef<Path>>(path: P, cookie: C) -> io::Result<Self> {
74 let path = path.as_ref();
75 let file = fs::File::open(path).map_err(|e| FileError::new(path, e))?;
76 Self::new_with_cookie(file, path, cookie)
77 }
78 }
79
80 impl<'a, C: fmt::Debug + Sync + Send> io::Read for File<'a, C> {
81 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
82 self.0.read(buf)
83 .map_err(|e| FileError::new(&self.1, e))
84 }
85 }
86
87 impl<'a, C: fmt::Debug + Sync + Send> BufferedReader<C> for File<'a, C> {
88 fn buffer(&self) -> &[u8] {
89 self.0.buffer()
90 }
91
92 fn data(&mut self, amount: usize) -> io::Result<&[u8]> {
93 let path = &self.1;
94 self.0.data(amount)
95 .map_err(|e| FileError::new(path, e))
96 }
97
98 fn data_hard(&mut self, amount: usize) -> io::Result<&[u8]> {
99 let path = &self.1;
100 self.0.data_hard(amount)
101 .map_err(|e| FileError::new(path, e))
102 }
103
104 fn consume(&mut self, amount: usize) -> &[u8] {
105 self.0.consume(amount)
106 }
107
108 fn data_consume(&mut self, amount: usize) -> io::Result<&[u8]> {
109 let path = &self.1;
110 self.0.data_consume(amount)
111 .map_err(|e| FileError::new(path, e))
112 }
113
114 fn data_consume_hard(&mut self, amount: usize) -> io::Result<&[u8]> {
115 let path = &self.1;
116 self.0.data_consume_hard(amount)
117 .map_err(|e| FileError::new(path, e))
118 }
119
120 fn get_mut(&mut self) -> Option<&mut dyn BufferedReader<C>> {
121 None
122 }
123
124 fn get_ref(&self) -> Option<&dyn BufferedReader<C>> {
125 None
126 }
127
128 fn into_inner<'b>(self: Box<Self>) -> Option<Box<dyn BufferedReader<C> + 'b>>
129 where Self: 'b {
130 None
131 }
132
133 fn cookie_set(&mut self, cookie: C) -> C {
134 self.0.cookie_set(cookie)
135 }
136
137 fn cookie_ref(&self) -> &C {
138 self.0.cookie_ref()
139 }
140
141 fn cookie_mut(&mut self) -> &mut C {
142 self.0.cookie_mut()
143 }
144 }
145
146 #[cfg(test)]
147 mod test {
148 use super::*;
149
150 #[test]
151 fn error_contains_path() {
152 let p = "/i/do/not/exist";
153 let e = File::open(p).unwrap_err();
154 assert!(e.to_string().contains(p));
155 }
156 }