Skip to main content

dort_core/
platform.rs

1use crate::{Error, Result};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
5#[serde(rename_all = "lowercase")]
6pub enum Platform {
7    Macos,
8    Ubuntu,
9}
10
11impl Platform {
12    pub fn detect() -> Result<Self> {
13        match std::env::consts::OS {
14            "macos" => Ok(Self::Macos),
15            "linux" => {
16                let text = std::fs::read_to_string("/etc/os-release").map_err(|e| {
17                    crate::error::io_error("read OS identity", "/etc/os-release", e)
18                })?;
19                Self::from_os_release(&text)
20            }
21            other => Err(Error::Invalid(format!(
22                "unsupported operating system: {other}"
23            ))),
24        }
25    }
26
27    pub fn from_os_release(text: &str) -> Result<Self> {
28        let id = text.lines().find_map(|line| {
29            line.strip_prefix("ID=")
30                .map(|v| v.trim().trim_matches(['"', '\'']))
31        });
32        match id {
33            Some("ubuntu") => Ok(Self::Ubuntu),
34            _ => Err(Error::Invalid(
35                "only Ubuntu Linux is supported (ID=ubuntu required)".into(),
36            )),
37        }
38    }
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
42#[serde(rename_all = "snake_case")]
43pub enum Architecture {
44    X86_64,
45    Aarch64,
46}
47
48impl Architecture {
49    pub fn detect() -> Result<Self> {
50        match std::env::consts::ARCH {
51            "x86_64" => Ok(Self::X86_64),
52            "aarch64" => Ok(Self::Aarch64),
53            arch => Err(Error::Invalid(format!("unsupported architecture: {arch}"))),
54        }
55    }
56}