use anyhow::{bail, Context, Result};
pub const RELEASE_REPO: &str = "ConradIrwin/corgi";
pub fn platform(host: &str) -> Result<&'static str> {
let platform = match host {
"aarch64-apple-darwin" => "macos-arm64",
_ => bail!(
"Corgi has no pinned libclang artifact for host `{host}` \
(only aarch64-apple-darwin is published today)"
),
};
Ok(platform)
}
pub fn is_supported(host: &str) -> bool {
platform(host).is_ok()
}
pub fn tag() -> String {
format!("libclang-{}", crate::zig::VERSION)
}
pub fn asset_name(host: &str) -> Result<String> {
Ok(format!("libclang-{}.tar.zst", platform(host)?))
}
pub fn url(host: &str) -> Result<String> {
Ok(format!(
"https://github.com/{RELEASE_REPO}/releases/download/{}/{}",
tag(),
asset_name(host)?
))
}
pub fn sha256_url(host: &str) -> Result<String> {
Ok(format!("{}.sha256", url(host)?))
}
pub const DYLIB_RELATIVE: &str = "lib/libclang.dylib";
pub fn parse_sha256_sidecar(contents: &str) -> Result<String> {
contents
.split_whitespace()
.next()
.map(str::to_string)
.with_context(|| format!("empty .sha256 sidecar: {contents:?}"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tag_follows_zig_version() {
assert_eq!(tag(), format!("libclang-{}", crate::zig::VERSION));
}
#[test]
fn arm_mac_is_supported_with_release_urls() {
let host = "aarch64-apple-darwin";
assert!(is_supported(host));
let url = url(host).unwrap();
assert_eq!(
url,
format!(
"https://github.com/ConradIrwin/corgi/releases/download/libclang-{}/libclang-macos-arm64.tar.zst",
crate::zig::VERSION
)
);
assert_eq!(sha256_url(host).unwrap(), format!("{url}.sha256"));
}
#[test]
fn intel_mac_is_unsupported_for_now() {
assert!(!is_supported("x86_64-apple-darwin"));
assert!(platform("x86_64-apple-darwin").is_err());
assert!(url("x86_64-apple-darwin").is_err());
}
#[test]
fn linux_is_unsupported() {
assert!(!is_supported("aarch64-unknown-linux-gnu"));
}
#[test]
fn sidecar_reads_the_shasum_hash() {
let hash = "a".repeat(64);
assert_eq!(
parse_sha256_sidecar(&format!("{hash} libclang-macos-arm64.tar.zst\n")).unwrap(),
hash
);
}
#[test]
fn sidecar_rejects_empty() {
assert!(parse_sha256_sidecar("").is_err());
assert!(parse_sha256_sidecar(" \n").is_err());
}
}