rusty_schema_diff/analyzer/
protobuf.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! Protobuf specific analyzer implementation
//!
//! This module provides functionality for analyzing Protobuf changes and
//! generating compatibility reports and migration paths.

use protobuf::descriptor::{FileDescriptorProto, DescriptorProto};
use crate::analyzer::{SchemaAnalyzer, SchemaChange, ChangeType};
use crate::{Schema, CompatibilityReport, MigrationPlan, ValidationResult, SchemaDiffError};
use crate::error::Result;
use crate::report::{CompatibilityIssue, IssueSeverity, ValidationError};
use std::collections::HashMap;

/// Analyzes Protobuf changes and generates compatibility reports.
pub struct ProtobufAnalyzer;

impl SchemaAnalyzer for ProtobufAnalyzer {
    /// Analyzes compatibility between two Protobuf versions.
    ///
    /// # Arguments
    ///
    /// * `old` - The original Protobuf version.
    /// * `new` - The new Protobuf version to compare against.
    ///
    /// # Returns
    ///
    /// A `CompatibilityReport` detailing the differences and compatibility status.
    fn analyze_compatibility(&self, old: &Schema, new: &Schema) -> Result<CompatibilityReport> {
        let old_desc = self.parse_proto(&old.content)?;
        let new_desc = self.parse_proto(&new.content)?;

        let mut changes = Vec::new();
        self.compare_descriptors(&old_desc, &new_desc, "", &mut changes)?;

        let compatibility_score = self.calculate_compatibility_score(&changes);
        let is_compatible = compatibility_score >= 80;

        Ok(CompatibilityReport {
            compatibility_score: compatibility_score.try_into().unwrap(),
            is_compatible,
            changes: changes,
            issues: vec![],
            metadata: Default::default(),
        })
    }

    /// Generates a migration path between Protobuf versions.
    ///
    /// # Arguments
    ///
    /// * `old` - The source Protobuf version.
    /// * `new` - The target Protobuf version.
    ///
    /// # Returns
    ///
    /// A `MigrationPlan` detailing the required changes.
    fn generate_migration_path(&self, old: &Schema, new: &Schema) -> Result<MigrationPlan> {
        let old_desc = self.parse_proto(&old.content)?;
        let new_desc = self.parse_proto(&new.content)?;

        let mut changes = Vec::new();
        self.compare_descriptors(&old_desc, &new_desc, "", &mut changes)?;

        Ok(MigrationPlan::new(
            old.version.to_string(),
            new.version.to_string(),
            changes,
        ))
    }

    fn validate_changes(&self, changes: &[SchemaChange]) -> Result<ValidationResult> {
        let errors: Vec<ValidationError> = changes
            .iter()
            .filter_map(|change| {
                self.validate_change(change).map(|issue| ValidationError {
                    message: issue.description,
                    path: issue.location,
                    code: format!("PROTO{}", match issue.severity {
                        IssueSeverity::Error => "001",
                        IssueSeverity::Warning => "002",
                        IssueSeverity::Info => "003",
                    }),
                })
            })
            .collect();

        Ok(ValidationResult {
            is_valid: errors.is_empty(),
            errors,
            context: Default::default(),
        })
    }
}

impl ProtobufAnalyzer {
    /// Parses protobuf content into a FileDescriptorProto
    fn parse_proto(&self, content: &str) -> Result<FileDescriptorProto> {
        // Basic implementation using protobuf parser
        match protobuf::text_format::parse_from_str(content) {
            Ok(desc) => Ok(desc),
            Err(e) => Err(SchemaDiffError::ProtobufError(e.to_string()))
        }
    }

    /// Compares two protobuf descriptors
    fn compare_descriptors(
        &self,
        old: &FileDescriptorProto,
        new: &FileDescriptorProto,
        path: &str,
        changes: &mut Vec<SchemaChange>,
    ) -> Result<()> {
        // Compare messages
        for old_msg in &old.message_type {
            if let Some(new_msg) = new.message_type.iter().find(|m| m.name() == old_msg.name()) {
                self.compare_messages(old_msg, new_msg, path, changes)?;
            } else {
                changes.push(SchemaChange {
                    change_type: ChangeType::Removal,
                    location: format!("{}/{}", path, old_msg.name()),
                    description: format!("Message '{}' was removed", old_msg.name()),
                    metadata: Default::default(),
                });
            }
        }

        // Check for new messages
        for new_msg in &new.message_type {
            if !old.message_type.iter().any(|m| m.name() == new_msg.name()) {
                changes.push(SchemaChange {
                    change_type: ChangeType::Addition,
                    location: format!("{}/{}", path, new_msg.name()),
                    description: format!("Message '{}' was added", new_msg.name()),
                    metadata: Default::default(),
                });
            }
        }

        Ok(())
    }

    /// Compares two protobuf messages
    fn compare_messages(
        &self,
        old_msg: &DescriptorProto,
        new_msg: &DescriptorProto,
        path: &str,
        changes: &mut Vec<SchemaChange>,
    ) -> Result<()> {
        self.compare_fields(path, old_msg, new_msg, changes);
        Ok(())
    }

    fn compare_fields(
        &self,
        path: &str,
        old_msg: &DescriptorProto,
        new_msg: &DescriptorProto,
        changes: &mut Vec<SchemaChange>,
    ) {
        for old_field in old_msg.field.iter() {
            if let Some(new_field) = new_msg.field.iter().find(|f| f.name() == old_field.name()) {
                if old_field.type_() != new_field.type_() {
                    let mut metadata = HashMap::new();
                    metadata.insert("message".to_string(), old_msg.name().to_string());
                    metadata.insert("field".to_string(), old_field.name().to_string());
                    metadata.insert("old_type".to_string(), format!("{:?}", old_field.type_()));
                    metadata.insert("new_type".to_string(), format!("{:?}", new_field.type_()));
                    
                    changes.push(SchemaChange::new(
                        ChangeType::Modification,
                        format!("{}/{}/{}", path, old_msg.name(), old_field.name()),
                        format!(
                            "Field '{}' type changed from {:?} to {:?}",
                            old_field.name(),
                            old_field.type_(),
                            new_field.type_()
                        ),
                        metadata,
                    ));
                }
            } else {
                let mut metadata = HashMap::new();
                metadata.insert("message".to_string(), old_msg.name().to_string());
                metadata.insert("field".to_string(), old_field.name().to_string());
                
                changes.push(SchemaChange::new(
                    ChangeType::Removal,
                    format!("{}/{}/{}", path, old_msg.name(), old_field.name()),
                    format!("Field '{}' was removed", old_field.name()),
                    metadata,
                ));
            }
        }

        // Check for new fields
        for new_field in new_msg.field.iter() {
            if !old_msg.field.iter().any(|f| f.name() == new_field.name()) {
                let mut metadata = HashMap::new();
                metadata.insert("message".to_string(), new_msg.name().to_string());
                metadata.insert("field".to_string(), new_field.name().to_string());
                
                changes.push(SchemaChange::new(
                    ChangeType::Addition,
                    format!("{}/{}/{}", path, new_msg.name(), new_field.name()),
                    format!("New field '{}' was added", new_field.name()),
                    metadata,
                ));
            }
        }
    }

    /// Validates a single schema change
    fn validate_change(&self, change: &SchemaChange) -> Option<CompatibilityIssue> {
        match change.change_type {
            ChangeType::Removal => Some(CompatibilityIssue {
                severity: IssueSeverity::Error,
                description: format!("Breaking change: {}", change.description),
                location: change.location.clone(),
            }),
            ChangeType::Modification => Some(CompatibilityIssue {
                severity: IssueSeverity::Warning,
                description: format!("Potential compatibility issue: {}", change.description),
                location: change.location.clone(),
            }),
            ChangeType::Rename => {
                todo!("Implement handling for Rename change type");
            },
            _ => None,
        }
    }

    /// Calculates compatibility score for protobuf changes
    fn calculate_compatibility_score(&self, changes: &[SchemaChange]) -> i32 {
        let base_score: i32 = 100;
        let mut deductions: i32 = 0;
        
        for change in changes {
            match change.change_type {
                ChangeType::Addition => (),
                ChangeType::Removal => deductions += 20,
                ChangeType::Modification => deductions += 10,
                ChangeType::Rename => {
                    todo!("Implement handling for Rename change type");
                },
            }
        }
        
        base_score.saturating_sub(deductions)
    }
}