diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-01-09 14:40:01 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-01-09 14:40:01 +0000 |
commit | 41e1968d4f546360c3f19b87a311cd7f6b8ebadc (patch) | |
tree | 1242c7cc8fb7aeab7dab0455859e2ebc982a5e20 /src/resource.rs | |
parent | 8a1a3baba5f7c7bc42de3b67a021cdb9fb8e1bbc (diff) | |
download | subplot-41e1968d4f546360c3f19b87a311cd7f6b8ebadc.tar.gz |
resource: Step one of VFS support, redirect all opens
This redirects all file reading via the new resource module
which will be used to control where files come from.
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src/resource.rs')
-rw-r--r-- | src/resource.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/resource.rs b/src/resource.rs new file mode 100644 index 0000000..15a239e --- /dev/null +++ b/src/resource.rs @@ -0,0 +1,25 @@ +//! Resources for subplot. +//! +//! This module encapsulates a mechanism for subplot to find resource files. +//! Resource files come from a number of locations, such as embedded into the +//! binary, from template paths given on the CLI, or from paths built into the +//! binary and present on disk. + +use std::io::{self, Read}; +use std::path::Path; + +/// Open a file for reading, honouring search paths established during +/// startup, and falling back to potentially embedded file content +pub fn open<P: AsRef<Path>>(subpath: P) -> io::Result<Box<impl Read>> { + let subpath = subpath.as_ref(); + Ok(Box::new(std::fs::File::open(subpath)?)) +} + +/// Read a file, honouring search paths established during startup, and +/// falling back to potentially embedded file content +pub fn read_as_string<P: AsRef<Path>>(subpath: P) -> io::Result<String> { + let mut f = open(subpath)?; + let mut ret = String::new(); + f.read_to_string(&mut ret)?; + Ok(ret) +} |