Skip to main content

datui_lib/
limits.rs

1//! The caps of `[limits]`, read where a reader stops. Set once from the config at
2//! startup; the defaults until then.
3
4use std::sync::RwLock;
5
6use crate::config::LimitsConfig;
7
8static LIMITS: RwLock<LimitsConfig> = RwLock::new(LimitsConfig::DEFAULT);
9
10/// The limits in force.
11pub fn get() -> LimitsConfig {
12    *LIMITS.read().unwrap_or_else(|e| e.into_inner())
13}
14
15/// Use `limits` from now on.
16pub fn set(limits: LimitsConfig) {
17    *LIMITS.write().unwrap_or_else(|e| e.into_inner()) = limits;
18}
19
20/// The end of a note that `left` were left out past `limit`, naming the `[limits]`
21/// key that raises it: `12 records left out: past the first 64 ยท limits.x raises it`.
22pub(crate) fn left_out(left: &str, limit: usize, key: &str) -> String {
23    format!(
24        "{left} left out: past the first {} {} limits.{key} raises it",
25        crate::numfmt::group_chrome(limit),
26        crate::glyphs::get().middot
27    )
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    /// A note of what a cap left out names the key that raises it.
35    #[test]
36    fn a_left_out_note_names_its_key() {
37        let note = left_out("3 records", 64 << 20, "indexed_records");
38        assert!(note.starts_with("3 records left out: past the first 67,108,864"));
39        assert!(note.ends_with("limits.indexed_records raises it"), "{note}");
40    }
41
42    /// `[limits]` in a config file sets the caps; what it leaves out stays the default.
43    #[test]
44    fn the_section_reads_from_toml() {
45        let limits: LimitsConfig =
46            toml::from_str("elf_symbols = 5\nmidi_bytes = \"1MiB\"").unwrap();
47        assert_eq!(limits.elf_symbols, 5);
48        assert_eq!(limits.midi_bytes.bytes(), 1 << 20);
49        assert_eq!(
50            limits.indexed_records,
51            LimitsConfig::DEFAULT.indexed_records
52        );
53    }
54}