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::InterfaceFrozen => Verdict::Compatible,
112 Category::InterfaceNumberChanged => Verdict::Breaking,
113
114 Category::EnumReordered => Verdict::Compatible,
120
121 Category::ServiceInterfaceAdded | Category::ServiceInterfaceRemoved => Verdict::Compatible,
129
130 Category::VisibilityChanged => visibility(change, old, new),
131 Category::InteractionAppended => appended(change, old, new),
132 Category::DeclAdded => added(change, old, new, scope),
133 Category::ConstraintChanged => constraint(change, old, new),
134 Category::TimingChanged => timing(change, old, new),
135 Category::RpcBoundChanged => rpc_bound(change, old, new),
136 Category::ContractChanged => contract(change, old, new),
137 }
138}
139
140fn visibility(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
158 let (Some(old_visibility), Some(new_visibility)) = (
159 find_visibility(old, &change.path, |name| find_old_shape(old, new, name)),
160 find_visibility(new, &change.path, |name| find_shape(new, name)),
161 ) else {
162 return Verdict::Breaking;
163 };
164
165 match (
166 v2::Visibility::try_from(old_visibility),
167 v2::Visibility::try_from(new_visibility),
168 ) {
169 (Ok(v2::Visibility::Internal), Ok(v2::Visibility::Public)) => Verdict::Compatible,
170 _ => Verdict::Breaking,
171 }
172}
173
174fn find_visibility<'a>(
181 package: &'a v2::Package,
182 path: &str,
183 shape_of: impl Fn(&str) -> Option<v2::InterfaceShape<'a>>,
184) -> Option<i32> {
185 let mut segments = path.split('/').skip(1);
186 let name = segments.next()?;
187
188 if let Some(member) = segments.next() {
189 return shape_of(name)?
190 .interface
191 .interactions
192 .iter()
193 .find(|decl| decl.name == member)
194 .map(|decl| decl.visibility);
195 }
196 if let Some(decl) = find_decl(package, name) {
197 return Some(decl.visibility);
198 }
199 if let Some(shape) = shape_of(name) {
204 return Some(shape.visibility());
205 }
206 package
209 .services
210 .iter()
211 .find(|service| service.name == name)
212 .map(|service| service.visibility)
213}
214
215fn appended(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
230 let Some((container, member)) = member_path(change) else {
231 return Verdict::Breaking;
232 };
233 let (Some(old_iface), Some(new_iface)) = (
234 find_old_interface(old, new, container),
235 find_interface(new, container),
236 ) else {
237 return Verdict::Breaking;
238 };
239 let Some(ordinal) = slot_ordinal(new_iface, member) else {
240 return Verdict::Breaking;
241 };
242
243 for (name, old_ordinal) in slots(old_iface) {
244 if old_ordinal == ordinal && name != member {
245 return Verdict::Breaking;
246 }
247 }
248 Verdict::Compatible
249}
250
251fn added(change: &Change, old: &v2::Package, new: &v2::Package, scope: &[&v2::Package]) -> Verdict {
267 let Some((container, member)) = member_path(change) else {
268 return Verdict::Compatible;
271 };
272 let (Some(old_decl), Some(new_decl)) = (find_decl(old, container), find_decl(new, container))
273 else {
274 return Verdict::Breaking;
275 };
276
277 use v2::decl::Kind;
282 let appended = match (&old_decl.kind, &new_decl.kind) {
283 (Some(Kind::StructDef(old_def)), Some(Kind::StructDef(new_def))) => {
284 appended_slot(
285 &struct_slots(old_def),
286 &struct_reserved(old_def),
287 &struct_slots(new_def),
288 ) && absence_reads_as_legal(new_def, member, new, scope)
289 }
290 (Some(Kind::UnionDef(old_def)), Some(Kind::UnionDef(new_def))) => {
291 !old_def.is_result
294 && !new_def.is_result
295 && appended_slot(
296 &union_slots(old_def),
297 &union_reserved(old_def),
298 &union_slots(new_def),
299 )
300 }
301 (Some(Kind::EnumDef(old_def)), Some(Kind::EnumDef(new_def))) => appended_slot(
302 &value_slots(&old_def.values),
303 &reserved_values(&old_def.reserved),
304 &value_slots(&new_def.values),
305 ),
306 (Some(Kind::EnumSetDef(old_def)), Some(Kind::EnumSetDef(new_def))) => appended_slot(
307 &value_slots(&old_def.bits),
308 &[],
309 &value_slots(&new_def.bits),
310 ),
311 _ => false,
313 };
314
315 if appended {
316 Verdict::Compatible
317 } else {
318 Verdict::Breaking
319 }
320}
321
322pub fn absence_refused(
331 new_set: &[v2::Package],
332 context: &[v2::Package],
333 package: &str,
334 container: &str,
335 member: &str,
336) -> bool {
337 let scope: Vec<&v2::Package> = new_set.iter().chain(context).collect();
338 let Some(home) = new_set.iter().find(|candidate| candidate.name == package) else {
339 return false;
340 };
341 match find_decl(home, container).and_then(|decl| decl.kind.as_ref()) {
342 Some(v2::decl::Kind::StructDef(def)) => !absence_reads_as_legal(def, member, home, &scope),
343 _ => false,
344 }
345}
346
347fn absence_reads_as_legal(
367 def: &v2::StructDef,
368 member: &str,
369 home: &v2::Package,
370 scope: &[&v2::Package],
371) -> bool {
372 let Some(r#type) = def.members.iter().find_map(|slot| match &slot.member {
373 Some(v2::struct_member::Member::Field(field)) if field.name == member => {
374 field.r#type.as_ref()
375 }
376 _ => None,
377 }) else {
378 return false;
379 };
380 if r#type.optional {
381 return true;
382 }
383 use v2::field_type::Kind;
384 match &r#type.kind {
385 Some(Kind::Primitive(primitive)) => matches!(
386 v2::PrimitiveType::try_from(*primitive),
387 Ok(v2::PrimitiveType::Boolean | v2::PrimitiveType::Integer | v2::PrimitiveType::Float)
388 ),
389 Some(Kind::InlineScalar(def)) => scalar_holds_zero(def),
390 Some(Kind::Named(reference)) => {
391 use v2::decl::Kind as Decl;
392 match resolve(home, scope, reference).and_then(|decl| decl.kind.as_ref()) {
393 Some(Decl::TypeDef(def)) => scalar_holds_zero(def),
394 Some(Decl::EnumDef(def)) => ridl_ir::zero::enum_zero_member(&def.values).is_some(),
395 Some(Decl::EnumSetDef(_)) => true,
397 _ => false,
398 }
399 }
400 _ => false,
401 }
402}
403
404fn scalar_holds_zero(def: &v2::TypeDef) -> bool {
408 let numeric = match def
409 .backing
410 .as_ref()
411 .and_then(|backing| backing.kind.as_ref())
412 {
413 Some(v2::backing::Kind::Unit(_)) => true,
414 Some(v2::backing::Kind::Primitive(primitive)) => matches!(
415 v2::PrimitiveType::try_from(*primitive),
416 Ok(v2::PrimitiveType::Boolean | v2::PrimitiveType::Integer | v2::PrimitiveType::Float)
417 ),
418 None => false,
419 };
420 numeric
421 && def.constraint.as_ref().is_none_or(|constraint| {
422 ridl_ir::zero::range_holds_zero(
423 constraint.min.as_deref(),
424 constraint.max.as_deref(),
425 constraint.step.as_deref(),
426 )
427 })
428}
429
430fn resolve<'a>(
434 home: &'a v2::Package,
435 scope: &[&'a v2::Package],
436 reference: &str,
437) -> Option<&'a v2::Decl> {
438 match reference.rsplit_once('.') {
439 Some((package, name)) => std::iter::once(home)
440 .chain(scope.iter().copied())
441 .find(|candidate| candidate.name == package)
442 .and_then(|candidate| find_decl(candidate, name)),
443 None => find_decl(home, reference),
444 }
445}
446
447fn appended_slot(old: &[(String, i64)], old_retired: &[i64], new: &[(String, i64)]) -> bool {
458 for (name, old_slot) in old {
459 match new.iter().find(|(new_name, _)| new_name == name) {
460 Some((_, new_slot)) if new_slot == old_slot => {}
461 _ => return false,
463 }
464 }
465
466 let high_water = old
467 .iter()
468 .map(|(_, slot)| *slot)
469 .chain(old_retired.iter().copied())
470 .max();
471 let old_names: Vec<&String> = old.iter().map(|(name, _)| name).collect();
472 for (name, slot) in new {
473 if old_names.contains(&name) {
474 continue;
475 }
476 match high_water {
477 Some(mark) if *slot <= mark => return false,
478 _ => {}
479 }
480 }
481 true
482}
483
484fn constraint(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
496 let mut segments = change.path.split('/').skip(1);
497 let (Some(name), None) = (segments.next(), segments.next()) else {
498 return Verdict::Breaking;
499 };
500 let (Some(old_decl), Some(new_decl)) = (find_decl(old, name), find_decl(new, name)) else {
501 return Verdict::Breaking;
502 };
503
504 use v2::decl::Kind;
505 let (Some(Kind::TypeDef(old_def)), Some(Kind::TypeDef(new_def))) =
506 (&old_decl.kind, &new_decl.kind)
507 else {
508 return Verdict::Breaking;
509 };
510
511 if narrows(old_def.constraint.as_ref(), new_def.constraint.as_ref()) {
512 Verdict::Breaking
513 } else {
514 Verdict::Compatible
515 }
516}
517
518fn narrows(old: Option<&v2::Constraint>, new: Option<&v2::Constraint>) -> bool {
524 let (Some(old), Some(new)) = (old, new) else {
525 return old.is_none() && new.is_some();
528 };
529
530 if raised(old.min.as_deref(), new.min.as_deref())
532 || lowered(old.max.as_deref(), new.max.as_deref())
533 {
534 return true;
535 }
536 if raised_u64(old.len_min, new.len_min) || lowered_u64(old.len_max, new.len_max) {
538 return true;
539 }
540 if new.step.is_some() && old.step != new.step {
543 return true;
544 }
545 if (new.pattern.is_some() && old.pattern != new.pattern)
548 || (new.pattern_const.is_some() && old.pattern_const != new.pattern_const)
549 {
550 return true;
551 }
552 false
553}
554
555fn raised(old: Option<&str>, new: Option<&str>) -> bool {
557 match (old, new) {
558 (None, Some(_)) => true,
559 (Some(old), Some(new)) => cmp_decimal(new, old).is_none_or(std::cmp::Ordering::is_gt),
560 _ => false,
561 }
562}
563
564fn lowered(old: Option<&str>, new: Option<&str>) -> bool {
566 match (old, new) {
567 (None, Some(_)) => true,
568 (Some(old), Some(new)) => cmp_decimal(new, old).is_none_or(std::cmp::Ordering::is_lt),
569 _ => false,
570 }
571}
572
573fn raised_u64(old: Option<u64>, new: Option<u64>) -> bool {
574 match (old, new) {
575 (None, Some(_)) => true,
576 (Some(old), Some(new)) => new > old,
577 _ => false,
578 }
579}
580
581fn lowered_u64(old: Option<u64>, new: Option<u64>) -> bool {
582 match (old, new) {
583 (None, Some(_)) => true,
584 (Some(old), Some(new)) => new < old,
585 _ => false,
586 }
587}
588
589fn cmp_decimal(left: &str, right: &str) -> Option<std::cmp::Ordering> {
599 use std::cmp::Ordering;
600
601 let (left_negative, left_digits) = split_sign(left)?;
602 let (right_negative, right_digits) = split_sign(right)?;
603 if left_negative != right_negative {
604 return Some(if left_negative {
607 Ordering::Less
608 } else {
609 Ordering::Greater
610 });
611 }
612
613 let magnitude = cmp_magnitude(&left_digits, &right_digits)?;
614 Some(if left_negative {
615 magnitude.reverse()
616 } else {
617 magnitude
618 })
619}
620
621fn split_sign(text: &str) -> Option<(bool, String)> {
624 let (negative, rest) = match text.strip_prefix('-') {
625 Some(rest) => (true, rest),
626 None => (false, text.strip_prefix('+').unwrap_or(text)),
627 };
628 if rest.is_empty() || !rest.chars().all(|c| c.is_ascii_digit() || c == '.') {
629 return None;
630 }
631 if rest.matches('.').count() > 1 {
632 return None;
633 }
634 Some((negative, rest.to_string()))
635}
636
637fn cmp_magnitude(left: &str, right: &str) -> Option<std::cmp::Ordering> {
640 use std::cmp::Ordering;
641
642 let (left_int, left_frac) = left.split_once('.').unwrap_or((left, ""));
643 let (right_int, right_frac) = right.split_once('.').unwrap_or((right, ""));
644
645 let left_int = left_int.trim_start_matches('0');
646 let right_int = right_int.trim_start_matches('0');
647 let by_length = left_int.len().cmp(&right_int.len());
648 if by_length != Ordering::Equal {
649 return Some(by_length);
650 }
651 let by_int = left_int.cmp(right_int);
652 if by_int != Ordering::Equal {
653 return Some(by_int);
654 }
655
656 let width = left_frac.len().max(right_frac.len());
658 let pad = |frac: &str| format!("{frac:0<width$}");
659 Some(pad(left_frac).cmp(&pad(right_frac)))
660}
661
662fn timing(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
674 let Some((container, member)) = member_path(change) else {
675 return Verdict::Breaking;
676 };
677 let (Some(old_decl), Some(new_decl)) = (
678 find_old_interaction(old, new, container, member),
679 find_interaction(new, container, member),
680 ) else {
681 return Verdict::Breaking;
682 };
683 let (Some(old_timing), Some(new_timing)) =
684 (interaction_timing(old_decl), interaction_timing(new_decl))
685 else {
686 return Verdict::Breaking;
696 };
697
698 let (Some(old_timing), Some(new_timing)) = (old_timing, new_timing) else {
699 return Verdict::Breaking;
702 };
703
704 if old_timing.mode != new_timing.mode {
705 return Verdict::Breaking;
706 }
707 if lowered(old_timing.min_us.as_deref(), new_timing.min_us.as_deref())
709 || dropped(old_timing.min_us.as_deref(), new_timing.min_us.as_deref())
710 || raised(old_timing.max_us.as_deref(), new_timing.max_us.as_deref())
711 || dropped(old_timing.max_us.as_deref(), new_timing.max_us.as_deref())
712 {
713 return Verdict::Breaking;
714 }
715 Verdict::Compatible
718}
719
720fn rpc_bound(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
739 let Some((container, member)) = member_path(change) else {
740 return Verdict::Breaking;
741 };
742 let (Some(old_decl), Some(new_decl)) = (
743 find_old_interaction(old, new, container, member),
744 find_interaction(new, container, member),
745 ) else {
746 return Verdict::Breaking;
747 };
748 let (Some(old_timing), Some(new_timing)) = (rpc_timing(old_decl), rpc_timing(new_decl)) else {
749 return Verdict::Breaking;
756 };
757
758 let (Some(old_timing), Some(new_timing)) = (old_timing, new_timing) else {
759 return Verdict::Breaking;
762 };
763
764 if old_timing.mode != new_timing.mode {
767 return Verdict::Breaking;
768 }
769 if raised(old_timing.min_us.as_deref(), new_timing.min_us.as_deref())
774 || dropped(old_timing.min_us.as_deref(), new_timing.min_us.as_deref())
775 || raised(old_timing.max_us.as_deref(), new_timing.max_us.as_deref())
776 || dropped(old_timing.max_us.as_deref(), new_timing.max_us.as_deref())
777 {
778 return Verdict::Breaking;
779 }
780 Verdict::Compatible
786}
787
788fn dropped(old: Option<&str>, new: Option<&str>) -> bool {
790 old.is_some() && new.is_none()
791}
792
793fn interaction_timing(decl: &v2::Decl) -> Option<Option<&v2::Timing>> {
796 use v2::decl::Kind;
797 match &decl.kind {
798 Some(Kind::SignalDef(def)) => Some(def.timing.as_ref()),
799 Some(Kind::EventDef(def)) => Some(def.timing.as_ref()),
800 _ => None,
801 }
802}
803
804fn rpc_timing(decl: &v2::Decl) -> Option<Option<&v2::Timing>> {
808 use v2::decl::Kind;
809 match &decl.kind {
810 Some(Kind::CommandDef(def)) => Some(def.timing.as_ref()),
811 Some(Kind::QueryDef(def)) => Some(def.timing.as_ref()),
812 _ => None,
813 }
814}
815
816fn contract(change: &Change, old: &v2::Package, new: &v2::Package) -> Verdict {
830 let Some((container, member)) = member_path(change) else {
831 return Verdict::Breaking;
832 };
833 let (Some(old_decl), Some(new_decl)) = (
834 find_old_interaction(old, new, container, member),
835 find_interaction(new, container, member),
836 ) else {
837 return Verdict::Breaking;
838 };
839 let (Some(old_clauses), Some(new_clauses)) = (contracts(old_decl), contracts(new_decl)) else {
840 return Verdict::Breaking;
841 };
842
843 let kind = |want: v2::ContractKind| {
844 move |clause: &&v2::Contract| v2::ContractKind::try_from(clause.kind) == Ok(want)
845 };
846 let sources = |clauses: &[v2::Contract], want: v2::ContractKind| -> Vec<String> {
847 let mut out: Vec<String> = clauses
848 .iter()
849 .filter(kind(want))
850 .map(|clause| clause.source.clone())
851 .collect();
852 out.sort();
853 out
854 };
855
856 let old_require = sources(old_clauses, v2::ContractKind::Require);
857 let new_require = sources(new_clauses, v2::ContractKind::Require);
858 let old_ensure = sources(old_clauses, v2::ContractKind::Ensure);
859 let new_ensure = sources(new_clauses, v2::ContractKind::Ensure);
860
861 if !covers(&old_require, &new_require) || !covers(&new_ensure, &old_ensure) {
862 return Verdict::Breaking;
863 }
864 Verdict::Compatible
865}
866
867fn covers(superset: &[String], subset: &[String]) -> bool {
869 let mut remaining: Vec<&String> = superset.iter().collect();
870 for clause in subset {
871 match remaining.iter().position(|held| *held == clause) {
872 Some(index) => {
873 remaining.swap_remove(index);
874 }
875 None => return false,
876 }
877 }
878 true
879}
880
881fn contracts(decl: &v2::Decl) -> Option<&[v2::Contract]> {
883 use v2::decl::Kind;
884 match &decl.kind {
885 Some(Kind::CommandDef(def)) => Some(&def.contracts),
886 Some(Kind::QueryDef(def)) => Some(&def.contracts),
887 _ => None,
888 }
889}
890
891fn member_path(change: &Change) -> Option<(&str, &str)> {
898 let mut segments = change.path.split('/').skip(1);
899 let container = segments.next()?;
900 let member = segments.next()?;
901 if segments.next().is_some() {
902 return None;
903 }
904 Some((container, member))
905}
906
907fn find_decl<'a>(package: &'a v2::Package, name: &str) -> Option<&'a v2::Decl> {
908 package.decls.iter().find(|decl| decl.name == name)
909}
910
911fn find_shape<'a>(package: &'a v2::Package, name: &str) -> Option<v2::InterfaceShape<'a>> {
916 package.shapes().find(|shape| shape.name == name)
917}
918
919fn find_old_shape<'a>(
926 old: &'a v2::Package,
927 new: &v2::Package,
928 name: &str,
929) -> Option<v2::InterfaceShape<'a>> {
930 let by_number = find_shape(new, name)
931 .filter(|shape| frozen(shape.interface))
932 .and_then(|shape| {
933 old.shapes().find(|candidate| {
934 frozen(candidate.interface) && candidate.interface.number == shape.interface.number
935 })
936 });
937 by_number.or_else(|| find_shape(old, name))
938}
939
940fn find_interface<'a>(package: &'a v2::Package, name: &str) -> Option<&'a v2::Interface> {
941 find_shape(package, name).map(|shape| shape.interface)
942}
943
944fn find_old_interface<'a>(
945 old: &'a v2::Package,
946 new: &v2::Package,
947 name: &str,
948) -> Option<&'a v2::Interface> {
949 find_old_shape(old, new, name).map(|shape| shape.interface)
950}
951
952fn find_interaction<'a>(
953 package: &'a v2::Package,
954 container: &str,
955 member: &str,
956) -> Option<&'a v2::Decl> {
957 find_interface(package, container)?
958 .interactions
959 .iter()
960 .find(|decl| decl.name == member)
961}
962
963fn find_old_interaction<'a>(
966 old: &'a v2::Package,
967 new: &v2::Package,
968 container: &str,
969 member: &str,
970) -> Option<&'a v2::Decl> {
971 find_old_interface(old, new, container)?
972 .interactions
973 .iter()
974 .find(|decl| decl.name == member)
975}
976
977fn slots(interface: &v2::Interface) -> Vec<(&str, u32)> {
989 interface
990 .interactions
991 .iter()
992 .filter_map(|decl| match &decl.kind {
993 Some(v2::decl::Kind::ReservedSlot(reserved)) => {
994 Some((reserved.name.as_deref().unwrap_or(""), decl.ordinal))
995 }
996 Some(_) => Some((decl.name.as_str(), decl.ordinal)),
997 None => None,
998 })
999 .collect()
1000}
1001
1002fn slot_ordinal(interface: &v2::Interface, member: &str) -> Option<u32> {
1003 slots(interface)
1004 .into_iter()
1005 .find(|(name, _)| *name == member)
1006 .map(|(_, ordinal)| ordinal)
1007}
1008
1009pub(crate) fn struct_slots(def: &v2::StructDef) -> Vec<(String, i64)> {
1013 def.members
1014 .iter()
1015 .filter_map(|member| match &member.member {
1016 Some(v2::struct_member::Member::Field(field)) => {
1017 Some((field.name.clone(), i64::from(field.ordinal)))
1018 }
1019 _ => None,
1020 })
1021 .collect()
1022}
1023
1024fn struct_reserved(def: &v2::StructDef) -> Vec<i64> {
1025 def.members
1026 .iter()
1027 .filter_map(|member| match &member.member {
1028 Some(v2::struct_member::Member::Reserved(reserved)) => {
1029 Some(i64::from(reserved.ordinal))
1030 }
1031 _ => None,
1032 })
1033 .collect()
1034}
1035
1036pub(crate) fn union_slots(def: &v2::UnionDef) -> Vec<(String, i64)> {
1038 def.arms
1039 .iter()
1040 .map(|arm| (arm.name.clone(), i64::from(arm.ordinal)))
1041 .collect()
1042}
1043
1044fn union_reserved(def: &v2::UnionDef) -> Vec<i64> {
1045 def.reserved
1046 .iter()
1047 .map(|reserved| i64::from(reserved.ordinal))
1048 .collect()
1049}
1050
1051pub(crate) fn value_slots(values: &[v2::EnumValue]) -> Vec<(String, i64)> {
1054 values
1055 .iter()
1056 .map(|value| (value.name.clone(), value.value))
1057 .collect()
1058}
1059
1060fn reserved_values(reserved: &[v2::Reserved]) -> Vec<i64> {
1061 reserved.iter().filter_map(|entry| entry.value).collect()
1062}
1063
1064pub fn category_from_word(word: &str) -> Option<Category> {
1071 crate::CATEGORIES
1072 .into_iter()
1073 .find(|category| crate::category_word(*category) == word)
1074}
1075
1076#[deny(
1086 clippy::wildcard_enum_match_arm,
1087 clippy::match_wildcard_for_single_variants
1088)]
1089pub fn explain(category: Category) -> &'static str {
1090 match category {
1091 Category::DeclAdded => concat!(
1092 "A declaration, interface, or service present only in the new snapshot.\n",
1093 " compatible a new package-level decl, interface, or service; an enum\n",
1094 " value appended above every live and retired number; a\n",
1095 " union arm appended at the end of the body (typl 7.4,\n",
1096 " append-only); a struct field appended at the end of the body\n",
1097 " when it is optional, or when it is a scalar, enum, or enum set\n",
1098 " whose type allows the value 0\n",
1099 " breaking a member inserted below the highest slot ever used, a member\n",
1100 " taking a retired number, any addition that moves a surviving\n",
1101 " member's slot, or any arm of a result union (ADR-0008 d4); a\n",
1102 " non-optional struct field appended whose type does not allow\n",
1103 " 0 — a string, bytes, struct, union, tuple, array, or map, a\n",
1104 " scalar whose range or step excludes 0, an enum with no zero\n",
1105 " member, or a type the diff cannot resolve (ridl diff\n",
1106 " resolves ridl.std types). A reader of the new version\n",
1107 " refuses every payload of the old one, which does not carry\n",
1108 " the field (typl 7.4). Declare the new field optional instead\n",
1109 " (`field : T?`): an absent optional field reads as absent\n",
1110 " note an interface is matched by its interfaces.lock number (lock\n",
1111 " design 7): a frozen number the old side never held is a new\n",
1112 " interface, and so is every provisional one — a declaration\n",
1113 " with no lock entry carries no identity, and a rename keeps its\n",
1114 " number, so it is never a renamed interface (interface_renamed)"
1115 ),
1116 Category::DeclRemoved => concat!(
1117 "A declaration, interface, or service present only in the old snapshot.\n",
1118 " breaking a removed service, interface, decl, enum value, struct\n",
1119 " field, or union arm withdraws something a consumer compiled\n",
1120 " against\n",
1121 " note an interface is matched by its interfaces.lock number (lock\n",
1122 " design 7), so an interface here is one whose number is gone\n",
1123 " from the new side and not retired there — a lock line deleted\n",
1124 " by hand, or a number changed by hand, which is this row plus\n",
1125 " decl_added. A number the new side's lock retires is\n",
1126 " interface_retired instead, and `ridl baseline` refuses to\n",
1127 " publish the removal of a number it does not find retired\n",
1128 " (RIDL-412)\n",
1129 " caveat a composite member retired the sanctioned way — replaced by\n",
1130 " a `reserved` tombstone in its own slot (typl 7.4) — is also\n",
1131 " reported breaking today. The body comparison is keyed on\n",
1132 " member names and does not read the `reserved` list, so it\n",
1133 " cannot yet tell that retirement from a bare deletion. This\n",
1134 " errs on the safe side; carried as debt, see the note on\n",
1135 " `diff_composite`. The interaction-level tombstone IS\n",
1136 " recognised — see interaction_retired"
1137 ),
1138 Category::InterfaceRenamed => concat!(
1139 "An interface whose interfaces.lock number is the same on both sides and\n",
1140 "whose name changed.\n",
1141 " compatible always — the number is the interface's identity and its\n",
1142 " routing key (lock design 7, rsdl decision D-7), so nothing\n",
1143 " moves on the wire. Visible in source: a rename changes the\n",
1144 " generated identity-table names in both wire backends, which\n",
1145 " is why the text report lists it under the heading\n",
1146 " \"compatible on the wire, visible in source\". The path\n",
1147 " carries the new name and the detail old -> new; changes\n",
1148 " inside the interface are reported under the new name and\n",
1149 " classified as any other interface's\n",
1150 " note a provisional number is no identity: a declaration with no\n",
1151 " lock entry is never matched, so a rename the lock does not\n",
1152 " record is decl_removed plus decl_added, breaking, until\n",
1153 " `ridl lock <pkg> --rename Old=New` records it"
1154 ),
1155 Category::InterfaceRetired => concat!(
1156 "An interface whose number the old snapshot held is gone from the new\n",
1157 "snapshot, whose interfaces.lock retires it.\n",
1158 " compatible always — the sanctioned removal of an interface: the entry\n",
1159 " keeps its number forever with the word `retired`, so the\n",
1160 " number is never allocated again (lock design 4 and 7, rsdl\n",
1161 " decision D-7). A number gone with no retired entry is\n",
1162 " decl_removed, breaking, and `ridl baseline` refuses to publish\n",
1163 " it (RIDL-412)"
1164 ),
1165 Category::InterfaceFrozen => concat!(
1166 "An interface matched by name whose number is the same on both sides and\n",
1167 "whose provisional flag went from true to false: `ridl lock` froze the\n",
1168 "number in place.\n",
1169 " compatible always — the number is the routing key and it did not\n",
1170 " change, so nothing moves on the wire. The catalog hash\n",
1171 " changes, because it covers each interface's number and its\n",
1172 " provisional flag, and a generated face refuses a port whose\n",
1173 " catalog hash is another one (ADR-0023 decision 8): a peer\n",
1174 " built before the lock and a peer built after it refuse each\n",
1175 " other. This is why the text report lists it under the\n",
1176 " heading \"compatible on the wire, may change the catalog\n",
1177 " hash\".\n",
1178 " The detail carries the number before and after, marked\n",
1179 " (provisional) on the old side\n",
1180 " note a baseline never holds a provisional number (`ridl baseline`\n",
1181 " refuses one, RIDL-411), so this change is seen between two\n",
1182 " builds, or in a reversed comparison whose old side is a\n",
1183 " build. A number 0 on either side, from a snapshot written\n",
1184 " before the lock existed, is not compared"
1185 ),
1186 Category::InterfaceNumberChanged => concat!(
1187 "An interface matched across the two sides whose number differs.\n",
1188 " breaking always — the number is the interface's routing key, so a\n",
1189 " peer built against the old number cannot reach the\n",
1190 " interface at the new one. Seen between two provisional\n",
1191 " builds when a sibling interface added before this one in\n",
1192 " byte order moves its provisional number, and when `ridl\n",
1193 " lock` freezes a provisional number as another number. The\n",
1194 " detail carries the old and the new number, each marked\n",
1195 " (provisional) when it is\n",
1196 " note two frozen numbers are matched by number, so they never\n",
1197 " differ here: a frozen number changed by hand is\n",
1198 " decl_removed plus decl_added. A number 0 on either side,\n",
1199 " from a snapshot written before the lock existed, is not\n",
1200 " compared.\n",
1201 " Run `ridl lock` to freeze the numbers, so that a later\n",
1202 " sibling cannot move them"
1203 ),
1204 Category::MemberReordered => concat!(
1205 "A surviving composite member whose slot in the body changed.\n",
1206 " breaking always — a struct field or union arm takes its wire\n",
1207 " identity from its ordinal, its 1-based place in the body\n",
1208 " counting tombstones (typl 7.4), so a member whose ordinal\n",
1209 " changed has a new wire identity; the detail carries the old\n",
1210 " and new ordinal. An enum value or enum-set bit takes its\n",
1211 " identity from the explicit number it declares (typl 8, 9),\n",
1212 " so one whose number changed has a new wire identity; the\n",
1213 " detail carries the old and new value or bit. An enum or\n",
1214 " enum-set body reordered with no number changed is\n",
1215 " enum_reordered, compatible, instead\n",
1216 " note reported only when both bodies hold the same member names:\n",
1217 " a reorder in the same edit as an addition or a removal is\n",
1218 " reported through decl_added or decl_removed alone. A reorder\n",
1219 " in the same edit as an in-place change to the body is\n",
1220 " reported with constraint_changed on the container as well.\n",
1221 " An enum or enum-set body whose only differences are changed\n",
1222 " numbers, in any order, reports member_reordered alone"
1223 ),
1224 Category::EnumReordered => concat!(
1225 "An enum or enum-set body whose difference is only order: its values or\n",
1226 "bits reordered with no number changed, the enum's reserved list\n",
1227 "reordered with no entry added, removed or changed, or both.\n",
1228 " compatible always — a value or bit takes its identity from the\n",
1229 " explicit number it declares (typl 8, 9), so nothing moves\n",
1230 " on the wire. The catalog hash covers only the declarations\n",
1231 " an interface reaches. When an interface reaches the enum or\n",
1232 " enum set, the order of its values or bits, and of an enum's\n",
1233 " reserved list, is part of the hash, so the hash changes\n",
1234 " then, and a generated face refuses a port whose catalog hash\n",
1235 " is another one (ADR-0023 decision 8). This is why the text\n",
1236 " report lists it under the heading \"compatible on the wire,\n",
1237 " may change the catalog hash\". The path is the container's;\n",
1238 " there is no detail\n",
1239 " note a value or bit whose number changed is member_reordered,\n",
1240 " breaking, not enum_reordered"
1241 ),
1242 Category::InteractionAppended => concat!(
1243 "An interaction added after every slot that existed before.\n",
1244 " compatible the slot it takes was never occupied\n",
1245 " breaking the slot was freed by an untombstoned removal and is now\n",
1246 " reused by a new name — a reused wire identity (ADR-0008 d14)"
1247 ),
1248 Category::InteractionInserted => concat!(
1249 "An interaction added before the end of the interface body.\n",
1250 " breaking always — every later ordinal shifts, and the ordinal is the\n",
1251 " transport identity (ridl 11)"
1252 ),
1253 Category::InteractionReordered => concat!(
1254 "A surviving interaction whose relative order in the body changed.\n",
1255 " breaking always — a reorder shifts wire identities (ridl 11)"
1256 ),
1257 Category::InteractionRemoved => concat!(
1258 "An interaction removed without a `reserved` tombstone holding its slot.\n",
1259 " breaking always — the freed ordinal is reusable, so the wire identity\n",
1260 " is no longer permanent (ridl 11)"
1261 ),
1262 Category::InteractionRetired => concat!(
1263 "An interaction retired to a `reserved` tombstone in its own ordinal slot.\n",
1264 " compatible always — the sanctioned retirement: the slot stays occupied\n",
1265 " and every later ordinal holds (ridl 11)"
1266 ),
1267 Category::KindChanged => concat!(
1268 "An interaction whose kind changed (signal, event, command, query, fixed).\n",
1269 " breaking any direction — the kind selects the transport shape"
1270 ),
1271 Category::PayloadChanged => concat!(
1272 "A signal, event, or fixed payload type changed.\n",
1273 " breaking any direction, a stream added or removed included"
1274 ),
1275 Category::ReturnChanged => concat!(
1276 "A query return shape changed.\n",
1277 " breaking any direction — an ok-arm change, an error arm added, removed,\n",
1278 " or retyped, a stream added or removed, or any other change to\n",
1279 " the synthesized inline `T | E` transport identity (ADR-0008 d4:\n",
1280 " interface + interaction ordinal + ordered arm types)"
1281 ),
1282 Category::ParamsChanged => concat!(
1283 "A command or query parameter list changed.\n",
1284 " breaking any direction — a parameter added, removed, renamed, retyped,\n",
1285 " or a stream added or removed on one"
1286 ),
1287 Category::TimingChanged => concat!(
1288 "A signal or event resolved timing changed (ADR-0008 d12).\n",
1289 " compatible min raised (a higher rate floor) or max lowered (a tighter\n",
1290 " staleness bound) with the mode unchanged; default_applied\n",
1291 " flipped over identical resolved bounds — a default made\n",
1292 " explicit\n",
1293 " breaking min lowered, max raised, a bound added where none was, a bound\n",
1294 " removed, or the strict-periodic/range mode flipped\n",
1295 " note editing `[defaults].timing` needs no special rule: diff\n",
1296 " compares resolved bounds, so it surfaces here on every\n",
1297 " defaulted interaction (ridl 9.1)"
1298 ),
1299 Category::RpcBoundChanged => concat!(
1300 "A command or query declared RPC bound changed (ADR-0015 d8).\n",
1301 " compatible min lowered (the caller may call more often) or max lowered\n",
1302 " (a stronger provider promise), with the mode unchanged;\n",
1303 " default_applied flipped over identical resolved bounds — a\n",
1304 " default made explicit\n",
1305 " breaking min raised — on an RPC, min is the call throttle and\n",
1306 " constrains the caller, so raising it withdraws a call rate\n",
1307 " the caller was entitled to use; max raised (a weaker provider\n",
1308 " promise); a bound added or removed, the whole annotation\n",
1309 " included\n",
1310 " note the min direction is the inverse of timing_changed's, which\n",
1311 " is why this is a category of its own rather than a branch:\n",
1312 " a missed branch would inherit the signal rule and call a\n",
1313 " raised RPC min compatible (ADR-0012 d9, fail closed).\n",
1314 " an RPC bound may come from a default, and diff compares\n",
1315 " resolved bounds, so editing a default surfaces here on\n",
1316 " every defaulted command or query (ridl 9.1)"
1317 ),
1318 Category::ContractChanged => concat!(
1319 "A command or query require/ensure clause set changed (ridl 13).\n",
1320 " compatible a require removed, or an ensure added\n",
1321 " breaking a require added or its text changed, or an ensure removed or\n",
1322 " its text changed. Clause text is compared verbatim: the\n",
1323 " classifier does not prove that one clause implies another\n",
1324 " (ADR-0008 d14)"
1325 ),
1326 Category::WidthChanged => concat!(
1327 "A derived wire width or scalar backing changed.\n",
1328 " breaking any IntWidth or FloatWidth change, uint64 versus int64\n",
1329 " included — the resolved width is part of the contract\n",
1330 " (typl 4.2, 5.6)"
1331 ),
1332 Category::ConstraintChanged => concat!(
1333 "A scalar constraint changed, or a composite body changed in place.\n",
1334 " compatible widened — min lowered, max raised, a length bound loosened,\n",
1335 " a step removed, a match pattern removed (by literal or by\n",
1336 " named constant), or the whole constraint dropped so the\n",
1337 " value is unbounded again\n",
1338 " breaking narrowed — min raised, max lowered, a length bound\n",
1339 " tightened, a step added or changed (divisibility is never\n",
1340 " proved), a match pattern added or rewritten (by literal or\n",
1341 " by named constant), or a constraint appearing where there\n",
1342 " was none, which bounds a previously unbounded value. A\n",
1343 " composite body changed in place is breaking: the walk\n",
1344 " does not say which member changed inside it\n",
1345 " note each facet is judged on its own and any one narrowing\n",
1346 " decides the change, so a mixed edit is breaking on the half\n",
1347 " that narrows. A widening that flips the resolved wire width\n",
1348 " is separately reported as width_changed, which is always\n",
1349 " breaking (typl 5.6)"
1350 ),
1351 Category::InitChanged => concat!(
1352 "A declared or resolved init value changed.\n",
1353 " breaking any direction — the init is the value a consumer reads before\n",
1354 " the first publish, so it is part of the contract (ridl 9.1)"
1355 ),
1356 Category::ReservedNameRedeclared => concat!(
1357 "A name retired by a `reserved` tombstone is live again — an interaction\n",
1358 "inside an interface body.\n",
1359 " breaking always — a retired identity is never reused (ridl 11,\n",
1360 " RIDL-401)"
1361 ),
1362 Category::ServiceChanged => concat!(
1363 "A service switched between the named list and an inline shape.\n",
1364 " breaking always — extraction rewrites the transport identity of every\n",
1365 " fallible query in the shape: an inline shape derives it from\n",
1366 " the service's dotted name, a named interface from its own name\n",
1367 " (ADR-0008 d4, ADR-0015 d15). A changed list is not this\n",
1368 " category: it is read as a set by the service_interface_*\n",
1369 " rows (ADR-0015 d19, as amended 2026-09-15)"
1370 ),
1371 Category::ServiceInterfaceAdded => concat!(
1372 "An interface joined a service's set of interfaces.\n",
1373 " compatible always — nothing that existed moved: an interface's number\n",
1374 " comes from its unit's interfaces.lock, not from its place\n",
1375 " in the list, and the routing key does not contain the service\n",
1376 " (ADR-0015 d19, as amended 2026-09-15)"
1377 ),
1378 Category::ServiceInterfaceRemoved => concat!(
1379 "An interface left a service's set of interfaces.\n",
1380 " compatible always — on the wire: the routing key does not contain the\n",
1381 " service, so no identity moves, and a split into two services\n",
1382 " is a removal plus an addition. Visible in source: the\n",
1383 " service.member addresses of that interface stop resolving\n",
1384 " under this service, which is why the text report lists it\n",
1385 " under the heading \"compatible on the wire, visible in\n",
1386 " source\". A consumer that loses its only provider is a\n",
1387 " wiring error for rsdl, not a package diff (ADR-0015 d19, as\n",
1388 " amended 2026-09-15)"
1389 ),
1390 Category::DocOnly => concat!(
1391 "Only doc comment, labels, or deprecation metadata changed.\n",
1392 " compatible always — none of it reaches a consumer's build.\n",
1393 " Visibility is NOT in this category: see\n",
1394 " visibility_changed"
1395 ),
1396 Category::VisibilityChanged => concat!(
1397 "The visibility a declaration is published at changed.\n",
1398 " compatible internal -> public: the declaration is offered to more\n",
1399 " consumers than before\n",
1400 " breaking public -> internal: `internal` maps to the target's\n",
1401 " package-private mechanism — Rust `pub(crate)`, a\n",
1402 " non-exported TypeScript member (ADR-0002 8) — so the\n",
1403 " declaration disappears from every out-of-package\n",
1404 " consumer's build. The wire layout does not move, but the\n",
1405 " consumer-visible guarantee narrows (ADR-0008 d14). Any\n",
1406 " direction involving an unset visibility is breaking"
1407 ),
1408 }
1409}