use serde::Deserialize;
use crate::common::target_arch::TargetArch;
#[derive(Debug, Clone, Deserialize)]
pub struct UbootConfig {
#[serde(rename = "download-url", default = "default_download_url")]
pub download_url: String,
#[serde(rename = "version", default = "default_version")]
pub version: String,
#[serde(rename = "path-prefix", default = "default_path_prefix")]
pub path_prefix: String,
}
impl Default for UbootConfig {
fn default() -> Self {
Self {
download_url: Self::DEFAULT_DOWNLOAD_URL.to_string(),
version: Self::DEFAULT_VERSION.to_string(),
path_prefix: Self::DEFAULT_PATH_PREFIX.to_string(),
}
}
}
impl UbootConfig {
const DEFAULT_DOWNLOAD_URL: &'static str =
"https://mirrors.dragonos.org.cn/pub/third_party/u-boot";
const DEFAULT_VERSION: &'static str = "v2023.10";
const DEFAULT_PATH_PREFIX: &'static str = "bin/uboot/";
pub fn full_download_url(&self, target_arch: TargetArch) -> String {
let arch_str: &str = target_arch.into();
format!(
"{}/u-boot-{}-{}.tar.xz",
self.download_url, self.version, arch_str
)
}
}
fn default_download_url() -> String {
UbootConfig::DEFAULT_DOWNLOAD_URL.to_string()
}
fn default_version() -> String {
UbootConfig::DEFAULT_VERSION.to_string()
}
fn default_path_prefix() -> String {
UbootConfig::DEFAULT_PATH_PREFIX.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_uboot_config() {
let config = UbootConfig::default();
assert_eq!(config.download_url, UbootConfig::DEFAULT_DOWNLOAD_URL);
assert_eq!(config.version, "v2023.10");
assert_eq!(config.path_prefix, "bin/uboot/");
}
#[test]
fn test_full_download_url_riscv64() {
let config = UbootConfig::default();
let url = config.full_download_url(TargetArch::RiscV64);
assert_eq!(
url,
"https://mirrors.dragonos.org.cn/pub/third_party/u-boot/u-boot-v2023.10-riscv64.tar.xz"
);
}
#[test]
fn test_empty_toml_deserialization() {
let toml_content = "";
let config: UbootConfig = toml::from_str(toml_content).unwrap();
assert_eq!(config.download_url, UbootConfig::DEFAULT_DOWNLOAD_URL);
assert_eq!(config.version, "v2023.10");
assert_eq!(config.path_prefix, "bin/uboot/");
}
}