Skip to main content

libdd_capabilities/
env.rs

1// Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/
2// SPDX-License-Identifier: Apache-2.0
3
4//! Environment-variable capability trait and error types.
5//!
6//! Sync: env access is a single map lookup on both native (`std::env`) and
7//! wasm (`process.env`).
8//!
9//! `set` and `unset` are intentionally absent from this trait. libdatadog is
10//! embedded in many kinds of runtime, where mutating the process environment
11//! very much unsafe. Exposing mutation here would make it trivially easy for
12//! callers to corrupt the environment of a multi-threaded host process.
13
14#[derive(Debug, thiserror::Error)]
15pub enum EnvError {
16    #[error("The value of the environment variable `{0}` is not valid UTF-8")]
17    NotUnicode(String),
18    #[error("IO error: {0}")]
19    Io(anyhow::Error),
20}
21
22pub trait EnvCapability: Clone + std::fmt::Debug {
23    fn new() -> Self;
24
25    /// Read an env var.
26    ///
27    /// `Ok(None)` means the variable is unset; `Err(NotUnicode)` means it is
28    /// set but its value is not valid UTF-8. Callers that treat "missing" and
29    /// "invalid" the same should collapse both branches explicitly.
30    fn get(&self, name: &str) -> Result<Option<String>, EnvError>;
31}