Skip to main content

dtcs/
lib.rs

1//! Reference implementation of the Data Transformation Contract Standard (DTCS).
2//!
3//! [`SPEC.md`](../SPEC.md) at the repository root is the authoritative normative
4//! specification. This crate implements the pipeline through contract analysis:
5//!
6//! ```text
7//! DTCS Document → Parser → Canonical Object Model → Validator → Diagnostics
8//!                                              └→ Analyzer → Analysis reports
9//! ```
10//!
11//! # Example
12//!
13//! ```
14//! use dtcs::{parse, validate, DocumentFormat};
15//!
16//! let yaml = br#"
17//! dtcsVersion: "1.0.0"
18//! id: "example"
19//! name: "Example"
20//! version: "0.2.0"
21//! inputs:
22//!   - id: "in"
23//!     schema:
24//!       fields:
25//!         - name: "value"
26//!           type: "string"
27//!           nullable: false
28//! outputs:
29//!   - id: "out"
30//!     schema:
31//!       fields:
32//!         - name: "value"
33//!           type: "string"
34//!           nullable: false
35//! lineage:
36//!   mappings:
37//!     - output: "out"
38//!       inputs: ["in"]
39//! "#;
40//!
41//! let result = parse(yaml, DocumentFormat::Yaml);
42//! let contract = result.contract.expect("parse succeeded");
43//! let report = validate(&contract);
44//! assert!(report.is_valid());
45//! ```
46
47/// DTCS specification version this crate targets.
48pub const SPEC_VERSION: &str = "1.0.0-draft";
49
50pub mod compatibility;
51pub mod diagnostics;
52pub mod lineage;
53pub mod metadata;
54pub mod model;
55pub mod parser;
56pub mod plan;
57pub mod validation;
58pub mod versioning;
59
60#[cfg(feature = "cli")]
61pub mod cli;
62
63#[cfg(feature = "python")]
64mod python;
65
66pub use compatibility::{
67    analyze as analyze_compatibility, analyze_evolution, ChangeCategory, ComparisonScope,
68    CompatibilityLevel, CompatibilityReport, EvolutionReport,
69};
70pub use diagnostics::{
71    codes, inspect_contract, Diagnostic, DiagnosticCategory, DiagnosticReport, DiagnosticStage,
72    Severity, ValidationReport,
73};
74pub use lineage::{analyze as analyze_lineage, LineageAnalysisReport, LineageGovernance};
75pub use model::{
76    parse_logical_type, type_compatible, LogicalType, TransformationContract, TypeCompatibility,
77    TypeParseError,
78};
79pub use parser::{parse, parse_file, parse_json, parse_yaml, DocumentFormat, ParseResult};
80pub use validation::{validate, ValidationPhase};
81
82/// Parse and validate a DTCS document in one step.
83#[must_use]
84pub fn parse_and_validate(content: &[u8], format: DocumentFormat) -> ValidationReport {
85    parse(content, format).validate()
86}
87
88impl TransformationContract {
89    /// Parse a contract from YAML text.
90    pub fn from_yaml(content: &str) -> ParseResult {
91        parse(content.as_bytes(), DocumentFormat::Yaml)
92    }
93
94    /// Parse a contract from JSON text.
95    pub fn from_json(content: &str) -> ParseResult {
96        parse(content.as_bytes(), DocumentFormat::Json)
97    }
98
99    /// Parse a contract from a file path.
100    pub fn from_file(path: impl AsRef<std::path::Path>) -> miette::Result<ParseResult> {
101        parse_file(path)
102    }
103}