headless_lms_utils/
icu4x.rs1use std::fmt::Debug;
2
3use anyhow::Context;
4use once_cell::sync::OnceCell;
5
6static ICU4X_POSTCARD: OnceCell<Vec<u8>> = OnceCell::new();
7
8#[derive(Clone, Copy)]
9pub struct Icu4xBlob {
10 postcard: &'static [u8],
11}
12
13impl Icu4xBlob {
14 pub fn new(icu4x_postcard_path: &str) -> anyhow::Result<Self> {
15 let data = ICU4X_POSTCARD.get_or_try_init(|| {
16 let postcard = std::fs::read(icu4x_postcard_path).with_context(|| {
17 format!("could not read icu4x postcard from {icu4x_postcard_path}")
18 })?;
19 anyhow::Ok(postcard)
20 })?;
21
22 Ok(Self {
23 postcard: data.as_slice(),
24 })
25 }
26
27 pub fn try_from_env() -> anyhow::Result<Self> {
29 let icu4x_postcard_path =
30 std::env::var("ICU4X_POSTCARD_PATH").context("ICU4X_POSTCARD_PATH not defined")?;
31 Self::new(&icu4x_postcard_path)
32 }
33
34 pub fn get(&self) -> &'static [u8] {
35 self.postcard
36 }
37}
38
39impl Debug for Icu4xBlob {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 write!(f, "Icu4xBlob")
42 }
43}