Skip to main content

kernel_apis/
util.rs

1//! The clock seam and small checks the modules share. The JavaScript semantics they use are in [`jsrt`].
2
3use serde_json::Value;
4use std::sync::Arc;
5
6/// Milliseconds since the epoch (`Date.now()`); injected so fixtures can fix it.
7pub type Clock = Arc<dyn Fn() -> i64 + Send + Sync>;
8
9pub fn system_clock() -> Clock {
10    Arc::new(jsrt::date::now_ms)
11}
12
13pub(crate) fn object(entries: Vec<(&str, Value)>) -> Value {
14    Value::Object(
15        entries
16            .into_iter()
17            .map(|(key, value)| (key.to_string(), value))
18            .collect(),
19    )
20}
21
22/// A versioned definition reference, `<id>@<version>` (`/^[a-z][a-z0-9-]*@[0-9]+$/`).
23pub(crate) fn valid_definition(text: &str) -> bool {
24    let Some((id, version)) = text.split_once('@') else {
25        return false;
26    };
27    id.as_bytes().first().is_some_and(u8::is_ascii_lowercase)
28        && id
29            .bytes()
30            .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
31        && !version.is_empty()
32        && version.bytes().all(|b| b.is_ascii_digit())
33}