diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-01-10 11:10:36 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-01-10 11:10:36 +0000 |
commit | 00ab17722cdb10e02e4e97cf8b098e1cb9e75978 (patch) | |
tree | edee69966fcd4d0a0d27169da3b12d7f9b42d097 /src/resource.rs | |
parent | d373be7f55233e5802840a804bf50f3f9a717252 (diff) | |
download | subplot-00ab17722cdb10e02e4e97cf8b098e1cb9e75978.tar.gz |
resource: Support embedded the resources
We detect the content of the share/ tree at build time and embed files into it.
Symbolic links to files will be followed and the target will be embedded.
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src/resource.rs')
-rw-r--r-- | src/resource.rs | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/resource.rs b/src/resource.rs index 3d65c68..2c1df0a 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -5,7 +5,7 @@ //! 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::io::{self, Cursor, Read}; use std::path::{Path, PathBuf}; use std::sync::Mutex; use structopt::StructOpt; @@ -43,6 +43,8 @@ lazy_static! { }; } +static EMBEDDED_FILES: &[(&str, &[u8])] = include!(concat!(env!("OUT_DIR"), "/embedded_files.inc")); + /// Put a path at the back of the queue to search in pub fn add_search_path<P: AsRef<Path>>(path: P) { SEARCH_PATHS @@ -53,7 +55,7 @@ pub fn add_search_path<P: AsRef<Path>>(path: P) { /// 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>> { +pub fn open<P: AsRef<Path>>(subpath: P) -> io::Result<Box<dyn Read>> { let subpath = subpath.as_ref(); let search_paths = SEARCH_PATHS.lock().expect("Unable to lock SEARCH_PATHS"); let search_paths = search_paths.iter().map(|p| p.as_path()); @@ -71,7 +73,17 @@ pub fn open<P: AsRef<Path>>(subpath: P) -> io::Result<Box<impl Read>> { break; } } - Ok(Box::new(ret?)) + + match ret { + Ok(ret) => Ok(Box::new(ret)), + Err(e) => { + if let Some(data) = EMBEDDED_FILES.iter().find(|e| Path::new(e.0) == subpath) { + Ok(Box::new(Cursor::new(data.1))) + } else { + Err(e) + } + } + } } /// Read a file, honouring search paths established during startup, and |