plushie/platform

Cross-target platform utilities.

Provides functions that need different implementations on BEAM vs JavaScript but are used by modules that should compile on both targets. Each function has dual @external annotations.

On BEAM, these delegate to plushie_ffi.erl or the Erlang standard library. On JavaScript, they delegate to plushie_platform_ffi.mjs which uses Node.js APIs where available and degrades gracefully in browser contexts.

BEAM-only operations (Erlang ports, OTP process management) live in plushie/ffi.gleam instead.

Values

pub fn arch_string() -> String

Return the CPU architecture as a string.

Returns one of: "x86_64", "aarch64", or the raw platform value. BEAM: erlang:system_info(system_architecture). JS: process.arch (Node.js) or "unknown" in browser.

pub fn crc32(data: BitArray) -> Int

CRC32 checksum of binary data.

BEAM: erlang:crc32/1. JS: pure lookup-table implementation.

pub fn file_exists(path: String) -> Bool

Check whether a file exists at the given path.

BEAM: filelib:is_regular/1. JS: fs.existsSync (Node.js) or False in browser.

pub fn format_date(
  year: Int,
  month: Int,
  day: Int,
  locale: String,
) -> String

Format a numeric calendar date for common locale families.

JS delegates to Intl.DateTimeFormat. BEAM formats US dates as month/day/year, common European dates as day/month/year, and Japanese, Chinese, Korean, or unknown locales as year-month-day. This is a small date-ordering helper, not full date-time formatting.

pub fn format_number(number: Float, locale: String) -> String

Format a number with common locale separators.

JS delegates to Intl.NumberFormat. BEAM provides dependency-free decimal and grouping separators for common English, German, and French locales, falling back to English-style formatting for unknown locales. This is not currency, percent, unit, or full CLDR formatting.

pub fn get_env(name: String) -> Result(String, Nil)

Get an environment variable.

BEAM: os:getenv/1. JS: process.env[name] (Node.js) or Error(Nil) in browser.

pub fn get_locale() -> String

Return the user’s locale as a normalized tag.

This is a lightweight platform helper, not a full CLDR locale resolver. BEAM reads locale environment variables and JS reads browser or Intl language settings. Missing, C, and POSIX locales fall back to "en-US".

pub fn is_finite_float(value: Float) -> Bool

Return true when a float is finite.

BEAM: local Erlang helper that rejects NaN and infinities. JS: Number.isFinite.

pub fn log_error(message: String) -> Nil

Log at error level.

BEAM: Erlang logger:error/1. JS: console.error.

pub fn log_info(message: String) -> Nil

Log at info level.

BEAM: Erlang logger:info/1. JS: console.info.

pub fn log_warning(message: String) -> Nil

Log at warning level.

BEAM: Erlang logger:warning/1. JS: console.warn.

pub fn math_atan2(y: Float, x: Float) -> Float

Two-argument arctangent function.

pub fn math_cos(x: Float) -> Float

Cosine function.

pub fn math_floor(x: Float) -> Float

Floor function.

pub fn math_pi() -> Float

Pi constant.

pub fn math_pow(base: Float, exponent: Float) -> Float

Power function.

pub fn math_sin(x: Float) -> Float

Sine function.

pub fn math_sqrt(x: Float) -> Float

Square root function.

pub fn monotonic_time_ms() -> Int

Return the current monotonic time in milliseconds.

BEAM: erlang:monotonic_time(millisecond). JS: Math.floor(performance.now()).

Both are monotonically increasing and start from an arbitrary origin. Suitable for measuring elapsed time and coalesce windows, not for wall-clock timestamps.

pub fn platform_string() -> String

Return the platform as a string.

Returns one of: "linux", "darwin", "windows", "unknown". BEAM: :os.type/0. JS: process.platform (Node.js) or "unknown" in browser.

pub fn set_env(name: String, value: String) -> Nil

Set an environment variable.

BEAM: os:putenv/2. JS: process.env[name] = value (Node.js) or no-op in browser.

pub fn sha256_hex(data: BitArray) -> String

Compute SHA-256 hash and return as lowercase hex string.

BEAM: crypto:hash/2. JS: Node.js crypto.createHash. Not available in browser (will throw).

pub fn stable_hash_key(value: a) -> String

Return a stable string key for a value, for deduplication.

Used by the runtime to deduplicate SendAfter timers for the same message value. Not a cryptographic hash. Accepts any type since both BEAM and JS implementations handle arbitrary values.

BEAM: erlang:phash2/1. JS: DJB2 hash of JSON.stringify. Both produce compact numeric strings; collision profiles differ but are adequate for same-platform timer dedup.

pub fn try_call(f: fn() -> a) -> Result(a, dynamic.Dynamic)

Call a function with error handling.

Catches panics and exceptions, returning Ok(value) on success or Error(reason) on failure. Used by the runtime to protect against crashes in user-provided update and view functions.

BEAM: Erlang :try/:catch. JS: try/catch.

pub fn unique_id() -> String

Generate a unique ID string.

BEAM: erlang:unique_integer([monotonic, positive]) converted to string, globally unique within the node lifetime. JS: timestamp-prefixed monotonic counter, unique within a single page/process lifetime.

pub fn unset_env(name: String) -> Nil

Unset an environment variable.

BEAM: os:unsetenv/1. JS: delete process.env[name] (Node.js) or no-op in browser.

pub fn zlib_compress(data: BitArray) -> BitArray

Zlib-compress binary data (deflate).

BEAM: zlib:compress/1. JS: Node.js zlib.deflateSync. Not available in browser (will throw).

Search Document