Get the schema an IR node belongs to.
(self, node: object)
| 216 | return self.schema.source_packages[source_key] |
| 217 | |
| 218 | def _schema_for_node(self, node: object) -> Schema: |
| 219 | """Get the schema an IR node belongs to.""" |
| 220 | file_path = node.location.file |
| 221 | source_key = str(Path(file_path).resolve()) |
| 222 | # `node` belongs to the self schema. |
| 223 | if source_key == str(Path(self.schema.source_file).resolve()): |
| 224 | return self.schema |
| 225 | # `node` belongs to an imported schema. |
| 226 | if not hasattr(self, "_source_schema_cache"): |
| 227 | self._source_schema_cache: Dict[str, Schema] = {} |
| 228 | if source_key in self._source_schema_cache: |
| 229 | return self._source_schema_cache[source_key] |
| 230 | enums = [ |
| 231 | enum |
| 232 | for enum in self.schema.enums |
| 233 | if str(Path(enum.location.file).resolve()) == source_key |
| 234 | ] |
| 235 | unions = [ |
| 236 | union |
| 237 | for union in self.schema.unions |
| 238 | if str(Path(union.location.file).resolve()) == source_key |
| 239 | ] |
| 240 | messages = [ |
| 241 | message |
| 242 | for message in self.schema.messages |
| 243 | if str(Path(message.location.file).resolve()) == source_key |
| 244 | ] |
| 245 | services = [ |
| 246 | service |
| 247 | for service in self.schema.services |
| 248 | if str(Path(service.location.file).resolve()) == source_key |
| 249 | ] |
| 250 | if enums or unions or messages or services: |
| 251 | schema = Schema( |
| 252 | package=self._package_for_source_file(file_path), |
| 253 | enums=enums, |
| 254 | messages=messages, |
| 255 | unions=unions, |
| 256 | services=services, |
| 257 | source_file=file_path, |
| 258 | source_format=self.schema.source_format, |
| 259 | ) |
| 260 | self._source_schema_cache[source_key] = schema |
| 261 | return schema |
| 262 | raise ValueError( |
| 263 | f"Rust generator cannot find source schema for " |
| 264 | f"{type(node).__name__} {getattr(node, 'name', '<unnamed>')!r}" |
| 265 | ) |
| 266 | |
| 267 | def _local_top_level_types( |
| 268 | self, schema: Schema |
no test coverage detected