bakare/src/source.rs

32 lines
694 B
Rust
Raw Normal View History

2018-12-22 16:09:22 +00:00
use std::fs::File;
use std::io::Error;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
2019-01-12 14:39:20 +00:00
2018-12-22 16:09:22 +00:00
use tempfile::tempdir;
use tempfile::TempDir;
2019-01-26 19:27:23 +00:00
pub struct TempSource {
2018-12-22 16:09:22 +00:00
directory: TempDir,
}
2019-01-26 19:27:23 +00:00
impl TempSource {
2018-12-22 16:09:22 +00:00
pub fn new() -> Result<Self, Error> {
Ok(Self { directory: tempdir()? })
}
pub fn write_text_to_file(&self, filename: &str, text: &str) -> Result<(), Error> {
2019-01-26 19:27:23 +00:00
let path = self.file_path(filename);
Ok(File::create(path)?.write_all(text.as_bytes())?)
2018-12-22 16:09:22 +00:00
}
pub fn path(&self) -> &Path {
self.directory.path()
}
2019-01-26 19:27:23 +00:00
pub fn file_path(&self, filename: &str) -> PathBuf {
self.directory.path().join(filename)
}
2018-12-22 16:09:22 +00:00
}