Files
solidcraft/crates/kernel/src/backend.rs
T
jyvikoandClaude Opus 5.5 5f743c46b7 Wave 86/509: kernel: one switch, SOLIDCRAFT_KERNEL, picks the kernel for every operation (and 3 more)
4 commits, squashed; the originals stay reachable from tag archive/merge-main-develop.

- d672cfa01 jyviko: kernel: one switch, SOLIDCRAFT_KERNEL, picks the kernel for every operation
- f35f06d98 jyviko: kernel: a revolution that is no analytic surface goes into SMLib as its B-spline
- a6ca4e0dd jyviko: AGENTS.md: one switch, SOLIDCRAFT_KERNEL, in place of the booleans-only note
- 51211ad1f jyviko: solidcraft-smlib: offset faces, thicken, and NoRoom for walls with no room

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
2026-10-21 07:35:00 -04:00

52 lines
2.1 KiB
Rust

//! Which kernel this process runs its operations on: truck (the default) or SMLib.
//!
//! The setting is the environment variable `SOLIDCRAFT_KERNEL`, read once per process: `truck`
//! (or unset) or `smlib`. Every operation that has an SMLib path asks [`backend`]. SMLib needs a
//! build with the kernel's `smlib` feature; set without it, or set to anything else, every such
//! operation fails with an error that says so rather than quietly running truck, so a comparison
//! run can never compare truck with itself.
use crate::{KernelError, Result};
/// The environment variable that picks the kernel.
pub const KERNEL_ENV: &str = "SOLIDCRAFT_KERNEL";
/// A kernel operations run on.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Backend {
Truck,
Smlib,
}
impl Backend {
/// The backend a setting names; `None` (unset) is truck.
pub fn parse(setting: Option<&str>) -> std::result::Result<Backend, String> {
match setting.map(str::trim) {
None | Some("") | Some("truck") => Ok(Backend::Truck),
Some("smlib") if cfg!(feature = "smlib") => Ok(Backend::Smlib),
Some("smlib") => Err(format!("{KERNEL_ENV}=smlib: not supported yet: this build has no SMLib (feature `smlib`)")),
Some(other) => Err(format!("{KERNEL_ENV}={other}: expected `truck` or `smlib`")),
}
}
}
/// The kernel this process runs on ([`KERNEL_ENV`], read on first use).
pub fn backend() -> Result<Backend> {
static SETTING: std::sync::OnceLock<std::result::Result<Backend, String>> = std::sync::OnceLock::new();
SETTING.get_or_init(|| Backend::parse(std::env::var(KERNEL_ENV).ok().as_deref())).clone().map_err(KernelError::Invalid)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_setting_names_a_backend_or_is_refused() {
assert_eq!(Backend::parse(None), Ok(Backend::Truck));
assert_eq!(Backend::parse(Some("truck")), Ok(Backend::Truck));
assert_eq!(Backend::parse(Some(" ")), Ok(Backend::Truck));
assert_eq!(Backend::parse(Some("smlib")).is_ok(), cfg!(feature = "smlib"));
assert!(Backend::parse(Some("occt")).is_err());
}
}