-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
161 additions
and
26 deletions.
There are no files selected for viewing
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,81 @@ | ||
use std::collections::HashMap; | ||
use std::fs::File; | ||
use std::io::BufReader; | ||
use std::io::Read; | ||
use std::path::PathBuf; | ||
use std::time::SystemTime; | ||
|
||
pub trait FileSystemHandler { | ||
fn open_stream<'a>(&'a self, name: &str) -> Option<Box<dyn Read + 'a>>; | ||
fn get_last_modification(&self, name: &str) -> Option<SystemTime>; | ||
} | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub struct MemoryFileSystem { | ||
files_map: HashMap<String, String>, | ||
} | ||
|
||
impl MemoryFileSystem { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
pub fn add_file(&mut self, filename: String, file_content: String) { | ||
self.files_map.insert(filename, file_content); | ||
} | ||
} | ||
|
||
impl FileSystemHandler for MemoryFileSystem { | ||
fn open_stream<'a>(&'a self, name: &str) -> Option<Box<dyn Read + 'a>> { | ||
if let Some(body) = self.files_map.get(name) { | ||
Some(Box::new(BufReader::new(body.as_bytes()))) | ||
} else { | ||
None | ||
} | ||
} | ||
fn get_last_modification(&self, _name: &str) -> Option<SystemTime> { | ||
Some(SystemTime::now()) | ||
} | ||
} | ||
#[derive(Clone, Debug)] | ||
pub struct RealFileSystem { | ||
root_folder: String, | ||
} | ||
|
||
impl RealFileSystem { | ||
pub fn new(root_folder: String) -> Self { | ||
Self { root_folder } | ||
} | ||
pub fn set_root_folder(&mut self, new_root: String) { | ||
self.root_folder = new_root; | ||
} | ||
pub fn get_root_folder(&self) -> &str { | ||
&self.root_folder | ||
} | ||
pub fn get_full_file_path(&self, name: &str) -> PathBuf { | ||
let mut path = PathBuf::from(&self.root_folder); | ||
path.push(name); | ||
path | ||
} | ||
} | ||
impl FileSystemHandler for RealFileSystem { | ||
fn open_stream<'a>(&'a self, name: &str) -> Option<Box<dyn Read + 'a>> { | ||
let path = self.get_full_file_path(name); | ||
|
||
let file_exists = File::open(path); | ||
if let Ok(file) = file_exists { | ||
Some(Box::new(BufReader::new(file))) | ||
} else { | ||
None | ||
} | ||
} | ||
fn get_last_modification(&self, name: &str) -> Option<SystemTime> { | ||
let path = self.get_full_file_path(name); | ||
let file_exists = File::open(path); | ||
if let Ok(file) = file_exists { | ||
let metadata = file.metadata().unwrap().modified().unwrap(); | ||
Some(metadata) | ||
} else { | ||
None | ||
} | ||
} | ||
} |
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
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,29 @@ | ||
use temple::error::Result; | ||
use temple::value::Value; | ||
use temple::{Context, MemoryFileSystem, RealFileSystem, TemplateEnv}; | ||
|
||
#[test] | ||
pub fn real_filesystem_basic_template() -> Result<()> { | ||
let mut temp_env = TemplateEnv::default(); | ||
let handler = RealFileSystem::new("tests/tests_data".to_string()); | ||
temp_env.add_filesystem_handler(Box::new(handler))?; | ||
let template = temp_env.load_template("simple.j2")?; | ||
let context = Context::default(); | ||
let result = template.render_as_string(context)?; | ||
assert_eq!(result, "Hello World!\n".to_string()); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
pub fn memory_filesystem_basic_template() -> Result<()> { | ||
let mut temp_env = TemplateEnv::default(); | ||
temp_env.add_global("key".to_string(), Value::String("Global value".to_string())); | ||
let mut handler = MemoryFileSystem::new(); | ||
handler.add_file("simple2.j2".to_string(), "Hello Rustaceans!".to_string()); | ||
temp_env.add_filesystem_handler(Box::new(handler))?; | ||
let template = temp_env.load_template("simple2.j2")?; | ||
let context = Context::default(); | ||
let result = template.render_as_string(context)?; | ||
assert_eq!(result, "Hello Rustaceans!".to_string()); | ||
Ok(()) | ||
} |
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 @@ | ||
Hello World! |
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