Use cfg_if for embed-data checks

This commit is contained in:
Johannes Altmanninger
2025-10-27 15:14:48 +01:00
parent 9845074a53
commit d4390c2fad
3 changed files with 94 additions and 88 deletions

View File

@@ -10,6 +10,7 @@
use crate::wchar::{L, WString, wstr};
use crate::wchar_ext::WExt;
use crate::wutil::{FileId, INVALID_FILE_ID, file_id_for_path};
use cfg_if::cfg_if;
use lru::LruCache;
#[cfg(feature = "embed-data")]
use rust_embed::RustEmbed;
@@ -44,15 +45,17 @@ pub struct Autoload {
#[folder = "share/"]
pub struct Asset;
#[cfg(feature = "embed-data")]
pub fn has_asset(cmd: &str) -> bool {
Asset::get(cmd).is_some()
}
#[cfg(not(feature = "embed-data"))]
pub fn has_asset(_cmd: &str) -> bool {
false
}
cfg_if!(
if #[cfg(feature = "embed-data")] {
pub fn has_asset(cmd: &str) -> bool {
Asset::get(cmd).is_some()
}
} else {
pub fn has_asset(_cmd: &str) -> bool {
false
}
}
);
#[derive(Clone, Copy, Eq, PartialEq)]
enum AssetDir {
@@ -347,10 +350,13 @@ fn check(
// Check hits.
if let Some(value) = self.known_files.get(cmd) {
#[cfg(feature = "embed-data")]
let embedded = matches!(value.file, AutoloadableFileInfo::EmbeddedPath(_));
#[cfg(not(feature = "embed-data"))]
let embedded = false;
cfg_if!(
if #[cfg(feature = "embed-data")] {
let embedded = matches!(value.file, AutoloadableFileInfo::EmbeddedPath(_));
} else {
let embedded = false;
}
);
if allow_stale
|| embedded
|| Self::is_fresh(value.last_checked, Self::current_timestamp())

View File

@@ -17,6 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
use cfg_if::cfg_if;
use fish::{
ast,
builtins::{
@@ -170,36 +171,35 @@ fn source_config_in_directory(parser: &Parser, dir: &wstr) -> bool {
/// Parse init files. exec_path is the path of fish executable as determined by argv[0].
fn read_init(parser: &Parser, paths: &ConfigPaths) {
#[cfg(feature = "embed-data")]
{
let emfile = Asset::get("config.fish").expect("Embedded file not found");
let src = bytes2wcstring(&emfile.data);
parser.libdata_mut().within_fish_init = true;
let fname: Arc<WString> = Arc::new(L!("embedded:config.fish").into());
let ret = parser.eval_file_wstr(src, fname, &IoChain::new(), None);
parser.libdata_mut().within_fish_init = false;
if let Err(msg) = ret {
eprintf!("%s", msg);
cfg_if!(
if #[cfg(feature = "embed-data")] {
let emfile = Asset::get("config.fish").expect("Embedded file not found");
let src = bytes2wcstring(&emfile.data);
parser.libdata_mut().within_fish_init = true;
let fname: Arc<WString> = Arc::new(L!("embedded:config.fish").into());
let ret = parser.eval_file_wstr(src, fname, &IoChain::new(), None);
parser.libdata_mut().within_fish_init = false;
if let Err(msg) = ret {
eprintf!("%s", msg);
}
} else {
let datapath = bytes2wcstring(paths.data.as_os_str().as_bytes());
if !source_config_in_directory(parser, &datapath) {
// If we cannot read share/config.fish, our internal configuration,
// something is wrong.
// That also means that our functions won't be found,
// and so any config we get would almost certainly be broken.
let escaped_pathname = escape(&datapath);
FLOGF!(
error,
"Fish cannot find its asset files in '%s'.\n\
Refusing to read configuration because of this.",
escaped_pathname,
);
return;
}
}
}
#[cfg(not(feature = "embed-data"))]
{
let datapath = bytes2wcstring(paths.data.as_os_str().as_bytes());
if !source_config_in_directory(parser, &datapath) {
// If we cannot read share/config.fish, our internal configuration,
// something is wrong.
// That also means that our functions won't be found,
// and so any config we get would almost certainly be broken.
let escaped_pathname = escape(&datapath);
FLOGF!(
error,
"Fish cannot find its asset files in '%s'.\n\
Refusing to read configuration because of this.",
escaped_pathname,
);
return;
}
}
);
source_config_in_directory(
parser,

View File

@@ -10,6 +10,7 @@
use crate::reader::reader_in_interactive_read;
use crate::tty_handoff::{get_scroll_content_up_capability, xtversion};
use crate::wutil::{Error, waccess, wbasename, wdirname, wrealpath};
use cfg_if::cfg_if;
use libc::F_OK;
macro_rules! str_enum {
@@ -471,25 +472,24 @@ pub fn status(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -> B
));
return Err(STATUS_INVALID_ARGS);
}
#[cfg(feature = "embed-data")]
{
let arg = crate::common::wcs2bytes(args[0]);
let arg = std::str::from_utf8(&arg).unwrap();
let Some(emfile) = crate::autoload::Asset::get(arg).or_else(|| Docs::get(arg))
else {
cfg_if!(
if #[cfg(not(feature = "embed-data"))] {
streams
.err
.appendln(sprintf!(NO_EMBEDDED_FILES_MSG.localize(), cmd));
return Err(STATUS_CMD_ERROR);
};
let src = bytes2wcstring(&emfile.data);
streams.out.append(src);
return Ok(SUCCESS);
}
#[cfg(not(feature = "embed-data"))]
{
streams
.err
.appendln(sprintf!(NO_EMBEDDED_FILES_MSG.localize(), cmd));
return Err(STATUS_CMD_ERROR);
}
} else {
let arg = crate::common::wcs2bytes(args[0]);
let arg = std::str::from_utf8(&arg).unwrap();
let Some(emfile) = crate::autoload::Asset::get(arg).or_else(|| Docs::get(arg))
else {
return Err(STATUS_CMD_ERROR);
};
let src = bytes2wcstring(&emfile.data);
streams.out.append(src);
return Ok(SUCCESS);
}
);
}
c @ STATUS_LIST_FILES => {
if args.len() > 1 {
@@ -502,37 +502,37 @@ pub fn status(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -> B
));
return Err(STATUS_INVALID_ARGS);
}
#[cfg(feature = "embed-data")]
{
use crate::util::wcsfilecmp_glob;
let mut paths = vec![];
let arg = crate::common::wcs2bytes(args.first().unwrap_or(&L!("")));
let arg = std::str::from_utf8(&arg).unwrap();
for path in crate::autoload::Asset::iter().chain(Docs::iter()) {
if arg.is_empty() || path.starts_with(arg) {
paths.push(bytes2wcstring(path.as_bytes()));
cfg_if!(
if #[cfg(not(feature = "embed-data"))] {
streams
.err
.appendln(sprintf!(NO_EMBEDDED_FILES_MSG.localize(), cmd));
return Err(STATUS_CMD_ERROR);
} else {
use crate::util::wcsfilecmp_glob;
let mut paths = vec![];
let arg = crate::common::wcs2bytes(args.first().unwrap_or(&L!("")));
let arg = std::str::from_utf8(&arg).unwrap();
for path in crate::autoload::Asset::iter().chain(Docs::iter()) {
if arg.is_empty() || path.starts_with(arg) {
paths.push(bytes2wcstring(path.as_bytes()));
}
}
paths.sort_by(|a, b| wcsfilecmp_glob(a, b));
for path in &paths {
streams.out.appendln(path);
}
if !paths.is_empty() {
return Ok(SUCCESS);
} else {
return Err(STATUS_CMD_ERROR);
}
}
paths.sort_by(|a, b| wcsfilecmp_glob(a, b));
for path in &paths {
streams.out.appendln(path);
}
if !paths.is_empty() {
return Ok(SUCCESS);
} else {
return Err(STATUS_CMD_ERROR);
}
}
#[cfg(not(feature = "embed-data"))]
{
streams
.err
.appendln(sprintf!(NO_EMBEDDED_FILES_MSG.localize(), cmd));
return Err(STATUS_CMD_ERROR);
}
);
}
c @ STATUS_TEST_TERMINAL_FEATURE => {
if args.len() != 1 {