rusty_schema_diff/analyzer.rs
1//! Core analyzer traits and types for schema comparison
2//!
3//! This module provides the base traits and types used by all schema analyzers.
4
5use crate::{Schema, CompatibilityReport, MigrationPlan, ValidationResult, error::Result};
6use serde::{Serialize, Deserialize};
7use std::collections::HashMap;
8
9pub mod json_schema;
10pub mod protobuf;
11pub mod openapi;
12pub mod sql;
13
14/// Core trait for implementing schema analyzers
15///
16/// This trait defines the required functionality for analyzing schema changes
17/// and generating compatibility reports and migration paths.
18pub trait SchemaAnalyzer {
19 /// Analyzes compatibility between two schema versions
20 ///
21 /// # Arguments
22 /// * `old` - The original schema version
23 /// * `new` - The new schema version to compare against
24 ///
25 /// # Returns
26 /// A compatibility report detailing the differences and compatibility status
27 fn analyze_compatibility(&self, old: &Schema, new: &Schema) -> Result<CompatibilityReport>;
28
29 /// Generates a migration path between schema versions
30 ///
31 /// # Arguments
32 /// * `old` - The source schema version
33 /// * `new` - The target schema version
34 ///
35 /// # Returns
36 /// A migration plan detailing the required changes
37 fn generate_migration_path(&self, old: &Schema, new: &Schema) -> Result<MigrationPlan>;
38
39 /// Validates proposed schema changes
40 ///
41 /// # Arguments
42 /// * `changes` - List of proposed schema changes to validate
43 ///
44 /// # Returns
45 /// Validation results indicating if the changes are safe
46 fn validate_changes(&self, changes: &[SchemaChange]) -> Result<ValidationResult>;
47}
48
49/// Represents a single schema change
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct SchemaChange {
52 /// Type of change (Addition, Removal, etc.)
53 pub change_type: ChangeType,
54 /// Path to the changed element
55 pub location: String,
56 /// Human-readable description of the change
57 pub description: String,
58 /// Metadata associated with the change
59 pub metadata: HashMap<String, String>,
60}
61
62impl SchemaChange {
63 /// Creates a new SchemaChange instance
64 ///
65 /// # Arguments
66 /// * `change_type` - The type of change
67 /// * `location` - The location of the change
68 /// * `description` - The description of the change
69 /// * `metadata` - The metadata associated with the change
70 ///
71 /// # Returns
72 /// A new SchemaChange instance
73 pub fn new(
74 change_type: ChangeType,
75 location: impl Into<String>,
76 description: impl Into<String>,
77 metadata: HashMap<String, String>,
78 ) -> Self {
79 SchemaChange {
80 change_type,
81 location: location.into(),
82 description: description.into(),
83 metadata,
84 }
85 }
86}
87
88/// Types of schema changes that can occur
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
90pub enum ChangeType {
91 /// New element added
92 Addition,
93 /// Existing element removed
94 Removal,
95 /// Element modified
96 Modification,
97 /// Element renamed
98 Rename,
99}