Skip to main content

sim_run_core/
codec_boot.rs

1use crate::{CliBoot, LibSourceSpec};
2
3/// Codec name used when `--codec` is omitted.
4pub const DEFAULT_CODEC_NAME: &str = "lisp";
5
6/// Returns the selected codec name for a boot session.
7pub fn boot_codec_name(boot: &CliBoot) -> &str {
8    boot.codec.as_deref().unwrap_or(DEFAULT_CODEC_NAME)
9}
10
11/// Returns the library symbol for a codec name.
12pub fn codec_lib_symbol(name: &str) -> String {
13    format!("codec/{name}")
14}
15
16pub(crate) fn explicit_codec_source_index(boot: &CliBoot, symbol: &str) -> Option<usize> {
17    boot.loads
18        .iter()
19        .position(|source| source_matches_codec_symbol(source, symbol))
20}
21
22fn source_matches_codec_symbol(source: &LibSourceSpec, symbol: &str) -> bool {
23    matches!(
24        source,
25        LibSourceSpec::Symbol(candidate) | LibSourceSpec::Host(candidate) if candidate == symbol
26    )
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn defaults_to_lisp_codec_symbol() {
35        let boot = CliBoot::default();
36
37        assert_eq!(boot_codec_name(&boot), "lisp");
38        assert_eq!(codec_lib_symbol(boot_codec_name(&boot)), "codec/lisp");
39    }
40
41    #[test]
42    fn override_selects_named_codec_symbol() {
43        let boot = CliBoot {
44            codec: Some("json".to_owned()),
45            ..CliBoot::default()
46        };
47
48        assert_eq!(boot_codec_name(&boot), "json");
49        assert_eq!(codec_lib_symbol(boot_codec_name(&boot)), "codec/json");
50    }
51}