1use std::collections::BTreeSet;
4use std::sync::LazyLock;
5
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8
9type Mapping = serde_json::Map<String, Value>;
10use thiserror::Error;
11
12use crate::SourceLanguage;
13use crate::markers::MarkerSet;
14
15const MAX_ASYNCAPI_BYTES: usize = 4 * 1024 * 1024;
16const MAX_SOURCE_BYTES: usize = 1024 * 1024;
17const MAX_OBSERVATIONS: usize = 1_024;
18const MAX_SCHEMA_FIELDS: usize = 256;
19const MAX_EVIDENCE_TEXT_CHARS: usize = 160;
20const MAX_ERROR_CHARS: usize = 512;
21const MAX_REFERENCE_DEPTH: usize = 16;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
25#[serde(rename_all = "snake_case")]
26pub enum EventBroker {
27 Kafka,
29 RabbitMq,
31 AwsSns,
33 AwsSqs,
35 Nats,
37 GooglePubSub,
39 Generic,
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
45#[serde(rename_all = "snake_case")]
46pub enum EventRole {
47 Publisher,
49 Subscriber,
51 Declaration,
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
57#[serde(rename_all = "snake_case")]
58pub enum DeliverySemantics {
59 AtMostOnce,
61 AtLeastOnce,
63 ExactlyOnce,
65 BestEffort,
67}
68
69#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
73pub struct EventEvidenceLine {
74 pub line: u32,
76 pub text: String,
78}
79
80#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
82pub struct EventSchemaField {
83 pub name: String,
85 pub field_type: Option<String>,
87 pub required: bool,
89}
90
91#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
93pub struct EventSchemaDefinition {
94 pub name: Option<String>,
96 pub version: Option<String>,
98 pub schema_format: Option<String>,
100 pub fields: Vec<EventSchemaField>,
102}
103
104#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
106pub struct EventObservation {
107 pub broker: EventBroker,
109 pub role: EventRole,
111 pub channel: Option<String>,
113 pub namespace: Option<String>,
115 pub protocol: Option<String>,
117 pub event_type: Option<String>,
119 pub schema: Option<EventSchemaDefinition>,
121 pub partition_key: Option<String>,
123 pub routing_key: Option<String>,
125 pub delivery_semantics: Option<DeliverySemantics>,
127 pub dead_letter_channel: Option<String>,
129 pub language: Option<SourceLanguage>,
131 pub evidence: Vec<EventEvidenceLine>,
133 pub confidence: f32,
135 pub incomplete: bool,
137 pub warnings: Vec<String>,
139}
140
141#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
143pub struct EventDocument {
144 pub source_path: Option<String>,
146 pub specification: Option<String>,
148 pub specification_version: Option<String>,
150 pub observations: Vec<EventObservation>,
152 pub warnings: Vec<String>,
154 pub incomplete: bool,
156}
157
158#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
160#[non_exhaustive]
161pub enum EventExtractionError {
162 #[error("event contract `{source_path}` exceeds the {limit_bytes}-byte input limit")]
164 InputTooLarge {
165 source_path: String,
167 limit_bytes: usize,
169 },
170 #[error("invalid AsyncAPI document `{source_path}`: {message}")]
172 InvalidSyntax {
173 source_path: String,
175 message: String,
177 },
178 #[error("AsyncAPI document `{source_path}` must contain a top-level object")]
180 InvalidRoot {
181 source_path: String,
183 },
184 #[error("unsupported AsyncAPI version in `{source_path}`: {version}")]
186 UnsupportedVersion {
187 source_path: String,
189 version: String,
191 },
192 #[error("invalid AsyncAPI structure in `{source_path}`: {message}")]
194 InvalidStructure {
195 source_path: String,
197 message: String,
199 },
200}
201
202pub fn extract_asyncapi(
213 source_path: &str,
214 input: &str,
215) -> Result<EventDocument, EventExtractionError> {
216 if input.len() > MAX_ASYNCAPI_BYTES {
217 return Err(EventExtractionError::InputTooLarge {
218 source_path: source_path.to_owned(),
219 limit_bytes: MAX_ASYNCAPI_BYTES,
220 });
221 }
222 let root = parse_asyncapi_value(source_path, input)?;
223 let root_map = root
224 .as_object()
225 .ok_or_else(|| EventExtractionError::InvalidRoot {
226 source_path: source_path.to_owned(),
227 })?;
228 let version = mapping_string(root_map, "asyncapi").ok_or_else(|| {
229 EventExtractionError::UnsupportedVersion {
230 source_path: source_path.to_owned(),
231 version: "missing".to_owned(),
232 }
233 })?;
234 let major = if version.starts_with("2.") {
235 2
236 } else if version.starts_with("3.") {
237 3
238 } else {
239 return Err(EventExtractionError::UnsupportedVersion {
240 source_path: source_path.to_owned(),
241 version: bounded_text(version, MAX_ERROR_CHARS),
242 });
243 };
244
245 let mut context = AsyncApiContext::new(source_path, input, &root, version);
246 if major == 2 {
247 extract_asyncapi_v2(&mut context)?;
248 } else {
249 extract_asyncapi_v3(&mut context)?;
250 }
251 Ok(context.finish())
252}
253
254fn parse_asyncapi_value(source_path: &str, input: &str) -> Result<Value, EventExtractionError> {
255 let result = if input.trim_start().starts_with(['{', '[']) {
256 serde_json::from_str(input).map_err(|error| error.to_string())
257 } else {
258 crate::yaml::from_str(input).map_err(|error| error.to_string())
259 };
260 result.map_err(|message| EventExtractionError::InvalidSyntax {
261 source_path: source_path.to_owned(),
262 message: bounded_text(&message, MAX_ERROR_CHARS),
263 })
264}
265
266#[must_use]
272pub fn parse_event_source(language: SourceLanguage, input: &str) -> EventDocument {
273 parse_event_source_with_prefilter(language, input, true)
274}
275
276fn parse_event_source_with_prefilter(
277 language: SourceLanguage,
278 input: &str,
279 prefilter: bool,
280) -> EventDocument {
281 let (source, truncated) = bounded_source(input);
282 let mut document = EventDocument::default();
283 if truncated {
284 document.incomplete = true;
285 document.warnings.push(format!(
286 "source input truncated at {MAX_SOURCE_BYTES} bytes"
287 ));
288 }
289 if input.contains('\0') {
290 document.incomplete = true;
291 document
292 .warnings
293 .push("source input contains a NUL byte".to_owned());
294 }
295 if prefilter && !EVENT_MARKERS.any_in(source) {
298 finish_document(&mut document);
299 return document;
300 }
301 let sanitized = sanitize_source(language, source);
302
303 for index in 0..sanitized.len() {
304 if document.observations.len() >= MAX_OBSERVATIONS {
305 document.incomplete = true;
306 document.warnings.push(format!(
307 "observations truncated at {MAX_OBSERVATIONS} items"
308 ));
309 break;
310 }
311 let Some(candidate) = source_candidate(&sanitized, index) else {
312 continue;
313 };
314 let Some(recognition) = recognize_source_call(&candidate) else {
315 continue;
316 };
317 let line_number = u32::try_from(index + 1).unwrap_or(u32::MAX);
318 append_source_observations(
319 &mut document.observations,
320 language,
321 line_number,
322 &candidate,
323 recognition,
324 );
325 }
326 finish_document(&mut document);
327 document
328}
329
330struct AsyncApiContext<'a> {
331 source_path: &'a str,
332 input: &'a str,
333 root: &'a Value,
334 version: &'a str,
335 observations: Vec<EventObservation>,
336 warnings: Vec<String>,
337 incomplete: bool,
338 server: ServerFacts,
339}
340
341impl<'a> AsyncApiContext<'a> {
342 fn new(source_path: &'a str, input: &'a str, root: &'a Value, version: &'a str) -> Self {
343 let (server, warnings) = global_server_facts(root);
344 Self {
345 source_path,
346 input,
347 root,
348 version,
349 observations: Vec::new(),
350 incomplete: !warnings.is_empty(),
351 warnings,
352 server,
353 }
354 }
355
356 fn push(&mut self, mut observation: EventObservation) {
357 if self.observations.len() >= MAX_OBSERVATIONS {
358 self.incomplete = true;
359 self.warnings.push(format!(
360 "observations truncated at {MAX_OBSERVATIONS} items"
361 ));
362 return;
363 }
364 normalize_observation(&mut observation);
365 self.observations.push(observation);
366 }
367
368 fn finish(self) -> EventDocument {
369 let mut document = EventDocument {
370 source_path: Some(self.source_path.to_owned()),
371 specification: Some("asyncapi".to_owned()),
372 specification_version: Some(self.version.to_owned()),
373 observations: self.observations,
374 warnings: self.warnings,
375 incomplete: self.incomplete,
376 };
377 finish_document(&mut document);
378 document
379 }
380}
381
382#[derive(Debug, Clone, Default)]
383struct ServerFacts {
384 broker: Option<EventBroker>,
385 protocol: Option<String>,
386 namespace: Option<String>,
387 ambiguous: bool,
388}
389
390fn extract_asyncapi_v2(context: &mut AsyncApiContext<'_>) -> Result<(), EventExtractionError> {
391 let Some(channels) = value_get(context.root, "channels") else {
392 context.incomplete = true;
393 context
394 .warnings
395 .push("AsyncAPI document has no channels object".to_owned());
396 return Ok(());
397 };
398 let channel_map =
399 channels
400 .as_object()
401 .ok_or_else(|| EventExtractionError::InvalidStructure {
402 source_path: context.source_path.to_owned(),
403 message: "`channels` must be an object".to_owned(),
404 })?;
405 for (channel_key, channel_item) in channel_map {
406 let raw_channel = channel_key.as_str();
407 let resolved_item = resolve_local(context.root, channel_item);
408 let item = resolved_item.value;
409 let mut emitted = false;
410 for (operation_name, role) in [
411 ("publish", EventRole::Publisher),
412 ("subscribe", EventRole::Subscriber),
413 ] {
414 let Some(operation) = value_get(item, operation_name) else {
415 continue;
416 };
417 emitted = true;
418 append_asyncapi_operation(
419 context,
420 raw_channel,
421 role,
422 operation,
423 Some(item),
424 &resolved_item.warnings,
425 );
426 }
427 if !emitted {
428 let mut observation = asyncapi_observation(
429 context,
430 raw_channel,
431 EventRole::Declaration,
432 None,
433 Some(item),
434 None,
435 );
436 observation.warnings.extend(resolved_item.warnings);
437 context.push(observation);
438 }
439 }
440 Ok(())
441}
442
443fn extract_asyncapi_v3(context: &mut AsyncApiContext<'_>) -> Result<(), EventExtractionError> {
444 let channel_map = value_get(context.root, "channels")
445 .and_then(Value::as_object)
446 .ok_or_else(|| EventExtractionError::InvalidStructure {
447 source_path: context.source_path.to_owned(),
448 message: "`channels` must be an object".to_owned(),
449 })?;
450 let mut used_channels = BTreeSet::new();
451 if let Some(operations) = value_get(context.root, "operations") {
452 let operation_map =
453 operations
454 .as_object()
455 .ok_or_else(|| EventExtractionError::InvalidStructure {
456 source_path: context.source_path.to_owned(),
457 message: "`operations` must be an object".to_owned(),
458 })?;
459 for (operation_name, operation) in operation_map {
460 let operation_label = operation_name.as_str();
461 let resolved_operation = resolve_local(context.root, operation);
462 let Some(action) =
463 value_get(resolved_operation.value, "action").and_then(Value::as_str)
464 else {
465 context.incomplete = true;
466 context.warnings.push(format!(
467 "AsyncAPI operation `{operation_label}` has no literal action"
468 ));
469 continue;
470 };
471 let role = match action {
472 "send" => EventRole::Publisher,
473 "receive" => EventRole::Subscriber,
474 other => {
475 context.incomplete = true;
476 context.warnings.push(format!(
477 "AsyncAPI operation `{operation_label}` uses unsupported action `{other}`"
478 ));
479 continue;
480 }
481 };
482 let Some(channel_value) = value_get(resolved_operation.value, "channel") else {
483 context.incomplete = true;
484 context.warnings.push(format!(
485 "AsyncAPI operation `{operation_label}` has no channel reference"
486 ));
487 continue;
488 };
489 let channel_key = reference_component_name(channel_value, "channels");
490 let resolved_channel = resolve_local(context.root, channel_value);
491 let channel = value_get(resolved_channel.value, "address").and_then(literal_string);
492 let raw_channel = channel.as_deref().unwrap_or("{dynamic-channel}");
493 if let Some(key) = channel_key {
494 used_channels.insert(key);
495 }
496 let mut reference_warnings = resolved_operation.warnings;
497 reference_warnings.extend(resolved_channel.warnings);
498 append_asyncapi_operation(
499 context,
500 raw_channel,
501 role,
502 resolved_operation.value,
503 Some(resolved_channel.value),
504 &reference_warnings,
505 );
506 }
507 }
508
509 for (channel_key, channel_item) in channel_map {
510 let name = channel_key.as_str();
511 if used_channels.contains(name) {
512 continue;
513 }
514 let resolved = resolve_local(context.root, channel_item);
515 append_asyncapi_declaration(context, resolved);
516 }
517 Ok(())
518}
519
520fn append_asyncapi_declaration(context: &mut AsyncApiContext<'_>, resolved: ResolvedValue<'_>) {
521 let raw_channel = value_get(resolved.value, "address")
522 .and_then(literal_string)
523 .unwrap_or_else(|| "{dynamic-channel}".to_owned());
524 let messages = operation_messages(context.root, resolved.value, Some(resolved.value));
525 if messages.is_empty() {
526 let mut observation = asyncapi_observation(
527 context,
528 &raw_channel,
529 EventRole::Declaration,
530 None,
531 Some(resolved.value),
532 None,
533 );
534 observation.warnings.extend(resolved.warnings);
535 context.push(observation);
536 return;
537 }
538 for message in messages {
539 let mut observation = asyncapi_observation(
540 context,
541 &raw_channel,
542 EventRole::Declaration,
543 None,
544 Some(resolved.value),
545 Some(message.value),
546 );
547 observation
548 .warnings
549 .extend(resolved.warnings.iter().cloned());
550 observation.warnings.extend(message.warnings);
551 context.push(observation);
552 }
553}
554
555fn append_asyncapi_operation(
556 context: &mut AsyncApiContext<'_>,
557 raw_channel: &str,
558 role: EventRole,
559 operation: &Value,
560 channel_item: Option<&Value>,
561 reference_warnings: &[String],
562) {
563 let resolved_operation = resolve_local(context.root, operation);
564 let messages = operation_messages(context.root, resolved_operation.value, channel_item);
565 if messages.is_empty() {
566 let mut observation = asyncapi_observation(
567 context,
568 raw_channel,
569 role,
570 Some(resolved_operation.value),
571 channel_item,
572 None,
573 );
574 observation
575 .warnings
576 .extend(reference_warnings.iter().cloned());
577 observation
578 .warnings
579 .extend(resolved_operation.warnings.iter().cloned());
580 observation.incomplete = true;
581 observation
582 .warnings
583 .push("operation has no statically resolvable message".to_owned());
584 context.push(observation);
585 return;
586 }
587 for message in messages {
588 let mut observation = asyncapi_observation(
589 context,
590 raw_channel,
591 role,
592 Some(resolved_operation.value),
593 channel_item,
594 Some(message.value),
595 );
596 observation
597 .warnings
598 .extend(reference_warnings.iter().cloned());
599 observation
600 .warnings
601 .extend(resolved_operation.warnings.iter().cloned());
602 observation.warnings.extend(message.warnings);
603 context.push(observation);
604 }
605}
606
607struct ResolvedValue<'a> {
608 value: &'a Value,
609 warnings: Vec<String>,
610}
611
612fn operation_messages<'a>(
613 root: &'a Value,
614 operation: &'a Value,
615 channel_item: Option<&'a Value>,
616) -> Vec<ResolvedValue<'a>> {
617 let message_value = value_get(operation, "message").or_else(|| {
618 value_get(operation, "messages")
619 .or_else(|| channel_item.and_then(|channel| value_get(channel, "messages")))
620 });
621 let Some(message_value) = message_value else {
622 return Vec::new();
623 };
624 let mut output = Vec::new();
625 collect_messages(root, message_value, &mut output);
626 output
627}
628
629fn collect_messages<'a>(root: &'a Value, value: &'a Value, output: &mut Vec<ResolvedValue<'a>>) {
630 if let Some(items) = value_get(value, "oneOf").and_then(Value::as_array) {
631 for item in items {
632 output.push(resolve_local(root, item));
633 }
634 return;
635 }
636 if let Some(sequence) = value.as_array() {
637 for item in sequence {
638 output.push(resolve_local(root, item));
639 }
640 return;
641 }
642 if let Some(mapping) = value.as_object()
643 && !mapping.contains_key("$ref")
644 && !mapping.contains_key("payload")
645 && !mapping.contains_key("name")
646 {
647 for item in mapping.values() {
648 output.push(resolve_local(root, item));
649 }
650 return;
651 }
652 output.push(resolve_local(root, value));
653}
654
655fn asyncapi_observation(
656 context: &AsyncApiContext<'_>,
657 raw_channel: &str,
658 role: EventRole,
659 operation: Option<&Value>,
660 channel_item: Option<&Value>,
661 message: Option<&Value>,
662) -> EventObservation {
663 let mut warnings = Vec::new();
664 if context.server.ambiguous {
665 warnings.push("server selection or namespace is ambiguous".to_owned());
666 }
667 let channel = literal_channel(raw_channel);
668 if channel.is_none() {
669 warnings.push("templated or empty channel is not an exact channel".to_owned());
670 }
671 let protocol = context.server.protocol.clone();
672 let broker = context
673 .server
674 .broker
675 .or_else(|| {
676 protocol
677 .as_deref()
678 .map(broker_from_protocol)
679 .filter(|broker| *broker != EventBroker::Generic)
680 })
681 .or_else(|| channel_item.and_then(broker_from_bindings))
682 .or_else(|| operation.and_then(broker_from_bindings))
683 .or_else(|| message.and_then(broker_from_bindings))
684 .unwrap_or(EventBroker::Generic);
685 let event_type = message.and_then(message_name);
686 let schema = message.and_then(|value| schema_definition(context.root, value, &mut warnings));
687 let partition_key = exact_metadata(
688 [message, operation, channel_item],
689 &["partitionKey", "partition_key", "x-partition-key"],
690 );
691 let routing_key = exact_metadata(
692 [operation, channel_item, message],
693 &["routingKey", "routing_key", "x-routing-key"],
694 );
695 let delivery_semantics = [operation, channel_item, message]
696 .into_iter()
697 .flatten()
698 .find_map(explicit_delivery_semantics);
699 let dead_letter_channel = exact_metadata(
700 [operation, channel_item, message],
701 &[
702 "deadLetterChannel",
703 "deadLetterQueue",
704 "deadLetterTopic",
705 "dead_letter_channel",
706 "x-dead-letter-channel",
707 ],
708 );
709 let evidence = evidence_for_token(
710 context.input,
711 channel.as_deref().unwrap_or(raw_channel),
712 &format!("AsyncAPI {role:?} channel"),
713 );
714 EventObservation {
715 broker,
716 role,
717 channel,
718 namespace: context.server.namespace.clone(),
719 protocol,
720 event_type,
721 schema,
722 partition_key,
723 routing_key,
724 delivery_semantics,
725 dead_letter_channel,
726 language: None,
727 evidence: vec![evidence],
728 confidence: if warnings.is_empty() { 1.0 } else { 0.0 },
729 incomplete: !warnings.is_empty(),
730 warnings,
731 }
732}
733
734fn schema_definition(
735 root: &Value,
736 message: &Value,
737 warnings: &mut Vec<String>,
738) -> Option<EventSchemaDefinition> {
739 let payload = value_get(message, "payload")?;
740 let resolved = resolve_local(root, payload);
741 warnings.extend(resolved.warnings);
742 let schema = resolved.value;
743 let name = message_name(message).or_else(|| reference_name(payload));
744 let version = exact_string_recursive(
745 message,
746 &["schemaVersion", "schema_version", "x-schema-version"],
747 3,
748 )
749 .or_else(|| {
750 exact_string_recursive(
751 schema,
752 &["schemaVersion", "schema_version", "x-schema-version"],
753 2,
754 )
755 });
756 let schema_format = value_get(message, "schemaFormat")
757 .and_then(literal_string)
758 .or_else(|| value_get(message, "contentType").and_then(literal_string))
759 .or_else(|| value_get(schema, "$schema").and_then(literal_string));
760 let required = value_get(schema, "required")
761 .and_then(Value::as_array)
762 .map(|items| {
763 items
764 .iter()
765 .filter_map(Value::as_str)
766 .map(str::to_owned)
767 .collect::<BTreeSet<_>>()
768 })
769 .unwrap_or_default();
770 let mut fields = Vec::new();
771 if let Some(properties) = value_get(schema, "properties").and_then(Value::as_object) {
772 for (field_name, field_schema) in properties {
773 if fields.len() >= MAX_SCHEMA_FIELDS {
774 warnings.push(format!(
775 "payload fields truncated at {MAX_SCHEMA_FIELDS} items"
776 ));
777 break;
778 }
779 let field_name = field_name.as_str();
780 let resolved_field = resolve_local(root, field_schema);
781 warnings.extend(resolved_field.warnings);
782 fields.push(EventSchemaField {
783 name: field_name.to_owned(),
784 field_type: schema_type(resolved_field.value)
785 .or_else(|| reference_name(field_schema)),
786 required: required.contains(field_name),
787 });
788 }
789 }
790 fields.sort();
791 fields.dedup();
792 Some(EventSchemaDefinition {
793 name,
794 version,
795 schema_format,
796 fields,
797 })
798}
799
800fn schema_type(value: &Value) -> Option<String> {
801 value_get(value, "type")
802 .and_then(Value::as_str)
803 .map(str::to_owned)
804 .or_else(|| {
805 value_get(value, "format")
806 .and_then(Value::as_str)
807 .map(str::to_owned)
808 })
809}
810
811fn message_name(message: &Value) -> Option<String> {
812 value_get(message, "name")
813 .and_then(literal_string)
814 .or_else(|| value_get(message, "title").and_then(literal_string))
815 .or_else(|| reference_name(message))
816}
817
818fn global_server_facts(root: &Value) -> (ServerFacts, Vec<String>) {
819 let Some(servers) = value_get(root, "servers").and_then(Value::as_object) else {
820 return (ServerFacts::default(), Vec::new());
821 };
822 let mut protocols = BTreeSet::new();
823 let mut namespaces = BTreeSet::new();
824 let mut warnings = Vec::new();
825 for server in servers.values() {
826 let resolved = resolve_local(root, server);
827 warnings.extend(resolved.warnings);
828 if let Some(protocol) = value_get(resolved.value, "protocol").and_then(literal_string) {
829 protocols.insert(protocol.to_ascii_lowercase());
830 }
831 let namespace = server_namespace(resolved.value);
832 if let Some(namespace) = namespace {
833 namespaces.insert(namespace);
834 } else if value_get(resolved.value, "url").is_some()
835 || value_get(resolved.value, "host").is_some()
836 {
837 warnings
838 .push("templated server namespace was not promoted to an exact value".to_owned());
839 }
840 }
841 let protocol = unique_value(&protocols);
842 let namespace = unique_value(&namespaces);
843 if protocols.len() > 1 {
844 warnings.push("multiple server protocols make broker selection ambiguous".to_owned());
845 }
846 if namespaces.len() > 1 {
847 warnings.push("multiple server namespaces make namespace selection ambiguous".to_owned());
848 }
849 let broker = protocol
850 .as_deref()
851 .map(broker_from_protocol)
852 .filter(|broker| *broker != EventBroker::Generic);
853 let ambiguous = !warnings.is_empty();
854 (
855 ServerFacts {
856 broker,
857 protocol,
858 namespace,
859 ambiguous,
860 },
861 warnings,
862 )
863}
864
865fn server_namespace(server: &Value) -> Option<String> {
866 if let Some(namespace) = value_get(server, "namespace").and_then(literal_string) {
867 return literal_namespace(&namespace);
868 }
869 if let Some(url) = value_get(server, "url").and_then(literal_string) {
870 return literal_namespace(&url);
871 }
872 let host = value_get(server, "host").and_then(literal_string)?;
873 let pathname = value_get(server, "pathname")
874 .and_then(literal_string)
875 .unwrap_or_default();
876 literal_namespace(&format!("{host}{pathname}"))
877}
878
879fn unique_value(values: &BTreeSet<String>) -> Option<String> {
880 (values.len() == 1)
881 .then(|| values.iter().next().cloned())
882 .flatten()
883}
884
885fn literal_namespace(value: &str) -> Option<String> {
886 let trimmed = value.trim();
887 if trimmed.is_empty()
888 || trimmed.contains(['{', '}'])
889 || trimmed.contains("${")
890 || trimmed.chars().any(char::is_whitespace)
891 {
892 return None;
893 }
894 let without_scheme = trimmed
895 .split_once("://")
896 .map_or(trimmed, |(_, remainder)| remainder);
897 let without_credentials = without_scheme
898 .rsplit_once('@')
899 .map_or(without_scheme, |(_, remainder)| remainder);
900 let namespace = without_credentials
901 .split(['?', '#'])
902 .next()
903 .unwrap_or_default()
904 .trim_end_matches('/');
905 (!namespace.is_empty()).then(|| namespace.to_owned())
906}
907
908fn broker_from_protocol(protocol: &str) -> EventBroker {
909 match protocol.to_ascii_lowercase().as_str() {
910 "kafka" | "kafka-secure" => EventBroker::Kafka,
911 "amqp" | "amqps" => EventBroker::RabbitMq,
912 "sns" | "aws-sns" => EventBroker::AwsSns,
913 "sqs" | "aws-sqs" => EventBroker::AwsSqs,
914 "nats" | "nats-secure" => EventBroker::Nats,
915 "googlepubsub" | "google-pubsub" | "gcp-pubsub" | "pubsub" => EventBroker::GooglePubSub,
916 _ => EventBroker::Generic,
917 }
918}
919
920fn broker_from_bindings(value: &Value) -> Option<EventBroker> {
921 let bindings = value_get(value, "bindings")?.as_object()?;
922 for key in bindings.keys() {
923 let broker = match key.to_ascii_lowercase().as_str() {
924 "kafka" => Some(EventBroker::Kafka),
925 "amqp" => Some(EventBroker::RabbitMq),
926 "sns" => Some(EventBroker::AwsSns),
927 "sqs" => Some(EventBroker::AwsSqs),
928 "nats" => Some(EventBroker::Nats),
929 "googlepubsub" | "google-pubsub" | "pubsub" => Some(EventBroker::GooglePubSub),
930 _ => None,
931 };
932 if broker.is_some() {
933 return broker;
934 }
935 }
936 None
937}
938
939fn explicit_delivery_semantics(value: &Value) -> Option<DeliverySemantics> {
940 let raw = exact_string_recursive(
941 value,
942 &[
943 "deliverySemantics",
944 "delivery_semantics",
945 "deliveryGuarantee",
946 "x-delivery-semantics",
947 ],
948 4,
949 )?;
950 parse_delivery_semantics(&raw)
951}
952
953fn parse_delivery_semantics(value: &str) -> Option<DeliverySemantics> {
954 let canonical = value
955 .chars()
956 .filter(char::is_ascii_alphanumeric)
957 .flat_map(char::to_lowercase)
958 .collect::<String>();
959 match canonical.as_str() {
960 "atmostonce" => Some(DeliverySemantics::AtMostOnce),
961 "atleastonce" => Some(DeliverySemantics::AtLeastOnce),
962 "exactlyonce" => Some(DeliverySemantics::ExactlyOnce),
963 "besteffort" => Some(DeliverySemantics::BestEffort),
964 _ => None,
965 }
966}
967
968fn exact_metadata<const N: usize>(values: [Option<&Value>; N], keys: &[&str]) -> Option<String> {
969 values
970 .into_iter()
971 .flatten()
972 .find_map(|value| exact_string_recursive(value, keys, 4))
973}
974
975fn exact_string_recursive(value: &Value, keys: &[&str], depth: usize) -> Option<String> {
976 if depth == 0 {
977 return None;
978 }
979 let mapping = value.as_object()?;
980 for key in keys {
981 if let Some(found) = mapping.get(*key)
982 && let Some(literal) = literal_string(found)
983 {
984 return Some(literal);
985 }
986 }
987 mapping
988 .values()
989 .find_map(|child| exact_string_recursive(child, keys, depth - 1))
990}
991
992fn resolve_local<'a>(root: &'a Value, value: &'a Value) -> ResolvedValue<'a> {
993 let mut current = value;
994 let mut warnings = Vec::new();
995 let mut visited = BTreeSet::new();
996 for _ in 0..MAX_REFERENCE_DEPTH {
997 let Some(reference) = value_get(current, "$ref").and_then(Value::as_str) else {
998 return ResolvedValue {
999 value: current,
1000 warnings,
1001 };
1002 };
1003 if !reference.starts_with("#/") {
1004 warnings.push(format!(
1005 "external reference `{}` was not resolved",
1006 bounded_text(reference, MAX_EVIDENCE_TEXT_CHARS)
1007 ));
1008 return ResolvedValue {
1009 value: current,
1010 warnings,
1011 };
1012 }
1013 if !visited.insert(reference.to_owned()) {
1014 warnings.push(format!(
1015 "cyclic local reference `{}` was not resolved",
1016 bounded_text(reference, MAX_EVIDENCE_TEXT_CHARS)
1017 ));
1018 return ResolvedValue {
1019 value: current,
1020 warnings,
1021 };
1022 }
1023 let Some(next) = json_pointer(root, reference) else {
1024 warnings.push(format!(
1025 "unresolved local reference `{}`",
1026 bounded_text(reference, MAX_EVIDENCE_TEXT_CHARS)
1027 ));
1028 return ResolvedValue {
1029 value: current,
1030 warnings,
1031 };
1032 };
1033 current = next;
1034 }
1035 warnings.push(format!(
1036 "local reference depth exceeded {MAX_REFERENCE_DEPTH}"
1037 ));
1038 ResolvedValue {
1039 value: current,
1040 warnings,
1041 }
1042}
1043
1044fn json_pointer<'a>(root: &'a Value, reference: &str) -> Option<&'a Value> {
1045 let mut current = root;
1046 for component in reference.strip_prefix("#/")?.split('/') {
1047 let decoded = component.replace("~1", "/").replace("~0", "~");
1048 current = current.as_object()?.get(&decoded)?;
1049 }
1050 Some(current)
1051}
1052
1053fn reference_component_name(value: &Value, component: &str) -> Option<String> {
1054 let reference = value_get(value, "$ref")?.as_str()?;
1055 let prefix = format!("#/{component}/");
1056 reference
1057 .strip_prefix(&prefix)
1058 .map(|name| name.replace("~1", "/").replace("~0", "~"))
1059}
1060
1061fn reference_name(value: &Value) -> Option<String> {
1062 value_get(value, "$ref")
1063 .and_then(Value::as_str)
1064 .and_then(|reference| reference.rsplit('/').next())
1065 .filter(|name| !name.is_empty())
1066 .map(|name| name.replace("~1", "/").replace("~0", "~"))
1067}
1068
1069fn value_get<'a>(value: &'a Value, key: &str) -> Option<&'a Value> {
1070 value.as_object()?.get(key)
1071}
1072
1073fn mapping_string<'a>(mapping: &'a Mapping, key: &str) -> Option<&'a str> {
1074 mapping.get(key).and_then(Value::as_str)
1075}
1076
1077fn literal_string(value: &Value) -> Option<String> {
1078 value
1079 .as_str()
1080 .map(str::trim)
1081 .filter(|value| !value.is_empty())
1082 .map(str::to_owned)
1083}
1084
1085fn literal_channel(value: &str) -> Option<String> {
1086 let trimmed = value.trim();
1087 (!trimmed.is_empty() && !trimmed.contains(['{', '}']) && !trimmed.contains("${"))
1088 .then(|| trimmed.to_owned())
1089}
1090
1091fn evidence_for_token(input: &str, token: &str, label: &str) -> EventEvidenceLine {
1092 let line = input
1093 .lines()
1094 .position(|source| source.contains(token))
1095 .and_then(|index| u32::try_from(index + 1).ok())
1096 .unwrap_or(1);
1097 EventEvidenceLine {
1098 line,
1099 text: bounded_text(label, MAX_EVIDENCE_TEXT_CHARS),
1100 }
1101}
1102
1103#[derive(Debug, Clone, Copy)]
1104struct SourceRecognition {
1105 broker: EventBroker,
1106 role: EventRole,
1107 call: &'static str,
1108}
1109
1110fn recognize_source_call(statement: &str) -> Option<SourceRecognition> {
1111 let masked = mask_string_contents(statement);
1112 if looks_like_function_declaration(&masked) {
1113 return None;
1114 }
1115 let compact = compact_lowercase(&masked);
1116 if let Some(role) = recognize_sns(&compact) {
1117 return Some(SourceRecognition {
1118 broker: EventBroker::AwsSns,
1119 role,
1120 call: "AWS SNS",
1121 });
1122 }
1123 if let Some(role) = recognize_sqs(&compact) {
1124 return Some(SourceRecognition {
1125 broker: EventBroker::AwsSqs,
1126 role,
1127 call: "AWS SQS",
1128 });
1129 }
1130 if let Some(role) = recognize_google_pubsub(&compact) {
1131 return Some(SourceRecognition {
1132 broker: EventBroker::GooglePubSub,
1133 role,
1134 call: "Google Pub/Sub",
1135 });
1136 }
1137 if let Some(role) = recognize_rabbitmq(&compact) {
1138 return Some(SourceRecognition {
1139 broker: EventBroker::RabbitMq,
1140 role,
1141 call: "RabbitMQ",
1142 });
1143 }
1144 if let Some(role) = recognize_kafka(&compact) {
1145 return Some(SourceRecognition {
1146 broker: EventBroker::Kafka,
1147 role,
1148 call: "Kafka",
1149 });
1150 }
1151 if let Some(role) = recognize_nats(&compact) {
1152 return Some(SourceRecognition {
1153 broker: EventBroker::Nats,
1154 role,
1155 call: "NATS",
1156 });
1157 }
1158 recognize_generic(&compact).map(|role| SourceRecognition {
1159 broker: EventBroker::Generic,
1160 role,
1161 call: "generic pub/sub",
1162 })
1163}
1164
1165fn recognize_sns(source: &str) -> Option<EventRole> {
1166 let identified = source.contains("topicarn")
1167 || source.contains("topic_arn")
1168 || source.contains("sns.")
1169 || source.contains("snsclient");
1170 if !identified {
1171 return None;
1172 }
1173 if contains_call(source, &["createtopic", "create_topic"]) {
1174 Some(EventRole::Declaration)
1175 } else if contains_call(source, &["publish", "send"]) {
1176 Some(EventRole::Publisher)
1177 } else if contains_call(source, &["subscribe"]) {
1178 Some(EventRole::Subscriber)
1179 } else {
1180 None
1181 }
1182}
1183
1184fn recognize_sqs(source: &str) -> Option<EventRole> {
1185 let identified = source.contains("queueurl")
1186 || source.contains("queue_url")
1187 || source.contains("sqs.")
1188 || source.contains("sqsclient")
1189 || source.contains("sendmessage")
1190 || source.contains("send_message")
1191 || source.contains("receivemessage")
1192 || source.contains("receive_message");
1193 if !identified {
1194 return None;
1195 }
1196 if contains_call(source, &["createqueue", "create_queue"]) {
1197 Some(EventRole::Declaration)
1198 } else if contains_call(source, &["sendmessage", "send_message"]) {
1199 Some(EventRole::Publisher)
1200 } else if contains_call(
1201 source,
1202 &[
1203 "receivemessage",
1204 "receive_message",
1205 "startmessagepoller",
1206 "start_message_poller",
1207 "sqslistener",
1208 ],
1209 ) {
1210 Some(EventRole::Subscriber)
1211 } else {
1212 None
1213 }
1214}
1215
1216fn recognize_google_pubsub(source: &str) -> Option<EventRole> {
1217 let identified = source.contains("pubsub")
1218 || source.contains("publisherclient")
1219 || source.contains("subscriberclient")
1220 || source.contains("topicpath")
1221 || source.contains("topic_path")
1222 || source.contains("subscriptionpath")
1223 || source.contains("subscription_path");
1224 if !identified {
1225 return None;
1226 }
1227 if contains_call(
1228 source,
1229 &[
1230 "createtopic",
1231 "create_topic",
1232 "createsubscription",
1233 "create_subscription",
1234 ],
1235 ) {
1236 Some(EventRole::Declaration)
1237 } else if contains_call(source, &["publish", "publishmessage", "publish_message"]) {
1238 Some(EventRole::Publisher)
1239 } else if contains_call(
1240 source,
1241 &[
1242 "subscribe",
1243 "pull",
1244 "streamingpull",
1245 "streaming_pull",
1246 "receive",
1247 "pubsubsubscription",
1248 ],
1249 ) {
1250 Some(EventRole::Subscriber)
1251 } else {
1252 None
1253 }
1254}
1255
1256fn recognize_rabbitmq(source: &str) -> Option<EventRole> {
1257 let identified = source.contains("rabbit")
1258 || source.contains("amqp")
1259 || source.contains("basicpublish")
1260 || source.contains("basic_publish")
1261 || source.contains("basicconsume")
1262 || source.contains("basic_consume")
1263 || source.contains("queuedeclare")
1264 || source.contains("queue_declare")
1265 || source.contains("exchangedeclare")
1266 || source.contains("exchange_declare");
1267 if !identified {
1268 return None;
1269 }
1270 if contains_call(
1271 source,
1272 &[
1273 "queuedeclare",
1274 "queue_declare",
1275 "exchangedeclare",
1276 "exchange_declare",
1277 "assertqueue",
1278 "assertexchange",
1279 ],
1280 ) {
1281 Some(EventRole::Declaration)
1282 } else if contains_call(
1283 source,
1284 &["basicpublish", "basic_publish", "publish", "send"],
1285 ) {
1286 Some(EventRole::Publisher)
1287 } else if contains_call(
1288 source,
1289 &[
1290 "basicconsume",
1291 "basic_consume",
1292 "consume",
1293 "get",
1294 "rabbitlistener",
1295 ],
1296 ) {
1297 Some(EventRole::Subscriber)
1298 } else {
1299 None
1300 }
1301}
1302
1303fn recognize_kafka(source: &str) -> Option<EventRole> {
1304 let identified = source.contains("kafka")
1305 || source.contains("kafkatemplate")
1306 || source.contains("kafkaproducer")
1307 || source.contains("kafkaconsumer")
1308 || source.contains("baserecord::to")
1309 || source.contains("futurerecord::to")
1310 || source.contains("newtopic")
1311 || (source.contains("producer.") && source.contains("topic"))
1312 || (source.contains("consumer.") && source.contains("subscribe"));
1313 if !identified {
1314 return None;
1315 }
1316 if contains_call(source, &["newtopic", "createtopics", "create_topics"]) {
1317 Some(EventRole::Declaration)
1318 } else if contains_call(
1319 source,
1320 &[
1321 "send",
1322 "publish",
1323 "produce",
1324 "writemessages",
1325 "write_messages",
1326 ],
1327 ) {
1328 Some(EventRole::Publisher)
1329 } else if contains_call(
1330 source,
1331 &[
1332 "subscribe",
1333 "subscribetopics",
1334 "subscribe_topics",
1335 "assign",
1336 "poll",
1337 "kafkalistener",
1338 ],
1339 ) {
1340 Some(EventRole::Subscriber)
1341 } else {
1342 None
1343 }
1344}
1345
1346fn recognize_nats(source: &str) -> Option<EventRole> {
1347 let identified = source.contains("nats")
1348 || source.contains("jetstream")
1349 || source.starts_with("nc.")
1350 || source.contains(" nc.")
1351 || source.starts_with("js.")
1352 || source.contains(" js.");
1353 if !identified {
1354 return None;
1355 }
1356 if contains_call(
1357 source,
1358 &["addstream", "add_stream", "createstream", "create_stream"],
1359 ) {
1360 Some(EventRole::Declaration)
1361 } else if contains_call(source, &["publish", "request"]) {
1362 Some(EventRole::Publisher)
1363 } else if contains_call(source, &["subscribe", "queuesubscribe", "queue_subscribe"]) {
1364 Some(EventRole::Subscriber)
1365 } else {
1366 None
1367 }
1368}
1369
1370fn recognize_generic(source: &str) -> Option<EventRole> {
1371 if contains_call(
1372 source,
1373 &[
1374 "declarechannel",
1375 "declare_channel",
1376 "createchannel",
1377 "create_channel",
1378 ],
1379 ) {
1380 Some(EventRole::Declaration)
1381 } else if contains_call(source, &["publish", "publisher.publish"]) {
1382 Some(EventRole::Publisher)
1383 } else if contains_call(source, &["subscribe", "subscriber.subscribe"]) {
1384 Some(EventRole::Subscriber)
1385 } else {
1386 None
1387 }
1388}
1389
1390fn contains_call(source: &str, names: &[&str]) -> bool {
1391 names.iter().any(|name| {
1392 let needle = format!("{name}(");
1393 let mut offset = 0;
1394 while let Some(relative) = source[offset..].find(&needle) {
1395 let position = offset + relative;
1396 if !source[..position]
1397 .chars()
1398 .next_back()
1399 .is_some_and(is_identifier_character)
1400 {
1401 return true;
1402 }
1403 offset = position.saturating_add(needle.len());
1404 if offset >= source.len() {
1405 break;
1406 }
1407 }
1408 false
1409 })
1410}
1411
1412fn looks_like_function_declaration(source: &str) -> bool {
1413 let trimmed = source.trim_start().to_ascii_lowercase();
1414 ["fn ", "def ", "func ", "function "]
1415 .iter()
1416 .any(|prefix| trimmed.starts_with(prefix))
1417 || ["public ", "private ", "protected ", "static "]
1418 .iter()
1419 .any(|prefix| {
1420 trimmed.starts_with(prefix)
1421 && trimmed
1422 .split_once('(')
1423 .is_some_and(|(head, _)| !head.contains(['.', '=']))
1424 })
1425}
1426
1427fn append_source_observations(
1428 output: &mut Vec<EventObservation>,
1429 language: SourceLanguage,
1430 line: u32,
1431 statement: &str,
1432 recognition: SourceRecognition,
1433) {
1434 let channels = source_channels(statement, recognition);
1435 let partition_key = literal_after_named(statement, &["partitionKey", "partition_key", "key"]);
1436 let routing_key = literal_after_named(statement, &["routingKey", "routing_key"]);
1437 let delivery_semantics = literal_after_named(
1438 statement,
1439 &[
1440 "deliverySemantics",
1441 "delivery_semantics",
1442 "deliveryGuarantee",
1443 ],
1444 )
1445 .and_then(|value| parse_delivery_semantics(&value));
1446 let dead_letter_channel = literal_after_named(
1447 statement,
1448 &[
1449 "deadLetterChannel",
1450 "dead_letter_channel",
1451 "deadLetterQueue",
1452 "dead_letter_queue",
1453 "deadLetterTopic",
1454 "dead_letter_topic",
1455 "dlq",
1456 ],
1457 );
1458 let event_type = literal_after_named(statement, &["eventType", "event_type", "messageType"]);
1459 let channels = if channels.is_empty() {
1460 vec![None]
1461 } else {
1462 channels.into_iter().map(Some).collect()
1463 };
1464 for channel in channels {
1465 let mut warnings = Vec::new();
1466 if channel.is_none() {
1467 warnings
1468 .push("dynamic channel expression was not promoted to an exact value".to_owned());
1469 }
1470 let exact = channel.is_some();
1471 output.push(EventObservation {
1472 broker: recognition.broker,
1473 role: recognition.role,
1474 channel,
1475 namespace: None,
1476 protocol: None,
1477 event_type: event_type.clone(),
1478 schema: None,
1479 partition_key: partition_key.clone(),
1480 routing_key: routing_key.clone(),
1481 delivery_semantics,
1482 dead_letter_channel: dead_letter_channel.clone(),
1483 language: Some(language),
1484 evidence: vec![EventEvidenceLine {
1485 line,
1486 text: bounded_text(
1487 &format!("{} {:?} literal call", recognition.call, recognition.role),
1488 MAX_EVIDENCE_TEXT_CHARS,
1489 ),
1490 }],
1491 confidence: if exact { 1.0 } else { 0.0 },
1492 incomplete: !exact,
1493 warnings,
1494 });
1495 }
1496}
1497
1498fn source_channels(statement: &str, recognition: SourceRecognition) -> Vec<String> {
1499 let named_keys: &[&str] = match recognition.broker {
1500 EventBroker::Kafka => &["topic", "topics", "Topic"],
1501 EventBroker::RabbitMq => {
1502 if recognition.role == EventRole::Publisher {
1503 &["exchange", "exchangeName", "exchange_name"]
1504 } else {
1505 &["queue", "queueName", "queue_name"]
1506 }
1507 }
1508 EventBroker::AwsSns => &["TopicArn", "topicArn", "topic_arn"],
1509 EventBroker::AwsSqs => &["QueueUrl", "queueUrl", "queue_url"],
1510 EventBroker::Nats => &["subject", "subjects"],
1511 EventBroker::GooglePubSub => {
1512 if recognition.role == EventRole::Subscriber {
1513 &["subscription", "subscriptionName", "subscription_name"]
1514 } else {
1515 &["topic", "topicName", "topic_name"]
1516 }
1517 }
1518 EventBroker::Generic => &["channel", "topic", "queue", "subject"],
1519 };
1520 let mut channels = literals_after_named(statement, named_keys);
1521 if channels.is_empty() {
1522 channels = call_argument_literals(statement, recognition, 0);
1523 }
1524 if recognition.broker == EventBroker::RabbitMq
1525 && recognition.role == EventRole::Publisher
1526 && channels.first().is_some_and(String::is_empty)
1527 {
1528 channels = call_argument_literals(statement, recognition, 1);
1529 }
1530 channels.retain(|channel| !channel.is_empty());
1531 channels.sort();
1532 channels.dedup();
1533 channels
1534}
1535
1536fn call_argument_literals(
1537 statement: &str,
1538 recognition: SourceRecognition,
1539 argument_index: usize,
1540) -> Vec<String> {
1541 let markers: &[&str] = match (recognition.broker, recognition.role) {
1542 (EventBroker::Kafka, EventRole::Publisher) => &[
1543 "BaseRecord::to",
1544 "FutureRecord::to",
1545 "kafkaTemplate.send",
1546 ".send",
1547 ".produce",
1548 "WriteMessages",
1549 ],
1550 (EventBroker::Kafka, EventRole::Subscriber) => &[
1551 ".subscribe",
1552 "SubscribeTopics",
1553 "subscribe_topics",
1554 "KafkaListener",
1555 ],
1556 (EventBroker::RabbitMq, EventRole::Publisher) => {
1557 &["basic_publish", "basicPublish", ".publish", ".Publish"]
1558 }
1559 (EventBroker::RabbitMq, EventRole::Subscriber) => &[
1560 "basic_consume",
1561 "basicConsume",
1562 ".consume",
1563 ".Consume",
1564 "RabbitListener",
1565 ],
1566 (EventBroker::RabbitMq, EventRole::Declaration) => {
1567 &["queue_declare", "queueDeclare", "assertQueue"]
1568 }
1569 (EventBroker::AwsSns | EventBroker::GooglePubSub, EventRole::Publisher) => {
1570 &[".publish", "publish"]
1571 }
1572 (EventBroker::AwsSns, EventRole::Subscriber) => &[".subscribe", "subscribe"],
1573 (EventBroker::AwsSns, EventRole::Declaration) => &["create_topic", "createTopic"],
1574 (EventBroker::AwsSqs, EventRole::Publisher) => &["send_message", "sendMessage"],
1575 (EventBroker::AwsSqs, EventRole::Subscriber) => {
1576 &["receive_message", "receiveMessage", "SqsListener"]
1577 }
1578 (EventBroker::AwsSqs, EventRole::Declaration) => &["create_queue", "createQueue"],
1579 (EventBroker::Nats | EventBroker::Generic, EventRole::Publisher) => {
1580 &[".publish", "publish", ".Publish", "Publish"]
1581 }
1582 (EventBroker::Nats | EventBroker::Generic, EventRole::Subscriber) => {
1583 &[".subscribe", "subscribe", ".Subscribe", "Subscribe"]
1584 }
1585 (EventBroker::Nats, EventRole::Declaration) => &["add_stream", "addStream"],
1586 (EventBroker::GooglePubSub, EventRole::Subscriber) => &[
1587 ".subscribe",
1588 "subscribe",
1589 ".pull",
1590 "pull",
1591 "PubSubSubscription",
1592 ],
1593 (EventBroker::GooglePubSub, EventRole::Declaration) => {
1594 &["create_topic", "createTopic", "create_subscription"]
1595 }
1596 (EventBroker::Generic, EventRole::Declaration) => {
1597 &["declare_channel", "declareChannel", "create_channel"]
1598 }
1599 _ => &[],
1600 };
1601 markers
1602 .iter()
1603 .find_map(|marker| {
1604 argument_expression(statement, marker, argument_index)
1605 .map(static_literals_from_expression)
1606 })
1607 .unwrap_or_default()
1608}
1609
1610fn argument_expression<'a>(source: &'a str, marker: &str, target: usize) -> Option<&'a str> {
1611 let marker_start = source.find(marker)?;
1612 let after_marker = &source[marker_start + marker.len()..];
1613 let open_offset = after_marker.find('(')?;
1614 let arguments = &after_marker[open_offset + 1..];
1615 let mut quote = None;
1616 let mut escaped = false;
1617 let mut depth = 0_u32;
1618 let mut start = 0;
1619 let mut index = 0;
1620 for (offset, character) in arguments.char_indices() {
1621 if let Some(delimiter) = quote {
1622 if escaped {
1623 escaped = false;
1624 } else if character == '\\' {
1625 escaped = true;
1626 } else if character == delimiter {
1627 quote = None;
1628 }
1629 continue;
1630 }
1631 match character {
1632 '"' | '\'' | '`' => quote = Some(character),
1633 '(' | '[' | '{' => depth = depth.saturating_add(1),
1634 ')' if depth == 0 => {
1635 return (index == target).then(|| &arguments[start..offset]);
1636 }
1637 ')' | ']' | '}' => depth = depth.saturating_sub(1),
1638 ',' if depth == 0 => {
1639 if index == target {
1640 return Some(&arguments[start..offset]);
1641 }
1642 index += 1;
1643 start = offset + character.len_utf8();
1644 }
1645 _ => {}
1646 }
1647 }
1648 (index == target).then(|| &arguments[start..])
1649}
1650
1651fn literals_after_named(source: &str, keys: &[&str]) -> Vec<String> {
1652 let mut output = Vec::new();
1653 for key in keys {
1654 let mut offset = 0;
1655 while let Some(relative) = source[offset..].find(key) {
1656 let position = offset + relative;
1657 if is_identifier_boundary(source, position, key.len()) {
1658 let after = &source[position + key.len()..];
1659 let expression = after
1660 .trim_start()
1661 .strip_prefix([':', '='])
1662 .or_else(|| after.trim_start().strip_prefix('('));
1663 if let Some(expression) = expression {
1664 output.extend(static_literals_from_expression(expression));
1665 }
1666 }
1667 offset = position.saturating_add(key.len());
1668 if offset >= source.len() {
1669 break;
1670 }
1671 }
1672 }
1673 output.sort();
1674 output.dedup();
1675 output
1676}
1677
1678fn literal_after_named(source: &str, keys: &[&str]) -> Option<String> {
1679 literals_after_named(source, keys).into_iter().next()
1680}
1681
1682fn static_literals_from_expression(expression: &str) -> Vec<String> {
1683 let trimmed = expression.trim_start_matches(|character: char| {
1684 character.is_whitespace() || matches!(character, '&' | '*')
1685 });
1686 if trimmed.starts_with("format!")
1687 || trimmed.starts_with("format(")
1688 || trimmed.starts_with("f\"")
1689 || trimmed.starts_with("f'")
1690 || trimmed.starts_with("F\"")
1691 || trimmed.starts_with("F'")
1692 {
1693 return Vec::new();
1694 }
1695 if let Some(inner) = trimmed.strip_prefix('[') {
1696 return split_literal_list(inner, ']');
1697 }
1698 if let Some(inner) = trimmed.strip_prefix('(') {
1699 return split_literal_list(inner, ')');
1700 }
1701 let Some((value, consumed)) = parse_static_literal(trimmed) else {
1702 return Vec::new();
1703 };
1704 let remainder = trimmed.get(consumed..).unwrap_or_default().trim_start();
1705 if remainder.is_empty() || remainder.starts_with([',', ')', ']', '}', ';', '.']) {
1706 vec![value]
1707 } else {
1708 Vec::new()
1709 }
1710}
1711
1712fn split_literal_list(source: &str, closing: char) -> Vec<String> {
1713 let mut output = Vec::new();
1714 let mut remainder = source;
1715 loop {
1716 remainder = remainder.trim_start();
1717 if remainder.starts_with(closing) || remainder.is_empty() {
1718 break;
1719 }
1720 let Some((value, consumed)) = parse_static_literal(remainder) else {
1721 return Vec::new();
1722 };
1723 output.push(value);
1724 remainder = remainder.get(consumed..).unwrap_or_default().trim_start();
1725 if remainder.starts_with(',') {
1726 remainder = &remainder[1..];
1727 } else if !remainder.starts_with(closing) {
1728 return Vec::new();
1729 }
1730 }
1731 output
1732}
1733
1734fn parse_static_literal(source: &str) -> Option<(String, usize)> {
1735 let (prefix, delimiter, raw) = if source.starts_with("r#\"") {
1736 ("r#\"", '"', true)
1737 } else if source.starts_with("r\"") {
1738 ("r\"", '"', true)
1739 } else if source.starts_with('"') {
1740 ("\"", '"', false)
1741 } else if source.starts_with('\'') {
1742 ("'", '\'', false)
1743 } else if source.starts_with('`') {
1744 ("`", '`', false)
1745 } else {
1746 return None;
1747 };
1748 let mut escaped = false;
1749 let mut value = String::new();
1750 let content_start = prefix.len();
1751 for (relative, character) in source[content_start..].char_indices() {
1752 if raw && character == delimiter {
1753 let end = content_start + relative;
1754 let suffix = if prefix == "r#\"" { "\"#" } else { "\"" };
1755 if source[end..].starts_with(suffix) {
1756 return Some((value, end + suffix.len()));
1757 }
1758 value.push(character);
1759 continue;
1760 }
1761 if !raw && escaped {
1762 value.push(match character {
1763 'n' => '\n',
1764 'r' => '\r',
1765 't' => '\t',
1766 other => other,
1767 });
1768 escaped = false;
1769 continue;
1770 }
1771 if !raw && character == '\\' {
1772 escaped = true;
1773 continue;
1774 }
1775 if !raw && character == delimiter {
1776 if delimiter == '`' && value.contains("${") {
1777 return None;
1778 }
1779 return Some((value, content_start + relative + character.len_utf8()));
1780 }
1781 value.push(character);
1782 }
1783 None
1784}
1785
1786fn is_identifier_boundary(source: &str, position: usize, length: usize) -> bool {
1787 let before = source[..position].chars().next_back();
1788 let after = source[position + length..].chars().next();
1789 !before.is_some_and(is_identifier_character) && !after.is_some_and(is_identifier_character)
1790}
1791
1792fn is_identifier_character(character: char) -> bool {
1793 character.is_ascii_alphanumeric() || character == '_'
1794}
1795
1796fn source_candidate(lines: &[String], start: usize) -> Option<String> {
1797 let first = lines.get(start)?.trim();
1798 if first.is_empty() || !possible_event_line(first) {
1799 return None;
1800 }
1801 let mut candidate = String::new();
1802 let mut balance = 0_i32;
1803 for line in lines.iter().skip(start).take(12) {
1804 if !candidate.is_empty() {
1805 candidate.push(' ');
1806 }
1807 candidate.push_str(line.trim());
1808 balance += delimiter_balance(line);
1809 if balance <= 0 && candidate.contains('(') {
1810 break;
1811 }
1812 }
1813 Some(candidate)
1814}
1815
1816const EVENT_LINE_MARKERS: [&str; 13] = [
1817 "publish",
1818 "subscribe",
1819 "send",
1820 "receive",
1821 "consume",
1822 "produce",
1823 "queue",
1824 "topic",
1825 "writemessages",
1826 "basic_",
1827 "createstream",
1828 "create_stream",
1829 "pull(",
1830];
1831
1832static EVENT_MARKERS: LazyLock<MarkerSet> = LazyLock::new(|| MarkerSet::new(&EVENT_LINE_MARKERS));
1833
1834fn possible_event_line(line: &str) -> bool {
1835 EVENT_MARKERS.any_in(line)
1836}
1837
1838fn delimiter_balance(line: &str) -> i32 {
1839 let mut balance = 0_i32;
1840 let mut quote = None;
1841 let mut escaped = false;
1842 for character in line.chars() {
1843 if let Some(delimiter) = quote {
1844 if escaped {
1845 escaped = false;
1846 } else if character == '\\' {
1847 escaped = true;
1848 } else if character == delimiter {
1849 quote = None;
1850 }
1851 continue;
1852 }
1853 match character {
1854 '"' | '\'' | '`' => quote = Some(character),
1855 '(' | '[' | '{' => balance += 1,
1856 ')' | ']' | '}' => balance -= 1,
1857 _ => {}
1858 }
1859 }
1860 balance
1861}
1862
1863fn sanitize_source(language: SourceLanguage, input: &str) -> Vec<String> {
1864 let hash_comments = matches!(language, SourceLanguage::Python);
1865 let mut block_comment = false;
1866 input
1867 .lines()
1868 .map(|line| sanitize_line(line, hash_comments, &mut block_comment))
1869 .collect()
1870}
1871
1872fn sanitize_line(line: &str, hash_comments: bool, block_comment: &mut bool) -> String {
1873 let characters = line.char_indices().collect::<Vec<_>>();
1874 let mut output = String::with_capacity(line.len());
1875 let mut index = 0;
1876 let mut quote = None;
1877 let mut escaped = false;
1878 while let Some(&(offset, character)) = characters.get(index) {
1879 let next = characters.get(index + 1).map(|(_, value)| *value);
1880 if *block_comment {
1881 if character == '*' && next == Some('/') {
1882 *block_comment = false;
1883 index += 2;
1884 } else {
1885 index += 1;
1886 }
1887 continue;
1888 }
1889 if let Some(delimiter) = quote {
1890 output.push(character);
1891 if escaped {
1892 escaped = false;
1893 } else if character == '\\' {
1894 escaped = true;
1895 } else if character == delimiter {
1896 quote = None;
1897 }
1898 index += 1;
1899 continue;
1900 }
1901 if matches!(character, '"' | '\'' | '`') {
1902 quote = Some(character);
1903 output.push(character);
1904 index += 1;
1905 continue;
1906 }
1907 if character == '/' && next == Some('/') {
1908 break;
1909 }
1910 if character == '/' && next == Some('*') {
1911 *block_comment = true;
1912 index += 2;
1913 continue;
1914 }
1915 if hash_comments && character == '#' {
1916 break;
1917 }
1918 output.push_str(&line[offset..offset + character.len_utf8()]);
1919 index += 1;
1920 }
1921 output
1922}
1923
1924fn mask_string_contents(source: &str) -> String {
1925 let mut output = String::with_capacity(source.len());
1926 let mut quote = None;
1927 let mut escaped = false;
1928 for character in source.chars() {
1929 if let Some(delimiter) = quote {
1930 if escaped {
1931 escaped = false;
1932 output.push(' ');
1933 } else if character == '\\' {
1934 escaped = true;
1935 output.push(' ');
1936 } else if character == delimiter {
1937 quote = None;
1938 output.push(character);
1939 } else {
1940 output.push(' ');
1941 }
1942 } else if matches!(character, '"' | '\'' | '`') {
1943 quote = Some(character);
1944 output.push(character);
1945 } else {
1946 output.push(character);
1947 }
1948 }
1949 output
1950}
1951
1952fn compact_lowercase(source: &str) -> String {
1953 source
1954 .chars()
1955 .filter(|character| !character.is_whitespace())
1956 .flat_map(char::to_lowercase)
1957 .collect()
1958}
1959
1960fn bounded_source(input: &str) -> (&str, bool) {
1961 if input.len() <= MAX_SOURCE_BYTES {
1962 return (input, false);
1963 }
1964 let boundary = input.floor_char_boundary(MAX_SOURCE_BYTES);
1965 (&input[..boundary], true)
1966}
1967
1968fn bounded_text(value: &str, max_chars: usize) -> String {
1969 let mut output = value.chars().take(max_chars).collect::<String>();
1970 if value.chars().count() > max_chars {
1971 output.push_str("...");
1972 }
1973 output
1974}
1975
1976fn normalize_observation(observation: &mut EventObservation) {
1977 observation.warnings.sort();
1978 observation.warnings.dedup();
1979 observation.evidence.sort();
1980 observation.evidence.dedup();
1981 if let Some(schema) = &mut observation.schema {
1982 schema.fields.sort();
1983 schema.fields.dedup();
1984 }
1985 if !observation.warnings.is_empty() {
1986 observation.incomplete = true;
1987 }
1988}
1989
1990fn finish_document(document: &mut EventDocument) {
1991 for observation in &mut document.observations {
1992 normalize_observation(observation);
1993 }
1994 document.observations.sort_by_key(observation_sort_key);
1995 let mut deduplicated = Vec::<EventObservation>::new();
1996 for observation in std::mem::take(&mut document.observations) {
1997 if let Some(previous) = deduplicated.last_mut()
1998 && observation_sort_key(previous) == observation_sort_key(&observation)
1999 {
2000 previous.evidence.extend(observation.evidence);
2001 normalize_observation(previous);
2002 } else {
2003 deduplicated.push(observation);
2004 }
2005 }
2006 document.observations = deduplicated;
2007 document.warnings.sort();
2008 document.warnings.dedup();
2009 document.incomplete |= document
2010 .observations
2011 .iter()
2012 .any(|observation| observation.incomplete);
2013}
2014
2015fn observation_sort_key(observation: &EventObservation) -> String {
2016 format!(
2017 "{:?}\u{0}{:?}\u{0}{}\u{0}{}\u{0}{}\u{0}{}\u{0}{:?}\u{0}{:?}\u{0}{}\u{0}{}\u{0}{:?}\u{0}{}\u{0}{:?}\u{0}{}\u{0}{:?}",
2018 observation.broker,
2019 observation.role,
2020 observation.channel.as_deref().unwrap_or_default(),
2021 observation.event_type.as_deref().unwrap_or_default(),
2022 observation.namespace.as_deref().unwrap_or_default(),
2023 observation.protocol.as_deref().unwrap_or_default(),
2024 observation.schema,
2025 observation.partition_key,
2026 observation.routing_key.as_deref().unwrap_or_default(),
2027 observation
2028 .dead_letter_channel
2029 .as_deref()
2030 .unwrap_or_default(),
2031 observation.delivery_semantics,
2032 observation.confidence,
2033 observation.language,
2034 observation.incomplete,
2035 observation.warnings
2036 )
2037}
2038
2039#[cfg(test)]
2040mod tests {
2041 #[test]
2042 fn source_marker_prefilter_should_not_change_any_corpus_document() {
2043 let corpus = crate::markers::differential_corpus();
2044 assert!(
2045 corpus.len() > 100,
2046 "differential corpus is unexpectedly small"
2047 );
2048 for (language, path, input) in corpus {
2049 assert_eq!(
2050 super::parse_event_source_with_prefilter(language, &input, true),
2051 super::parse_event_source_with_prefilter(language, &input, false),
2052 "event prefilter changed {path}"
2053 );
2054 }
2055 }
2056
2057 use super::*;
2058
2059 fn source_observation(language: SourceLanguage, source: &str) -> EventObservation {
2060 parse_event_source(language, source)
2061 .observations
2062 .into_iter()
2063 .next()
2064 .expect("fixture should produce an observation")
2065 }
2066
2067 #[test]
2068 fn asyncapi_two_extracts_kafka_message_schema_and_bindings() {
2069 let source = r#"
2070asyncapi: 2.6.0
2071servers:
2072 production:
2073 url: kafka.example.test:9092/orders
2074 protocol: kafka
2075channels:
2076 orders.created:
2077 bindings:
2078 kafka:
2079 topicConfiguration: {}
2080 publish:
2081 bindings:
2082 kafka:
2083 x-partition-key: tenant_id
2084 message:
2085 name: OrderCreated
2086 schemaFormat: application/schema+json;version=draft-07
2087 x-schema-version: "2"
2088 payload:
2089 type: object
2090 required: [id]
2091 properties:
2092 tenant_id: { type: string }
2093 id: { type: string }
2094"#;
2095 let document = extract_asyncapi("asyncapi.yaml", source).expect("fixture should parse");
2096
2097 assert!(matches!(
2098 document.observations.as_slice(),
2099 [observation]
2100 if observation.broker == EventBroker::Kafka
2101 && observation.role == EventRole::Publisher
2102 && observation.channel.as_deref() == Some("orders.created")
2103 && observation.partition_key.as_deref() == Some("tenant_id")
2104 && observation.schema.as_ref().is_some_and(|schema|
2105 schema.name.as_deref() == Some("OrderCreated")
2106 && schema.version.as_deref() == Some("2")
2107 && schema.fields.len() == 2)
2108 ));
2109 }
2110
2111 #[test]
2112 fn asyncapi_three_extracts_send_and_receive_operations() {
2113 let source = r"
2114asyncapi: 3.0.0
2115servers:
2116 broker:
2117 host: nats.example.test
2118 protocol: nats
2119channels:
2120 jobs:
2121 address: jobs.ready
2122 messages:
2123 Job:
2124 name: Job
2125 payload:
2126 type: object
2127 properties:
2128 id: { type: string }
2129operations:
2130 sendJob:
2131 action: send
2132 channel:
2133 $ref: '#/channels/jobs'
2134 receiveJob:
2135 action: receive
2136 channel:
2137 $ref: '#/channels/jobs'
2138";
2139 let document = extract_asyncapi("asyncapi.json", source).expect("fixture should parse");
2140
2141 assert!(
2142 document.observations.len() == 2
2143 && document.observations.iter().all(|observation| {
2144 observation.channel.as_deref() == Some("jobs.ready")
2145 && observation.namespace.as_deref() == Some("nats.example.test")
2146 && observation.broker == EventBroker::Nats
2147 })
2148 );
2149 }
2150
2151 #[test]
2152 fn asyncapi_multiple_server_protocols_remain_ambiguous() {
2153 let source = r"
2154asyncapi: 2.6.0
2155servers:
2156 kafka:
2157 url: kafka.example.test
2158 protocol: kafka
2159 amqp:
2160 url: rabbit.example.test
2161 protocol: amqp
2162channels:
2163 events:
2164 publish:
2165 message:
2166 name: Event
2167";
2168 let document = extract_asyncapi("asyncapi.yaml", source).expect("fixture should parse");
2169
2170 assert!(
2171 document.incomplete
2172 && document
2173 .warnings
2174 .iter()
2175 .any(|warning| { warning.contains("multiple server protocols") })
2176 );
2177 }
2178
2179 #[test]
2180 fn asyncapi_templated_channel_is_not_exact() {
2181 let source = r"
2182asyncapi: 2.6.0
2183channels:
2184 orders.{tenant}:
2185 subscribe:
2186 message:
2187 name: Order
2188";
2189 let document = extract_asyncapi("asyncapi.yaml", source).expect("fixture should parse");
2190
2191 assert!(matches!(
2192 document.observations.as_slice(),
2193 [observation]
2194 if observation.channel.is_none()
2195 && observation.incomplete
2196 && observation.confidence == 0.0
2197 ));
2198 }
2199
2200 #[test]
2201 fn asyncapi_supports_json_and_dead_letter_metadata() {
2202 let source = r#"{
2203 "asyncapi": "2.6.0",
2204 "servers": {"queue": {"url": "queue.example.test", "protocol": "sqs"}},
2205 "channels": {
2206 "jobs": {
2207 "subscribe": {
2208 "x-delivery-semantics": "at-least-once",
2209 "deadLetterQueue": "jobs-dead",
2210 "message": {"name": "Job"}
2211 }
2212 }
2213 }
2214 }"#;
2215 let document = extract_asyncapi("asyncapi.json", source).expect("fixture should parse");
2216
2217 assert!(matches!(
2218 document.observations.as_slice(),
2219 [observation]
2220 if observation.broker == EventBroker::AwsSqs
2221 && observation.delivery_semantics
2222 == Some(DeliverySemantics::AtLeastOnce)
2223 && observation.dead_letter_channel.as_deref() == Some("jobs-dead")
2224 ));
2225 }
2226
2227 #[test]
2228 fn kafka_literal_call_is_exact() {
2229 let observation = source_observation(
2230 SourceLanguage::Rust,
2231 r#"producer.send(FutureRecord::to("orders.created").payload(body));"#,
2232 );
2233
2234 assert!(
2235 observation.broker == EventBroker::Kafka
2236 && observation.channel.as_deref() == Some("orders.created")
2237 && observation.role == EventRole::Publisher
2238 );
2239 }
2240
2241 #[test]
2242 fn rabbitmq_literal_call_extracts_exchange() {
2243 let observation = source_observation(
2244 SourceLanguage::Python,
2245 r#"channel.basic_publish(exchange="orders", routing_key="created", body=data)"#,
2246 );
2247
2248 assert!(
2249 observation.broker == EventBroker::RabbitMq
2250 && observation.channel.as_deref() == Some("orders")
2251 && observation.routing_key.as_deref() == Some("created")
2252 );
2253 }
2254
2255 #[test]
2256 fn sns_literal_call_extracts_topic_arn() {
2257 let observation = source_observation(
2258 SourceLanguage::TypeScript,
2259 r#"sns.publish({ TopicArn: "arn:aws:sns:us-east-1:123:orders", Message: body });"#,
2260 );
2261
2262 assert!(
2263 observation.broker == EventBroker::AwsSns
2264 && observation.channel.as_deref() == Some("arn:aws:sns:us-east-1:123:orders")
2265 );
2266 }
2267
2268 #[test]
2269 fn sqs_literal_call_extracts_queue_url() {
2270 let observation = source_observation(
2271 SourceLanguage::JavaScript,
2272 r#"sqs.sendMessage({ QueueUrl: "https://sqs.example.test/orders", MessageBody: body });"#,
2273 );
2274
2275 assert!(
2276 observation.broker == EventBroker::AwsSqs
2277 && observation.channel.as_deref() == Some("https://sqs.example.test/orders")
2278 );
2279 }
2280
2281 #[test]
2282 fn nats_literal_call_extracts_subject() {
2283 let observation = source_observation(
2284 SourceLanguage::Go,
2285 r#"natsConnection.Publish("orders.created", payload)"#,
2286 );
2287
2288 assert!(
2289 observation.broker == EventBroker::Nats
2290 && observation.channel.as_deref() == Some("orders.created")
2291 );
2292 }
2293
2294 #[test]
2295 fn google_pubsub_literal_call_extracts_topic() {
2296 let observation = source_observation(
2297 SourceLanguage::Java,
2298 r#"pubsubPublisher.publish("projects/demo/topics/orders", message);"#,
2299 );
2300
2301 assert!(
2302 observation.broker == EventBroker::GooglePubSub
2303 && observation.channel.as_deref() == Some("projects/demo/topics/orders")
2304 );
2305 }
2306
2307 #[test]
2308 fn generic_literal_subscriber_is_exact() {
2309 let observation = source_observation(
2310 SourceLanguage::Python,
2311 r#"event_bus.subscribe("inventory.changed", handler)"#,
2312 );
2313
2314 assert!(
2315 observation.broker == EventBroker::Generic
2316 && observation.role == EventRole::Subscriber
2317 && observation.channel.as_deref() == Some("inventory.changed")
2318 );
2319 }
2320
2321 #[test]
2322 fn dynamic_channel_remains_incomplete_without_exact_value() {
2323 let observation = source_observation(
2324 SourceLanguage::TypeScript,
2325 "eventBus.publish(topicName, payload);",
2326 );
2327
2328 assert!(
2329 observation.channel.is_none()
2330 && observation.incomplete
2331 && observation.confidence == 0.0
2332 && observation
2333 .warnings
2334 .iter()
2335 .any(|warning| warning.contains("dynamic channel"))
2336 );
2337 }
2338
2339 #[test]
2340 fn interpolated_literal_remains_dynamic() {
2341 let observation = source_observation(
2342 SourceLanguage::JavaScript,
2343 "nats.publish(`orders.${tenant}`, payload);",
2344 );
2345
2346 assert!(observation.channel.is_none() && observation.incomplete);
2347 }
2348
2349 #[test]
2350 fn concatenated_literal_remains_dynamic() {
2351 let observation = source_observation(
2352 SourceLanguage::Java,
2353 r#"eventBus.publish("orders." + tenant, payload);"#,
2354 );
2355
2356 assert!(observation.channel.is_none() && observation.incomplete);
2357 }
2358
2359 #[test]
2360 fn call_text_inside_string_is_not_observed() {
2361 let document = parse_event_source(
2362 SourceLanguage::Rust,
2363 r#"let example = "event_bus.publish(\"orders\", payload)";"#,
2364 );
2365
2366 assert_eq!(document.observations, Vec::new());
2367 }
2368
2369 #[test]
2370 fn evidence_does_not_retain_source_or_payload() {
2371 let secret = "private-message-body";
2372 let document = parse_event_source(
2373 SourceLanguage::JavaScript,
2374 &format!(r#"eventBus.publish("orders", "{secret}");"#),
2375 );
2376 let serialized = serde_json::to_string(&document).expect("event document should serialize");
2377
2378 assert!(!serialized.contains(secret));
2379 }
2380
2381 #[test]
2382 fn source_results_are_sorted_and_deduplicated() {
2383 let document = parse_event_source(
2384 SourceLanguage::Python,
2385 "bus.publish(\"z\", body)\nbus.publish(\"a\", body)\nbus.publish(\"a\", body)",
2386 );
2387
2388 assert!(matches!(
2389 document.observations.as_slice(),
2390 [first, second]
2391 if first.channel.as_deref() == Some("a")
2392 && second.channel.as_deref() == Some("z")
2393 ));
2394 }
2395}