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