1use ridl_ir::v2;
26
27use crate::{Category, Change, Verdict, frozen};
28
29#[cfg(test)]
30mod classify_tests;
31
32pub fn classify(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
45 classify_in(change, old, new, &[])
46}
47
48#[deny(
60 clippy::wildcard_enum_match_arm,
61 clippy::match_wildcard_for_single_variants
62)]
63pub(crate) fn classify_in(
64 change: &Change,
65 old: &v2::Package,
66 new: &v2::Package,
67 scope: &[&v2::Package],
68) -> Verdict {
69 match change.category {
70 Category::InteractionInserted
73 | Category::InteractionReordered
74 | Category::MemberReordered
75 | Category::InteractionRemoved
76 | Category::ReservedNameRedeclared
77 | Category::KindChanged
78 | Category::PayloadChanged
79 | Category::ReturnChanged
80 | Category::ParamsChanged
81 | Category::WidthChanged
82 | Category::ServiceChanged
83 | Category::DeclRemoved => Verdict::Breaking,
84
85 Category::InitChanged => Verdict::Breaking,
89
90 Category::DocOnly | Category::InteractionRetired => Verdict::Compatible,
97
98 Category::InterfaceRenamed | Category::InterfaceRetired => Verdict::Compatible,
105
106 Category::ServiceInterfaceAdded | Category::ServiceInterfaceRemoved => Verdict::Compatible,
114
115 Category::VisibilityChanged => visibility(change, old, new),
116 Category::InteractionAppended => appended(change, old, new),
117 Category::DeclAdded => added(change, old, new, scope),
118 Category::ConstraintChanged => constraint(change, old, new),
119 Category::TimingChanged => timing(change, old, new),
120 Category::RpcBoundChanged => rpc_bound(change, old, new),
121 Category::ContractChanged => contract(change, old, new),
122 }
123}
124
125fn visibility(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
143 let (Some(old_visibility), Some(new_visibility)) = (
144 find_visibility(old, &change.path, |name| find_old_shape(old, new, name)),
145 find_visibility(new, &change.path, |name| find_shape(new, name)),
146 ) else {
147 return Verdict::Breaking;
148 };
149
150 match (
151 v2::Visibility::try_from(old_visibility),
152 v2::Visibility::try_from(new_visibility),
153 ) {
154 (Ok(v2::Visibility::Internal), Ok(v2::Visibility::Public)) => Verdict::Compatible,
155 _ => Verdict::Breaking,
156 }
157}
158
159fn find_visibility<'a>(
166 package: &'a v2::Package,
167 path: &str,
168 shape_of: impl Fn(&str) -> Option<v2::InterfaceShape<'a>>,
169) -> Option<i32> {
170 let mut segments = path.split('/').skip(1);
171 let name = segments.next()?;
172
173 if let Some(member) = segments.next() {
174 return shape_of(name)?
175 .interface
176 .interactions
177 .iter()
178 .find(|decl| decl.name == member)
179 .map(|decl| decl.visibility);
180 }
181 if let Some(decl) = find_decl(package, name) {
182 return Some(decl.visibility);
183 }
184 if let Some(shape) = shape_of(name) {
189 return Some(shape.visibility());
190 }
191 package
194 .services
195 .iter()
196 .find(|service| service.name == name)
197 .map(|service| service.visibility)
198}
199
200fn appended(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
215 let Some((container, member)) = member_path(change) else {
216 return Verdict::Breaking;
217 };
218 let (Some(old_iface), Some(new_iface)) = (
219 find_old_interface(old, new, container),
220 find_interface(new, container),
221 ) else {
222 return Verdict::Breaking;
223 };
224 let Some(ordinal) = slot_ordinal(new_iface, member) else {
225 return Verdict::Breaking;
226 };
227
228 for (name, old_ordinal) in slots(old_iface) {
229 if old_ordinal == ordinal && name != member {
230 return Verdict::Breaking;
231 }
232 }
233 Verdict::Compatible
234}
235
236fn added(change: &Change, old: &v2::Package, new: &v2::Package, scope: &[&v2::Package]) -> Verdict {
252 let Some((container, member)) = member_path(change) else {
253 return Verdict::Compatible;
256 };
257 let (Some(old_decl), Some(new_decl)) = (find_decl(old, container), find_decl(new, container))
258 else {
259 return Verdict::Breaking;
260 };
261
262 use v2::decl::Kind;
267 let appended = match (&old_decl.kind, &new_decl.kind) {
268 (Some(Kind::StructDef(old_def)), Some(Kind::StructDef(new_def))) => {
269 appended_slot(
270 &struct_slots(old_def),
271 &struct_reserved(old_def),
272 &struct_slots(new_def),
273 ) && absence_reads_as_legal(new_def, member, new, scope)
274 }
275 (Some(Kind::UnionDef(old_def)), Some(Kind::UnionDef(new_def))) => {
276 !old_def.is_result
279 && !new_def.is_result
280 && appended_slot(
281 &union_slots(old_def),
282 &union_reserved(old_def),
283 &union_slots(new_def),
284 )
285 }
286 (Some(Kind::EnumDef(old_def)), Some(Kind::EnumDef(new_def))) => appended_slot(
287 &value_slots(&old_def.values),
288 &reserved_values(&old_def.reserved),
289 &value_slots(&new_def.values),
290 ),
291 (Some(Kind::EnumSetDef(old_def)), Some(Kind::EnumSetDef(new_def))) => appended_slot(
292 &value_slots(&old_def.bits),
293 &[],
294 &value_slots(&new_def.bits),
295 ),
296 _ => false,
298 };
299
300 if appended {
301 Verdict::Compatible
302 } else {
303 Verdict::Breaking
304 }
305}
306
307pub fn absence_refused(
316 new_set: &[v2::Package],
317 context: &[v2::Package],
318 package: &str,
319 container: &str,
320 member: &str,
321) -> bool {
322 let scope: Vec<&v2::Package> = new_set.iter().chain(context).collect();
323 let Some(home) = new_set.iter().find(|candidate| candidate.name == package) else {
324 return false;
325 };
326 match find_decl(home, container).and_then(|decl| decl.kind.as_ref()) {
327 Some(v2::decl::Kind::StructDef(def)) => !absence_reads_as_legal(def, member, home, &scope),
328 _ => false,
329 }
330}
331
332fn absence_reads_as_legal(
352 def: &v2::StructDef,
353 member: &str,
354 home: &v2::Package,
355 scope: &[&v2::Package],
356) -> bool {
357 let Some(r#type) = def.members.iter().find_map(|slot| match &slot.member {
358 Some(v2::struct_member::Member::Field(field)) if field.name == member => {
359 field.r#type.as_ref()
360 }
361 _ => None,
362 }) else {
363 return false;
364 };
365 if r#type.optional {
366 return true;
367 }
368 use v2::field_type::Kind;
369 match &r#type.kind {
370 Some(Kind::Primitive(primitive)) => matches!(
371 v2::PrimitiveType::try_from(*primitive),
372 Ok(v2::PrimitiveType::Boolean | v2::PrimitiveType::Integer | v2::PrimitiveType::Float)
373 ),
374 Some(Kind::InlineScalar(def)) => scalar_holds_zero(def),
375 Some(Kind::Named(reference)) => {
376 use v2::decl::Kind as Decl;
377 match resolve(home, scope, reference).and_then(|decl| decl.kind.as_ref()) {
378 Some(Decl::TypeDef(def)) => scalar_holds_zero(def),
379 Some(Decl::EnumDef(def)) => ridl_ir::zero::enum_zero_member(&def.values).is_some(),
380 Some(Decl::EnumSetDef(_)) => true,
382 _ => false,
383 }
384 }
385 _ => false,
386 }
387}
388
389fn scalar_holds_zero(def: &v2::TypeDef) -> bool {
393 let numeric = match def
394 .backing
395 .as_ref()
396 .and_then(|backing| backing.kind.as_ref())
397 {
398 Some(v2::backing::Kind::Unit(_)) => true,
399 Some(v2::backing::Kind::Primitive(primitive)) => matches!(
400 v2::PrimitiveType::try_from(*primitive),
401 Ok(v2::PrimitiveType::Boolean | v2::PrimitiveType::Integer | v2::PrimitiveType::Float)
402 ),
403 None => false,
404 };
405 numeric
406 && def.constraint.as_ref().is_none_or(|constraint| {
407 ridl_ir::zero::range_holds_zero(
408 constraint.min.as_deref(),
409 constraint.max.as_deref(),
410 constraint.step.as_deref(),
411 )
412 })
413}
414
415fn resolve<'a>(
419 home: &'a v2::Package,
420 scope: &[&'a v2::Package],
421 reference: &str,
422) -> Option<&'a v2::Decl> {
423 match reference.rsplit_once('.') {
424 Some((package, name)) => std::iter::once(home)
425 .chain(scope.iter().copied())
426 .find(|candidate| candidate.name == package)
427 .and_then(|candidate| find_decl(candidate, name)),
428 None => find_decl(home, reference),
429 }
430}
431
432fn appended_slot(old: &[(String, i64)], old_retired: &[i64], new: &[(String, i64)]) -> bool {
443 for (name, old_slot) in old {
444 match new.iter().find(|(new_name, _)| new_name == name) {
445 Some((_, new_slot)) if new_slot == old_slot => {}
446 _ => return false,
448 }
449 }
450
451 let high_water = old
452 .iter()
453 .map(|(_, slot)| *slot)
454 .chain(old_retired.iter().copied())
455 .max();
456 let old_names: Vec<&String> = old.iter().map(|(name, _)| name).collect();
457 for (name, slot) in new {
458 if old_names.contains(&name) {
459 continue;
460 }
461 match high_water {
462 Some(mark) if *slot <= mark => return false,
463 _ => {}
464 }
465 }
466 true
467}
468
469fn constraint(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
481 let mut segments = change.path.split('/').skip(1);
482 let (Some(name), None) = (segments.next(), segments.next()) else {
483 return Verdict::Breaking;
484 };
485 let (Some(old_decl), Some(new_decl)) = (find_decl(old, name), find_decl(new, name)) else {
486 return Verdict::Breaking;
487 };
488
489 use v2::decl::Kind;
490 let (Some(Kind::TypeDef(old_def)), Some(Kind::TypeDef(new_def))) =
491 (&old_decl.kind, &new_decl.kind)
492 else {
493 return Verdict::Breaking;
494 };
495
496 if narrows(old_def.constraint.as_ref(), new_def.constraint.as_ref()) {
497 Verdict::Breaking
498 } else {
499 Verdict::Compatible
500 }
501}
502
503fn narrows(old: Option<&v2::Constraint>, new: Option<&v2::Constraint>) -> bool {
509 let (Some(old), Some(new)) = (old, new) else {
510 return old.is_none() && new.is_some();
513 };
514
515 if raised(old.min.as_deref(), new.min.as_deref())
517 || lowered(old.max.as_deref(), new.max.as_deref())
518 {
519 return true;
520 }
521 if raised_u64(old.len_min, new.len_min) || lowered_u64(old.len_max, new.len_max) {
523 return true;
524 }
525 if new.step.is_some() && old.step != new.step {
528 return true;
529 }
530 if (new.pattern.is_some() && old.pattern != new.pattern)
533 || (new.pattern_const.is_some() && old.pattern_const != new.pattern_const)
534 {
535 return true;
536 }
537 false
538}
539
540fn raised(old: Option<&str>, new: Option<&str>) -> bool {
542 match (old, new) {
543 (None, Some(_)) => true,
544 (Some(old), Some(new)) => cmp_decimal(new, old).is_none_or(std::cmp::Ordering::is_gt),
545 _ => false,
546 }
547}
548
549fn lowered(old: Option<&str>, new: Option<&str>) -> bool {
551 match (old, new) {
552 (None, Some(_)) => true,
553 (Some(old), Some(new)) => cmp_decimal(new, old).is_none_or(std::cmp::Ordering::is_lt),
554 _ => false,
555 }
556}
557
558fn raised_u64(old: Option<u64>, new: Option<u64>) -> bool {
559 match (old, new) {
560 (None, Some(_)) => true,
561 (Some(old), Some(new)) => new > old,
562 _ => false,
563 }
564}
565
566fn lowered_u64(old: Option<u64>, new: Option<u64>) -> bool {
567 match (old, new) {
568 (None, Some(_)) => true,
569 (Some(old), Some(new)) => new < old,
570 _ => false,
571 }
572}
573
574fn cmp_decimal(left: &str, right: &str) -> Option<std::cmp::Ordering> {
584 use std::cmp::Ordering;
585
586 let (left_negative, left_digits) = split_sign(left)?;
587 let (right_negative, right_digits) = split_sign(right)?;
588 if left_negative != right_negative {
589 return Some(if left_negative {
592 Ordering::Less
593 } else {
594 Ordering::Greater
595 });
596 }
597
598 let magnitude = cmp_magnitude(&left_digits, &right_digits)?;
599 Some(if left_negative {
600 magnitude.reverse()
601 } else {
602 magnitude
603 })
604}
605
606fn split_sign(text: &str) -> Option<(bool, String)> {
609 let (negative, rest) = match text.strip_prefix('-') {
610 Some(rest) => (true, rest),
611 None => (false, text.strip_prefix('+').unwrap_or(text)),
612 };
613 if rest.is_empty() || !rest.chars().all(|c| c.is_ascii_digit() || c == '.') {
614 return None;
615 }
616 if rest.matches('.').count() > 1 {
617 return None;
618 }
619 Some((negative, rest.to_string()))
620}
621
622fn cmp_magnitude(left: &str, right: &str) -> Option<std::cmp::Ordering> {
625 use std::cmp::Ordering;
626
627 let (left_int, left_frac) = left.split_once('.').unwrap_or((left, ""));
628 let (right_int, right_frac) = right.split_once('.').unwrap_or((right, ""));
629
630 let left_int = left_int.trim_start_matches('0');
631 let right_int = right_int.trim_start_matches('0');
632 let by_length = left_int.len().cmp(&right_int.len());
633 if by_length != Ordering::Equal {
634 return Some(by_length);
635 }
636 let by_int = left_int.cmp(right_int);
637 if by_int != Ordering::Equal {
638 return Some(by_int);
639 }
640
641 let width = left_frac.len().max(right_frac.len());
643 let pad = |frac: &str| format!("{frac:0<width$}");
644 Some(pad(left_frac).cmp(&pad(right_frac)))
645}
646
647fn timing(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
659 let Some((container, member)) = member_path(change) else {
660 return Verdict::Breaking;
661 };
662 let (Some(old_decl), Some(new_decl)) = (
663 find_old_interaction(old, new, container, member),
664 find_interaction(new, container, member),
665 ) else {
666 return Verdict::Breaking;
667 };
668 let (Some(old_timing), Some(new_timing)) =
669 (interaction_timing(old_decl), interaction_timing(new_decl))
670 else {
671 return Verdict::Breaking;
681 };
682
683 let (Some(old_timing), Some(new_timing)) = (old_timing, new_timing) else {
684 return Verdict::Breaking;
687 };
688
689 if old_timing.mode != new_timing.mode {
690 return Verdict::Breaking;
691 }
692 if lowered(old_timing.min_us.as_deref(), new_timing.min_us.as_deref())
694 || dropped(old_timing.min_us.as_deref(), new_timing.min_us.as_deref())
695 || raised(old_timing.max_us.as_deref(), new_timing.max_us.as_deref())
696 || dropped(old_timing.max_us.as_deref(), new_timing.max_us.as_deref())
697 {
698 return Verdict::Breaking;
699 }
700 Verdict::Compatible
703}
704
705fn rpc_bound(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
724 let Some((container, member)) = member_path(change) else {
725 return Verdict::Breaking;
726 };
727 let (Some(old_decl), Some(new_decl)) = (
728 find_old_interaction(old, new, container, member),
729 find_interaction(new, container, member),
730 ) else {
731 return Verdict::Breaking;
732 };
733 let (Some(old_timing), Some(new_timing)) = (rpc_timing(old_decl), rpc_timing(new_decl)) else {
734 return Verdict::Breaking;
741 };
742
743 let (Some(old_timing), Some(new_timing)) = (old_timing, new_timing) else {
744 return Verdict::Breaking;
747 };
748
749 if old_timing.mode != new_timing.mode {
752 return Verdict::Breaking;
753 }
754 if raised(old_timing.min_us.as_deref(), new_timing.min_us.as_deref())
759 || dropped(old_timing.min_us.as_deref(), new_timing.min_us.as_deref())
760 || raised(old_timing.max_us.as_deref(), new_timing.max_us.as_deref())
761 || dropped(old_timing.max_us.as_deref(), new_timing.max_us.as_deref())
762 {
763 return Verdict::Breaking;
764 }
765 Verdict::Compatible
771}
772
773fn dropped(old: Option<&str>, new: Option<&str>) -> bool {
775 old.is_some() && new.is_none()
776}
777
778fn interaction_timing(decl: &v2::Decl) -> Option<Option<&v2::Timing>> {
781 use v2::decl::Kind;
782 match &decl.kind {
783 Some(Kind::SignalDef(def)) => Some(def.timing.as_ref()),
784 Some(Kind::EventDef(def)) => Some(def.timing.as_ref()),
785 _ => None,
786 }
787}
788
789fn rpc_timing(decl: &v2::Decl) -> Option<Option<&v2::Timing>> {
793 use v2::decl::Kind;
794 match &decl.kind {
795 Some(Kind::CommandDef(def)) => Some(def.timing.as_ref()),
796 Some(Kind::QueryDef(def)) => Some(def.timing.as_ref()),
797 _ => None,
798 }
799}
800
801fn contract(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
815 let Some((container, member)) = member_path(change) else {
816 return Verdict::Breaking;
817 };
818 let (Some(old_decl), Some(new_decl)) = (
819 find_old_interaction(old, new, container, member),
820 find_interaction(new, container, member),
821 ) else {
822 return Verdict::Breaking;
823 };
824 let (Some(old_clauses), Some(new_clauses)) = (contracts(old_decl), contracts(new_decl)) else {
825 return Verdict::Breaking;
826 };
827
828 let kind = |want: v2::ContractKind| {
829 move |clause: &&v2::Contract| v2::ContractKind::try_from(clause.kind) == Ok(want)
830 };
831 let sources = |clauses: &[v2::Contract], want: v2::ContractKind| -> Vec<String> {
832 let mut out: Vec<String> = clauses
833 .iter()
834 .filter(kind(want))
835 .map(|clause| clause.source.clone())
836 .collect();
837 out.sort();
838 out
839 };
840
841 let old_require = sources(old_clauses, v2::ContractKind::Require);
842 let new_require = sources(new_clauses, v2::ContractKind::Require);
843 let old_ensure = sources(old_clauses, v2::ContractKind::Ensure);
844 let new_ensure = sources(new_clauses, v2::ContractKind::Ensure);
845
846 if !covers(&old_require, &new_require) || !covers(&new_ensure, &old_ensure) {
847 return Verdict::Breaking;
848 }
849 Verdict::Compatible
850}
851
852fn covers(superset: &[String], subset: &[String]) -> bool {
854 let mut remaining: Vec<&String> = superset.iter().collect();
855 for clause in subset {
856 match remaining.iter().position(|held| *held == clause) {
857 Some(index) => {
858 remaining.swap_remove(index);
859 }
860 None => return false,
861 }
862 }
863 true
864}
865
866fn contracts(decl: &v2::Decl) -> Option<&[v2::Contract]> {
868 use v2::decl::Kind;
869 match &decl.kind {
870 Some(Kind::CommandDef(def)) => Some(&def.contracts),
871 Some(Kind::QueryDef(def)) => Some(&def.contracts),
872 _ => None,
873 }
874}
875
876fn member_path(change: &Change) -> Option<(&str, &str)> {
883 let mut segments = change.path.split('/').skip(1);
884 let container = segments.next()?;
885 let member = segments.next()?;
886 if segments.next().is_some() {
887 return None;
888 }
889 Some((container, member))
890}
891
892fn find_decl<'a>(package: &'a v2::Package, name: &str) -> Option<&'a v2::Decl> {
893 package.decls.iter().find(|decl| decl.name == name)
894}
895
896fn find_shape<'a>(package: &'a v2::Package, name: &str) -> Option<v2::InterfaceShape<'a>> {
901 package.shapes().find(|shape| shape.name == name)
902}
903
904fn find_old_shape<'a>(
911 old: &'a v2::Package,
912 new: &v2::Package,
913 name: &str,
914) -> Option<v2::InterfaceShape<'a>> {
915 let by_number = find_shape(new, name)
916 .filter(|shape| frozen(shape.interface))
917 .and_then(|shape| {
918 old.shapes().find(|candidate| {
919 frozen(candidate.interface) && candidate.interface.number == shape.interface.number
920 })
921 });
922 by_number.or_else(|| find_shape(old, name))
923}
924
925fn find_interface<'a>(package: &'a v2::Package, name: &str) -> Option<&'a v2::Interface> {
926 find_shape(package, name).map(|shape| shape.interface)
927}
928
929fn find_old_interface<'a>(
930 old: &'a v2::Package,
931 new: &v2::Package,
932 name: &str,
933) -> Option<&'a v2::Interface> {
934 find_old_shape(old, new, name).map(|shape| shape.interface)
935}
936
937fn find_interaction<'a>(
938 package: &'a v2::Package,
939 container: &str,
940 member: &str,
941) -> Option<&'a v2::Decl> {
942 find_interface(package, container)?
943 .interactions
944 .iter()
945 .find(|decl| decl.name == member)
946}
947
948fn find_old_interaction<'a>(
951 old: &'a v2::Package,
952 new: &v2::Package,
953 container: &str,
954 member: &str,
955) -> Option<&'a v2::Decl> {
956 find_old_interface(old, new, container)?
957 .interactions
958 .iter()
959 .find(|decl| decl.name == member)
960}
961
962fn slots(interface: &v2::Interface) -> Vec<(&str, u32)> {
974 interface
975 .interactions
976 .iter()
977 .filter_map(|decl| match &decl.kind {
978 Some(v2::decl::Kind::ReservedSlot(reserved)) => {
979 Some((reserved.name.as_deref().unwrap_or(""), decl.ordinal))
980 }
981 Some(_) => Some((decl.name.as_str(), decl.ordinal)),
982 None => None,
983 })
984 .collect()
985}
986
987fn slot_ordinal(interface: &v2::Interface, member: &str) -> Option<u32> {
988 slots(interface)
989 .into_iter()
990 .find(|(name, _)| *name == member)
991 .map(|(_, ordinal)| ordinal)
992}
993
994pub(crate) fn struct_slots(def: &v2::StructDef) -> Vec<(String, i64)> {
998 def.members
999 .iter()
1000 .filter_map(|member| match &member.member {
1001 Some(v2::struct_member::Member::Field(field)) => {
1002 Some((field.name.clone(), i64::from(field.ordinal)))
1003 }
1004 _ => None,
1005 })
1006 .collect()
1007}
1008
1009fn struct_reserved(def: &v2::StructDef) -> Vec<i64> {
1010 def.members
1011 .iter()
1012 .filter_map(|member| match &member.member {
1013 Some(v2::struct_member::Member::Reserved(reserved)) => {
1014 Some(i64::from(reserved.ordinal))
1015 }
1016 _ => None,
1017 })
1018 .collect()
1019}
1020
1021pub(crate) fn union_slots(def: &v2::UnionDef) -> Vec<(String, i64)> {
1023 def.arms
1024 .iter()
1025 .map(|arm| (arm.name.clone(), i64::from(arm.ordinal)))
1026 .collect()
1027}
1028
1029fn union_reserved(def: &v2::UnionDef) -> Vec<i64> {
1030 def.reserved
1031 .iter()
1032 .map(|reserved| i64::from(reserved.ordinal))
1033 .collect()
1034}
1035
1036fn value_slots(values: &[v2::EnumValue]) -> Vec<(String, i64)> {
1039 values
1040 .iter()
1041 .map(|value| (value.name.clone(), value.value))
1042 .collect()
1043}
1044
1045fn reserved_values(reserved: &[v2::Reserved]) -> Vec<i64> {
1046 reserved.iter().filter_map(|entry| entry.value).collect()
1047}
1048
1049pub fn category_from_word(word: &str) -> Option<Category> {
1056 crate::CATEGORIES
1057 .into_iter()
1058 .find(|category| crate::category_word(*category) == word)
1059}
1060
1061#[deny(
1071 clippy::wildcard_enum_match_arm,
1072 clippy::match_wildcard_for_single_variants
1073)]
1074pub fn explain(category: Category) -> &'static str {
1075 match category {
1076 Category::DeclAdded => concat!(
1077 "A declaration, interface, or service present only in the new snapshot.\n",
1078 " compatible a new package-level decl, interface, or service; an enum\n",
1079 " value appended above every live and retired number; a\n",
1080 " union arm appended at the end of the body (typl 7.4,\n",
1081 " append-only); a struct field appended at the end of the body\n",
1082 " when it is optional, or when it is a scalar, enum, or enum set\n",
1083 " whose type allows the value 0\n",
1084 " breaking a member inserted below the highest slot ever used, a member\n",
1085 " taking a retired number, any addition that moves a surviving\n",
1086 " member's slot, or any arm of a result union (ADR-0008 d4); a\n",
1087 " non-optional struct field appended whose type does not allow\n",
1088 " 0 — a string, bytes, struct, union, tuple, array, or map, a\n",
1089 " scalar whose range or step excludes 0, an enum with no zero\n",
1090 " member, or a type the diff cannot resolve (ridl diff\n",
1091 " resolves ridl.std types). A reader of the new version\n",
1092 " refuses every payload of the old one, which does not carry\n",
1093 " the field (typl 7.4). Declare the new field optional instead\n",
1094 " (`field : T?`): an absent optional field reads as absent\n",
1095 " note an interface is matched by its interfaces.lock number (lock\n",
1096 " design 7): a frozen number the old side never held is a new\n",
1097 " interface, and so is every provisional one — a declaration\n",
1098 " with no lock entry carries no identity, and a rename keeps its\n",
1099 " number, so it is never a renamed interface (interface_renamed)"
1100 ),
1101 Category::DeclRemoved => concat!(
1102 "A declaration, interface, or service present only in the old snapshot.\n",
1103 " breaking a removed service, interface, decl, enum value, struct\n",
1104 " field, or union arm withdraws something a consumer compiled\n",
1105 " against\n",
1106 " note an interface is matched by its interfaces.lock number (lock\n",
1107 " design 7), so an interface here is one whose number is gone\n",
1108 " from the new side and not retired there — a lock line deleted\n",
1109 " by hand, or a number changed by hand, which is this row plus\n",
1110 " decl_added. A number the new side's lock retires is\n",
1111 " interface_retired instead, and `ridl baseline` refuses to\n",
1112 " publish the removal of a number it does not find retired\n",
1113 " (RIDL-412)\n",
1114 " caveat a composite member retired the sanctioned way — replaced by\n",
1115 " a `reserved` tombstone in its own slot (typl 7.4) — is also\n",
1116 " reported breaking today. The body comparison is keyed on\n",
1117 " member names and does not read the `reserved` list, so it\n",
1118 " cannot yet tell that retirement from a bare deletion. This\n",
1119 " errs on the safe side; carried as debt, see the note on\n",
1120 " `diff_composite`. The interaction-level tombstone IS\n",
1121 " recognised — see interaction_retired"
1122 ),
1123 Category::InterfaceRenamed => concat!(
1124 "An interface whose interfaces.lock number is the same on both sides and\n",
1125 "whose name changed.\n",
1126 " compatible always — the number is the interface's identity and its\n",
1127 " routing key (lock design 7, rsdl decision D-7), so nothing\n",
1128 " moves on the wire. Visible in source: a rename changes the\n",
1129 " generated identity-table names in both wire backends, which\n",
1130 " is why the text report lists it under the heading\n",
1131 " \"compatible on the wire, visible in source\". The path\n",
1132 " carries the new name and the detail old -> new; changes\n",
1133 " inside the interface are reported under the new name and\n",
1134 " classified as any other interface's\n",
1135 " note a provisional number is no identity: a declaration with no\n",
1136 " lock entry is never matched, so a rename the lock does not\n",
1137 " record is decl_removed plus decl_added, breaking, until\n",
1138 " `ridl lock <pkg> --rename Old=New` records it"
1139 ),
1140 Category::InterfaceRetired => concat!(
1141 "An interface whose number the old snapshot held is gone from the new\n",
1142 "snapshot, whose interfaces.lock retires it.\n",
1143 " compatible always — the sanctioned removal of an interface: the entry\n",
1144 " keeps its number forever with the word `retired`, so the\n",
1145 " number is never allocated again (lock design 4 and 7, rsdl\n",
1146 " decision D-7). A number gone with no retired entry is\n",
1147 " decl_removed, breaking, and `ridl baseline` refuses to publish\n",
1148 " it (RIDL-412)"
1149 ),
1150 Category::MemberReordered => concat!(
1151 "A surviving composite member whose slot in the body changed.\n",
1152 " breaking always — a struct field or union arm takes its wire\n",
1153 " identity from its ordinal, its 1-based place in the body\n",
1154 " counting tombstones (typl 7.4), so a member whose ordinal\n",
1155 " changed has a new wire identity; the detail carries the old\n",
1156 " and new ordinal. An enum value or enum-set bit carries an\n",
1157 " explicit number instead (typl 8, 9), but the walk compares\n",
1158 " positions, not those numbers, so a textual reorder of an\n",
1159 " enum or enum-set body is reported breaking as well,\n",
1160 " conservatively, even when no number changed; the detail\n",
1161 " carries the old and new position\n",
1162 " note reported only when both bodies hold the same member names:\n",
1163 " a reorder in the same edit as an addition or a removal is\n",
1164 " reported through decl_added or decl_removed alone. A reorder\n",
1165 " in the same edit as an in-place change to the body is\n",
1166 " reported with constraint_changed on the container as well"
1167 ),
1168 Category::InteractionAppended => concat!(
1169 "An interaction added after every slot that existed before.\n",
1170 " compatible the slot it takes was never occupied\n",
1171 " breaking the slot was freed by an untombstoned removal and is now\n",
1172 " reused by a new name — a reused wire identity (ADR-0008 d14)"
1173 ),
1174 Category::InteractionInserted => concat!(
1175 "An interaction added before the end of the interface body.\n",
1176 " breaking always — every later ordinal shifts, and the ordinal is the\n",
1177 " transport identity (ridl 11)"
1178 ),
1179 Category::InteractionReordered => concat!(
1180 "A surviving interaction whose relative order in the body changed.\n",
1181 " breaking always — a reorder shifts wire identities (ridl 11)"
1182 ),
1183 Category::InteractionRemoved => concat!(
1184 "An interaction removed without a `reserved` tombstone holding its slot.\n",
1185 " breaking always — the freed ordinal is reusable, so the wire identity\n",
1186 " is no longer permanent (ridl 11)"
1187 ),
1188 Category::InteractionRetired => concat!(
1189 "An interaction retired to a `reserved` tombstone in its own ordinal slot.\n",
1190 " compatible always — the sanctioned retirement: the slot stays occupied\n",
1191 " and every later ordinal holds (ridl 11)"
1192 ),
1193 Category::KindChanged => concat!(
1194 "An interaction whose kind changed (signal, event, command, query, fixed).\n",
1195 " breaking any direction — the kind selects the transport shape"
1196 ),
1197 Category::PayloadChanged => concat!(
1198 "A signal, event, or fixed payload type changed.\n",
1199 " breaking any direction, a stream added or removed included"
1200 ),
1201 Category::ReturnChanged => concat!(
1202 "A query return shape changed.\n",
1203 " breaking any direction — an ok-arm change, an error arm added, removed,\n",
1204 " or retyped, a stream added or removed, or any other change to\n",
1205 " the synthesized inline `T | E` transport identity (ADR-0008 d4:\n",
1206 " interface + interaction ordinal + ordered arm types)"
1207 ),
1208 Category::ParamsChanged => concat!(
1209 "A command or query parameter list changed.\n",
1210 " breaking any direction — a parameter added, removed, renamed, retyped,\n",
1211 " or a stream added or removed on one"
1212 ),
1213 Category::TimingChanged => concat!(
1214 "A signal or event resolved timing changed (ADR-0008 d12).\n",
1215 " compatible min raised (a higher rate floor) or max lowered (a tighter\n",
1216 " staleness bound) with the mode unchanged; default_applied\n",
1217 " flipped over identical resolved bounds — a default made\n",
1218 " explicit\n",
1219 " breaking min lowered, max raised, a bound added where none was, a bound\n",
1220 " removed, or the strict-periodic/range mode flipped\n",
1221 " note editing `[defaults].timing` needs no special rule: diff\n",
1222 " compares resolved bounds, so it surfaces here on every\n",
1223 " defaulted interaction (ridl 9.1)"
1224 ),
1225 Category::RpcBoundChanged => concat!(
1226 "A command or query declared RPC bound changed (ADR-0015 d8).\n",
1227 " compatible min lowered (the caller may call more often) or max lowered\n",
1228 " (a stronger provider promise), with the mode unchanged;\n",
1229 " default_applied flipped over identical resolved bounds — a\n",
1230 " default made explicit\n",
1231 " breaking min raised — on an RPC, min is the call throttle and\n",
1232 " constrains the caller, so raising it withdraws a call rate\n",
1233 " the caller was entitled to use; max raised (a weaker provider\n",
1234 " promise); a bound added or removed, the whole annotation\n",
1235 " included\n",
1236 " note the min direction is the inverse of timing_changed's, which\n",
1237 " is why this is a category of its own rather than a branch:\n",
1238 " a missed branch would inherit the signal rule and call a\n",
1239 " raised RPC min compatible (ADR-0012 d9, fail closed).\n",
1240 " an RPC bound may come from a default, and diff compares\n",
1241 " resolved bounds, so editing a default surfaces here on\n",
1242 " every defaulted command or query (ridl 9.1)"
1243 ),
1244 Category::ContractChanged => concat!(
1245 "A command or query require/ensure clause set changed (ridl 13).\n",
1246 " compatible a require removed, or an ensure added\n",
1247 " breaking a require added or its text changed, or an ensure removed or\n",
1248 " its text changed. Clause text is compared verbatim: the\n",
1249 " classifier does not prove that one clause implies another\n",
1250 " (ADR-0008 d14)"
1251 ),
1252 Category::WidthChanged => concat!(
1253 "A derived wire width or scalar backing changed.\n",
1254 " breaking any IntWidth or FloatWidth change, uint64 versus int64\n",
1255 " included — the resolved width is part of the contract\n",
1256 " (typl 4.2, 5.6)"
1257 ),
1258 Category::ConstraintChanged => concat!(
1259 "A scalar constraint changed, or a composite body changed in place.\n",
1260 " compatible widened — min lowered, max raised, a length bound loosened,\n",
1261 " a step removed, a match pattern removed (by literal or by\n",
1262 " named constant), or the whole constraint dropped so the\n",
1263 " value is unbounded again\n",
1264 " breaking narrowed — min raised, max lowered, a length bound\n",
1265 " tightened, a step added or changed (divisibility is never\n",
1266 " proved), a match pattern added or rewritten (by literal or\n",
1267 " by named constant), or a constraint appearing where there\n",
1268 " was none, which bounds a previously unbounded value. A\n",
1269 " composite body changed in place is breaking: the walk\n",
1270 " does not say which member changed inside it\n",
1271 " note each facet is judged on its own and any one narrowing\n",
1272 " decides the change, so a mixed edit is breaking on the half\n",
1273 " that narrows. A widening that flips the resolved wire width\n",
1274 " is separately reported as width_changed, which is always\n",
1275 " breaking (typl 5.6)"
1276 ),
1277 Category::InitChanged => concat!(
1278 "A declared or resolved init value changed.\n",
1279 " breaking any direction — the init is the value a consumer reads before\n",
1280 " the first publish, so it is part of the contract (ridl 9.1)"
1281 ),
1282 Category::ReservedNameRedeclared => concat!(
1283 "A name retired by a `reserved` tombstone is live again — an interaction\n",
1284 "inside an interface body.\n",
1285 " breaking always — a retired identity is never reused (ridl 11,\n",
1286 " RIDL-401)"
1287 ),
1288 Category::ServiceChanged => concat!(
1289 "A service switched between the named list and an inline shape.\n",
1290 " breaking always — extraction rewrites the transport identity of every\n",
1291 " fallible query in the shape: an inline shape derives it from\n",
1292 " the service's dotted name, a named interface from its own name\n",
1293 " (ADR-0008 d4, ADR-0015 d15). A changed list is not this\n",
1294 " category: it is read as a set by the service_interface_*\n",
1295 " rows (ADR-0015 d19, as amended 2026-09-15)"
1296 ),
1297 Category::ServiceInterfaceAdded => concat!(
1298 "An interface joined a service's set of interfaces.\n",
1299 " compatible always — nothing that existed moved: an interface's number\n",
1300 " comes from its package's interfaces.lock, not from its place\n",
1301 " in the list, and the routing key does not contain the service\n",
1302 " (ADR-0015 d19, as amended 2026-09-15)"
1303 ),
1304 Category::ServiceInterfaceRemoved => concat!(
1305 "An interface left a service's set of interfaces.\n",
1306 " compatible always — on the wire: the routing key does not contain the\n",
1307 " service, so no identity moves, and a split into two services\n",
1308 " is a removal plus an addition. Visible in source: the\n",
1309 " service.member addresses of that interface stop resolving\n",
1310 " under this service, which is why the text report lists it\n",
1311 " under the heading \"compatible on the wire, visible in\n",
1312 " source\". A consumer that loses its only provider is a\n",
1313 " wiring error for rsdl, not a package diff (ADR-0015 d19, as\n",
1314 " amended 2026-09-15)"
1315 ),
1316 Category::DocOnly => concat!(
1317 "Only doc comment, labels, or deprecation metadata changed.\n",
1318 " compatible always — none of it reaches a consumer's build.\n",
1319 " Visibility is NOT in this category: see\n",
1320 " visibility_changed"
1321 ),
1322 Category::VisibilityChanged => concat!(
1323 "The visibility a declaration is published at changed.\n",
1324 " compatible internal -> public: the declaration is offered to more\n",
1325 " consumers than before\n",
1326 " breaking public -> internal: `internal` maps to the target's\n",
1327 " package-private mechanism — Rust `pub(crate)`, a\n",
1328 " non-exported TypeScript member (ADR-0002 8) — so the\n",
1329 " declaration disappears from every out-of-package\n",
1330 " consumer's build. The wire layout does not move, but the\n",
1331 " consumer-visible guarantee narrows (ADR-0008 d14). Any\n",
1332 " direction involving an unset visibility is breaking"
1333 ),
1334 }
1335}