use std::{
fmt::{self, Debug, Formatter},
path::Path,
};
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct File {
path: &'static str,
contents: &'static [u8],
metadata: Option<crate::Metadata>,
}
impl File {
pub const fn new(path: &'static str, contents: &'static [u8]) -> Self {
File {
path,
contents,
metadata: None,
}
}
pub fn path(&self) -> &'static Path {
Path::new(self.path)
}
pub fn contents(&self) -> &'static [u8] {
self.contents
}
pub fn contents_utf8(&self) -> Option<&'static str> {
std::str::from_utf8(self.contents()).ok()
}
pub const fn with_metadata(self, metadata: crate::Metadata) -> Self {
let File { path, contents, .. } = self;
File {
path,
contents,
metadata: Some(metadata),
}
}
pub fn metadata(&self) -> Option<&crate::Metadata> {
self.metadata.as_ref()
}
}
impl Debug for File {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("File")
.field("path", &self.path)
.field("contents", &format_args!("<{} bytes>", self.contents.len()))
.field("metadata", &self.metadata)
.finish()
}
}