2
0

manifest.rs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. use std::path::PathBuf;
  2. use anyhow::Result;
  3. use serde::Deserialize;
  4. use crate::common::target_arch::TargetArch;
  5. use std::fs;
  6. use toml;
  7. /// The main configuration file for DADK
  8. #[derive(Debug, Clone, Deserialize)]
  9. pub struct DadkManifestFile {
  10. pub metadata: Metadata,
  11. /// A flag variable used to indicate whether
  12. /// the default value function was called during deserialization.
  13. #[serde(skip)]
  14. pub used_default: bool,
  15. }
  16. impl DadkManifestFile {
  17. pub fn load(path: &PathBuf) -> Result<Self> {
  18. // 读取文件内容
  19. let content = fs::read_to_string(path)?;
  20. Self::load_from_str(&content)
  21. }
  22. pub fn load_from_str(content: &str) -> Result<Self> {
  23. // Parse TOML content
  24. let mut manifest_toml: DadkManifestFile = toml::from_str(content)?;
  25. manifest_toml.used_default = check_used_default();
  26. Ok(manifest_toml)
  27. }
  28. }
  29. thread_local! {
  30. /// Global variable to track if default values were used during deserialization.
  31. static USED_DEFAULT: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
  32. }
  33. /// Call this function to set a flag when
  34. /// default values are used during DADK manifest parsing
  35. fn set_used_default() {
  36. USED_DEFAULT.with(|used_default| {
  37. used_default.set(true);
  38. });
  39. }
  40. /// Check if default values were used during deserialization.
  41. fn check_used_default() -> bool {
  42. USED_DEFAULT.with(|used_default| used_default.get())
  43. }
  44. #[derive(Debug, Clone, Deserialize)]
  45. pub struct Metadata {
  46. /// Target processor architecture
  47. pub arch: TargetArch,
  48. /// Rootfs configuration file path
  49. #[serde(default = "default_rootfs_config_path", rename = "rootfs-config")]
  50. pub rootfs_config: PathBuf,
  51. /// Hypervisor configuration file path
  52. #[serde(
  53. default = "default_hypervisor_config_path",
  54. rename = "hypervisor-config"
  55. )]
  56. pub hypervisor_config: PathBuf,
  57. /// Boot configuration file path
  58. #[serde(default = "default_boot_config_path", rename = "boot-config")]
  59. pub boot_config: PathBuf,
  60. /// Sysroot directory path
  61. #[serde(default = "default_sysroot_dir", rename = "sysroot-dir")]
  62. pub sysroot_dir: PathBuf,
  63. /// Root Cache directory path
  64. #[serde(default = "default_cache_root_dir", rename = "cache-root-dir")]
  65. pub cache_root_dir: PathBuf,
  66. /// User configuration directory path
  67. /// 这个字段只是临时用于兼容旧版本,v0.2版本重构完成后会删除
  68. #[deprecated(note = "This field is deprecated and will be removed in DADK 0.2")]
  69. #[serde(default = "default_user_config_dir", rename = "user-config-dir")]
  70. pub user_config_dir: PathBuf,
  71. }
  72. /// Returns the default path for the rootfs configuration file.
  73. fn default_rootfs_config_path() -> PathBuf {
  74. set_used_default();
  75. "config/rootfs.toml".into()
  76. }
  77. /// Returns the default path for the hypervisor configuration file.
  78. fn default_hypervisor_config_path() -> PathBuf {
  79. set_used_default();
  80. "config/hypervisor.toml".into()
  81. }
  82. /// Returns the default path for the boot configuration file.
  83. fn default_boot_config_path() -> PathBuf {
  84. set_used_default();
  85. "config/boot.toml".into()
  86. }
  87. /// Returns the default path for the sysroot directory.
  88. fn default_sysroot_dir() -> PathBuf {
  89. set_used_default();
  90. "bin/sysroot".into()
  91. }
  92. /// Returns the default path for the cache directory.
  93. fn default_cache_root_dir() -> PathBuf {
  94. set_used_default();
  95. "bin/dadk_cache".into()
  96. }
  97. fn default_user_config_dir() -> PathBuf {
  98. set_used_default();
  99. "user/dadk/config".into()
  100. }
  101. #[cfg(test)]
  102. mod tests {
  103. use super::*;
  104. use std::io::Write;
  105. use tempfile::NamedTempFile;
  106. /// Test loading a complete configuration file
  107. #[test]
  108. fn test_full_load_success() -> Result<()> {
  109. let toml_content = r#"
  110. [metadata]
  111. arch = "x86_64"
  112. rootfs-config = "config/rootfs-x86_64.toml"
  113. hypervisor-config = "config/hypervisor-x86_64.toml"
  114. boot-config = "config/boot-x86_64.toml"
  115. sysroot-dir = "bin/sysroot"
  116. cache-root-dir = "bin/dadk_cache"
  117. user-config-dir = "user/dadk/config"
  118. "#;
  119. let mut temp_file = NamedTempFile::new()?;
  120. temp_file.write_all(toml_content.as_bytes())?;
  121. let path = temp_file.path().to_path_buf();
  122. let manifest = DadkManifestFile::load(&path)?;
  123. assert_eq!(manifest.metadata.arch, TargetArch::X86_64);
  124. assert_eq!(
  125. manifest.metadata.rootfs_config,
  126. PathBuf::from("config/rootfs-x86_64.toml")
  127. );
  128. assert_eq!(
  129. manifest.metadata.hypervisor_config,
  130. PathBuf::from("config/hypervisor-x86_64.toml")
  131. );
  132. assert_eq!(
  133. manifest.metadata.boot_config,
  134. PathBuf::from("config/boot-x86_64.toml")
  135. );
  136. assert_eq!(manifest.metadata.sysroot_dir, PathBuf::from("bin/sysroot"));
  137. assert!(!manifest.used_default);
  138. Ok(())
  139. }
  140. /// Test whether an error is reported when the file does not exist.
  141. #[test]
  142. fn test_load_file_not_found() {
  143. let path = PathBuf::from("non_existent_file.toml");
  144. let result = DadkManifestFile::load(&path);
  145. assert!(result.is_err());
  146. }
  147. /// Test whether an error is reported when the TOML content is invalid
  148. #[test]
  149. fn test_load_invalid_toml() -> Result<()> {
  150. let invalid_toml_content = r#"
  151. [metadata
  152. arch = "x86_64"
  153. "#;
  154. let mut temp_file = NamedTempFile::new()?;
  155. temp_file.write_all(invalid_toml_content.as_bytes())?;
  156. let path = temp_file.path().to_path_buf();
  157. let result = DadkManifestFile::load(&path);
  158. assert!(result.is_err());
  159. Ok(())
  160. }
  161. /// Test whether an error is reported when the arch field is invalid
  162. #[test]
  163. fn test_load_invalid_arch_toml() -> Result<()> {
  164. // Invalid arch value
  165. let invalid_toml_content = r#"
  166. [metadata]
  167. arch = "abcde"
  168. "#;
  169. let mut temp_file = NamedTempFile::new()?;
  170. temp_file.write_all(invalid_toml_content.as_bytes())?;
  171. let path = temp_file.path().to_path_buf();
  172. let result = DadkManifestFile::load(&path);
  173. assert!(result.is_err());
  174. Ok(())
  175. }
  176. /// Test whether an error is reported when a required field is missing
  177. #[test]
  178. fn test_load_missing_required_fields() -> Result<()> {
  179. let toml_content = r#"
  180. [metadata]
  181. # arch field is missing
  182. "#;
  183. let mut temp_file = NamedTempFile::new()?;
  184. temp_file.write_all(toml_content.as_bytes())?;
  185. let path = temp_file.path().to_path_buf();
  186. let result = DadkManifestFile::load(&path);
  187. assert!(result.is_err());
  188. Ok(())
  189. }
  190. /// Test whether default values are used
  191. /// when the rootfs_config and other configuration file path fields are not set
  192. #[test]
  193. fn test_load_default_config_path_value() -> Result<()> {
  194. let toml_content = r#"
  195. [metadata]
  196. arch = "x86_64"
  197. "#;
  198. let mut temp_file = NamedTempFile::new()?;
  199. temp_file.write_all(toml_content.as_bytes())?;
  200. let path = temp_file.path().to_path_buf();
  201. let manifest = DadkManifestFile::load(&path)?;
  202. assert_eq!(manifest.used_default, true);
  203. assert_eq!(
  204. manifest.metadata.rootfs_config,
  205. PathBuf::from("config/rootfs.toml")
  206. );
  207. assert_eq!(
  208. manifest.metadata.hypervisor_config,
  209. PathBuf::from("config/hypervisor.toml")
  210. );
  211. assert_eq!(
  212. manifest.metadata.boot_config,
  213. PathBuf::from("config/boot.toml")
  214. );
  215. Ok(())
  216. }
  217. }