rama_utils/include_dir/
file.rs1use super::Metadata;
2use std::{
3 fmt::{self, Debug, Formatter},
4 path::Path,
5};
6
7#[derive(Clone, PartialEq, Eq)]
9pub struct File<'a> {
10 path: &'a str,
11 contents: &'a [u8],
12 metadata: Option<super::Metadata>,
13}
14
15impl<'a> File<'a> {
16 #[must_use]
18 pub const fn new(path: &'a str, contents: &'a [u8]) -> Self {
19 File {
20 path,
21 contents,
22 metadata: None,
23 }
24 }
25
26 #[must_use]
29 pub fn path(&self) -> &'a Path {
30 Path::new(self.path)
31 }
32
33 #[must_use]
35 pub fn contents(&self) -> &[u8] {
36 self.contents
37 }
38
39 #[must_use]
41 pub fn contents_utf8(&self) -> Option<&str> {
42 std::str::from_utf8(self.contents()).ok()
43 }
44}
45
46impl<'a> File<'a> {
47 #[must_use]
49 pub const fn with_metadata(self, metadata: Metadata) -> Self {
50 let File { path, contents, .. } = self;
51
52 File {
53 path,
54 contents,
55 metadata: Some(metadata),
56 }
57 }
58
59 #[must_use]
61 pub fn metadata(&self) -> Option<&Metadata> {
62 self.metadata.as_ref()
63 }
64}
65
66impl<'a> Debug for File<'a> {
67 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
68 let File {
69 path,
70 contents,
71 metadata,
72 } = self;
73
74 let mut d = f.debug_struct("File");
75
76 d.field("path", path)
77 .field("contents", &format!("<{} bytes>", contents.len()));
78 d.field("metadata", metadata);
79
80 d.finish()
81 }
82}