mod input;
use crate::catalog::Code;
use serde::Serialize;
pub(crate) use input::ChangeInput;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum CompatibilitySeverity {
Compatible,
Breaking,
Forbidden,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CompatibilityChange {
id: &'static str,
severity: CompatibilitySeverity,
code: Option<Code>,
path: String,
previous: String,
current: String,
reason: String,
action: String,
}
impl CompatibilityChange {
pub const fn id(&self) -> &'static str {
self.id
}
pub const fn severity(&self) -> CompatibilitySeverity {
self.severity
}
pub const fn code(&self) -> Option<&Code> {
self.code.as_ref()
}
pub fn path(&self) -> &str {
&self.path
}
pub fn previous(&self) -> &str {
&self.previous
}
pub fn current(&self) -> &str {
&self.current
}
pub fn reason(&self) -> &str {
&self.reason
}
pub fn action(&self) -> &str {
&self.action
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CompatibilityReport {
changes: Vec<CompatibilityChange>,
}
impl CompatibilityReport {
pub(crate) fn new(mut changes: Vec<CompatibilityChange>) -> Self {
changes.sort_by(|left, right| {
let left_key = (left.code.as_ref(), left.path.as_str(), left.id);
let right_key = (right.code.as_ref(), right.path.as_str(), right.id);
left_key.cmp(&right_key)
});
Self { changes }
}
pub fn changes(&self) -> &[CompatibilityChange] {
&self.changes
}
pub fn is_compatible(&self) -> bool {
self.changes
.iter()
.all(|change| change.severity == CompatibilitySeverity::Compatible)
}
pub fn has_breaking(&self) -> bool {
self.changes
.iter()
.any(|change| change.severity == CompatibilitySeverity::Breaking)
}
pub fn has_forbidden(&self) -> bool {
self.changes
.iter()
.any(|change| change.severity == CompatibilitySeverity::Forbidden)
}
}
pub(crate) fn push(changes: &mut Vec<CompatibilityChange>, input: ChangeInput) {
changes.push(input.finish());
}