-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add layered file provider for loose file and cook server loading
- Loading branch information
Showing
10 changed files
with
1,048 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::path::PathBuf; | ||
|
||
use anyhow::Result; | ||
use serde::Deserialize; | ||
|
||
use super::{FileInfo, FileProvider}; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct PlainFileProviderConfig { | ||
path: PathBuf, | ||
globs: Vec<String>, | ||
} | ||
impl PlainFileProviderConfig { | ||
pub fn build(self) -> Result<PlainFileProvider> { | ||
PlainFileProvider::new(self) | ||
} | ||
} | ||
|
||
pub struct PlainFileProvider { | ||
path: PathBuf, | ||
globs: Vec<glob::Pattern>, | ||
} | ||
impl PlainFileProvider { | ||
pub fn new(config: PlainFileProviderConfig) -> Result<Self> { | ||
Ok(Self { | ||
path: config.path, | ||
globs: config | ||
.globs | ||
.iter() | ||
.flat_map(|g| glob::Pattern::new(g)) | ||
.collect(), | ||
}) | ||
} | ||
} | ||
|
||
impl FileProvider for PlainFileProvider { | ||
fn matches(&self, path: &str) -> bool { | ||
self.globs.iter().any(|g| g.matches(path)) | ||
} | ||
fn get_file_info(&mut self, path: &str) -> Result<FileInfo> { | ||
match std::fs::File::open(self.path.join(path)) { | ||
Ok(file) => { | ||
let meta = file.metadata()?; | ||
Ok(FileInfo { | ||
file_exists: true, | ||
read_only: meta.permissions().readonly(), | ||
size: meta.len() as i64, | ||
timestamp: 0, // TODO timestamp | ||
access_timestamp: 0, // TODO timestamp | ||
}) | ||
} | ||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(FileInfo { | ||
file_exists: false, | ||
..Default::default() | ||
}), | ||
Err(e) => Err(e.into()), | ||
} | ||
} | ||
fn get_file(&mut self, path: &str) -> Result<Vec<u8>> { | ||
Ok(std::fs::read(self.path.join(path))?) | ||
} | ||
} |
Oops, something went wrong.