kayro-cli 0.2.0

Create, build, and run Kayro desktop apps for humans and agents
Documentation
use crate::{args::Architecture, error::CliError};

pub fn resolve_target(
    target: Option<&str>,
    architecture: Option<Architecture>,
) -> Result<Option<String>, CliError> {
    if let Some(target) = target {
        return Ok(Some(target.to_owned()));
    }

    architecture
        .map(|architecture| target_for(architecture, std::env::consts::OS))
        .transpose()
}

fn target_for(architecture: Architecture, os: &str) -> Result<String, CliError> {
    let architecture_name = architecture.rust_name();
    let target = match (os, architecture) {
        ("macos", Architecture::Aarch64 | Architecture::X86_64) => {
            format!("{architecture_name}-apple-darwin")
        }
        ("windows", _) => format!("{architecture_name}-pc-windows-msvc"),
        ("linux", _) => format!("{architecture_name}-unknown-linux-gnu"),
        _ => {
            return Err(CliError::UnsupportedArchitecture {
                arch: architecture_name.to_owned(),
                os: os.to_owned(),
            });
        }
    };
    Ok(target)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn maps_supported_architectures_without_guessing_a_foreign_os() {
        assert_eq!(
            target_for(Architecture::Aarch64, "macos").unwrap(),
            "aarch64-apple-darwin"
        );
        assert_eq!(
            target_for(Architecture::X86_64, "windows").unwrap(),
            "x86_64-pc-windows-msvc"
        );
        assert_eq!(
            target_for(Architecture::I686, "linux").unwrap(),
            "i686-unknown-linux-gnu"
        );
        assert!(target_for(Architecture::I686, "macos").is_err());
    }

    #[test]
    fn explicit_target_is_never_rewritten() {
        assert_eq!(
            resolve_target(Some("aarch64-unknown-linux-musl"), None).unwrap(),
            Some("aarch64-unknown-linux-musl".to_owned())
        );
    }
}