|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +use crate::model::{ |
| 4 | + declaration::{Declaration, Namespace}, |
| 5 | + graph::{BASIC_OBJECT_ID, Graph, OBJECT_ID}, |
| 6 | + ids::DeclarationId, |
| 7 | +}; |
| 8 | + |
| 9 | +#[derive(Debug, PartialEq, Eq)] |
| 10 | +pub enum IntegrityErrorKind { |
| 11 | + /// A declaration's owner is not a namespace (module, class, or singleton class) |
| 12 | + OwnerIsNotNamespace, |
| 13 | + /// A declaration's owner does not exist in the graph |
| 14 | + OwnerDoesNotExist, |
| 15 | + /// A singleton class chain never resolves to a non-singleton namespace |
| 16 | + SingletonClassChainDoesNotTerminate, |
| 17 | + /// A non-root declaration unexpectedly owns itself |
| 18 | + UnexpectedSelfOwnership, |
| 19 | +} |
| 20 | + |
| 21 | +/// An integrity error found during graph validation |
| 22 | +#[derive(Debug, PartialEq, Eq)] |
| 23 | +pub struct IntegrityError { |
| 24 | + kind: IntegrityErrorKind, |
| 25 | + declaration_name: String, |
| 26 | + uris: Vec<String>, |
| 27 | +} |
| 28 | + |
| 29 | +impl fmt::Display for IntegrityError { |
| 30 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 31 | + let message = match self.kind { |
| 32 | + IntegrityErrorKind::OwnerIsNotNamespace => { |
| 33 | + format!("Declaration `{}` is owned by a non-namespace", self.declaration_name) |
| 34 | + } |
| 35 | + IntegrityErrorKind::OwnerDoesNotExist => { |
| 36 | + format!( |
| 37 | + "Declaration `{}` has an owner that does not exist in the graph", |
| 38 | + self.declaration_name |
| 39 | + ) |
| 40 | + } |
| 41 | + IntegrityErrorKind::SingletonClassChainDoesNotTerminate => { |
| 42 | + format!( |
| 43 | + "Singleton class `{}` does not eventually attach to a non-singleton namespace", |
| 44 | + self.declaration_name |
| 45 | + ) |
| 46 | + } |
| 47 | + IntegrityErrorKind::UnexpectedSelfOwnership => { |
| 48 | + format!("Declaration `{}` unexpectedly owns itself", self.declaration_name) |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + write!(f, "{message}. Defined in: {}", self.uris.join(", ")) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl std::error::Error for IntegrityError {} |
| 57 | + |
| 58 | +/// Checks the integrity of the graph data |
| 59 | +#[must_use] |
| 60 | +pub fn check_integrity(graph: &Graph) -> Vec<IntegrityError> { |
| 61 | + let mut errors = Vec::new(); |
| 62 | + let self_owners = [*OBJECT_ID, *BASIC_OBJECT_ID]; |
| 63 | + |
| 64 | + for (id, declaration) in graph.declarations() { |
| 65 | + let owner_id = declaration.owner_id(); |
| 66 | + |
| 67 | + // Check for constants that own themselves. Only `Object` and `BasicObject` own themselves and no other constant |
| 68 | + if *id == *owner_id { |
| 69 | + if self_owners.contains(id) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + errors.push(IntegrityError { |
| 73 | + kind: IntegrityErrorKind::UnexpectedSelfOwnership, |
| 74 | + declaration_name: declaration.name().to_string(), |
| 75 | + uris: collect_uris(graph, declaration), |
| 76 | + }); |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + // Check that the owner exists |
| 81 | + let Some(owner) = graph.declarations().get(owner_id) else { |
| 82 | + errors.push(IntegrityError { |
| 83 | + kind: IntegrityErrorKind::OwnerDoesNotExist, |
| 84 | + declaration_name: declaration.name().to_string(), |
| 85 | + uris: collect_uris(graph, declaration), |
| 86 | + }); |
| 87 | + continue; |
| 88 | + }; |
| 89 | + |
| 90 | + // Check that the owner is a namespace |
| 91 | + if owner.as_namespace().is_none() { |
| 92 | + errors.push(IntegrityError { |
| 93 | + kind: IntegrityErrorKind::OwnerIsNotNamespace, |
| 94 | + declaration_name: declaration.name().to_string(), |
| 95 | + uris: collect_uris(graph, declaration), |
| 96 | + }); |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + // Check singleton class chain termination |
| 101 | + if let Declaration::Namespace(Namespace::SingletonClass(_)) = declaration |
| 102 | + && !singleton_chain_terminates(graph, *owner_id) |
| 103 | + { |
| 104 | + errors.push(IntegrityError { |
| 105 | + kind: IntegrityErrorKind::SingletonClassChainDoesNotTerminate, |
| 106 | + declaration_name: declaration.name().to_string(), |
| 107 | + uris: collect_uris(graph, declaration), |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + errors |
| 113 | +} |
| 114 | + |
| 115 | +/// Collects the URIs where a declaration is defined, sorted and deduplicated |
| 116 | +fn collect_uris(graph: &Graph, declaration: &Declaration) -> Vec<String> { |
| 117 | + declaration |
| 118 | + .definitions() |
| 119 | + .iter() |
| 120 | + .map(|def_id| { |
| 121 | + let definition = graph.definitions().get(def_id).unwrap(); |
| 122 | + let document = graph.documents().get(definition.uri_id()).unwrap(); |
| 123 | + document.uri().to_string() |
| 124 | + }) |
| 125 | + .collect() |
| 126 | +} |
| 127 | + |
| 128 | +/// Walks the singleton class chain to verify that it eventually finds a module or class as its attached object |
| 129 | +fn singleton_chain_terminates(graph: &Graph, start_owner_id: DeclarationId) -> bool { |
| 130 | + const MAX_SINGLETON_DEPTH: usize = 128; |
| 131 | + let mut current_id = start_owner_id; |
| 132 | + |
| 133 | + for _ in 0..MAX_SINGLETON_DEPTH { |
| 134 | + let Some(current) = graph.declarations().get(¤t_id) else { |
| 135 | + return false; |
| 136 | + }; |
| 137 | + |
| 138 | + match current { |
| 139 | + Declaration::Namespace(Namespace::SingletonClass(_)) => { |
| 140 | + current_id = *current.owner_id(); |
| 141 | + } |
| 142 | + Declaration::Namespace(_) => return true, |
| 143 | + _ => return false, |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + false |
| 148 | +} |
0 commit comments