Skip to main content

quanttide_devops/contract/
mod.rs

1pub mod core;
2/// 契约模型四维架构:Stages / Platforms / Sources / Scopes。
3///
4/// 参考:<https://github.com/quanttide/quanttide-essay-of-devops/blob/main/contract/index.md>
5pub mod error;
6pub mod platform;
7pub mod scope;
8pub mod source;
9pub mod stage;
10pub mod version;
11
12pub use core::{Contract, detect_language_by_files};
13pub use error::ContractError;
14pub use platform::{Pipeline, Platform, Registry, SourceControl};
15pub use scope::{BuildTool, Language, Scope};
16pub use source::{Source, SourceType, VersionSource};
17pub use stage::{Stage, StageBuild, StageRelease, StageTest};
18pub use version::{normalize_version, read_all_config_versions, validate_version};
19
20use std::path::Path;
21
22/// 从 `.quanttide/devops/contract.yaml` 加载契约。
23pub fn load(repo_path: &Path) -> Result<Contract, ContractError> {
24    let path = repo_path.join(".quanttide/devops/contract.yaml");
25    let content = std::fs::read_to_string(&path)?;
26    load_from_str(&content)
27}
28
29/// 从 YAML 字符串解析契约。
30pub fn load_from_str(s: &str) -> Result<Contract, ContractError> {
31    serde_yaml::from_str::<Contract>(s).map_err(|e| ContractError::Parse(e.to_string()))
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn test_load_from_str_valid() {
40        let yaml = r#"
41stages:
42  test:
43    threshold: 80
44
45scopes:
46  cli:
47    dir: src/cli
48"#;
49        let c = load_from_str(yaml).unwrap();
50        assert_eq!(c.scopes.len(), 1);
51        assert_eq!(c.scopes[0].name, "cli");
52        assert_eq!(c.scopes[0].dir, "src/cli");
53        assert_eq!(c.stages.test.threshold, 80.0);
54    }
55
56    #[test]
57    fn test_load_from_str_empty() {
58        let c = load_from_str("").unwrap();
59        assert!(c.scopes.is_empty());
60    }
61
62    #[test]
63    fn test_load_from_str_invalid() {
64        let err = load_from_str("invalid: [").unwrap_err();
65        assert!(err.to_string().contains("解析失败"));
66    }
67}