Skip to main content

solcompat_core/
signals.rs

1//! Typed framework observations with an adapter for schema-1 evidence kinds.
2
3use crate::{Evidence, Program};
4
5/// A recognized source construct, not proof of semantic symbol identity.
6/// Variant ordering preserves the existing evidence ordering by kind string.
7#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
8pub enum SourceSignal {
9    AnchorCpiContextAccountInfo,
10    AnchorDiscriminatorMethod,
11    PinocchioAccountInfo,
12    PinocchioImmutableEntrypoint,
13    PinocchioMutableEntrypoint,
14    PinocchioPubkey,
15}
16
17impl SourceSignal {
18    /// Existing schema-1 identifier; serialized models continue to use strings.
19    pub const fn evidence_kind(self) -> &'static str {
20        match self {
21            Self::AnchorCpiContextAccountInfo => "anchor-cpi-context-account-info",
22            Self::AnchorDiscriminatorMethod => "anchor-discriminator-method",
23            Self::PinocchioAccountInfo => "pinocchio-account-info",
24            Self::PinocchioImmutableEntrypoint => "pinocchio-immutable-entrypoint",
25            Self::PinocchioMutableEntrypoint => "pinocchio-mutable-entrypoint",
26            Self::PinocchioPubkey => "pinocchio-pubkey",
27        }
28    }
29
30    // Preserve broad prefix filtering for callers' existing schema-1 evidence.
31    // Recognizing a typed signal and selecting framework evidence are distinct.
32    pub(crate) fn is_framework_evidence(kind: &str) -> bool {
33        Self::is_pinocchio_evidence(kind) || kind.starts_with("anchor-")
34    }
35
36    pub(crate) fn is_pinocchio_evidence(kind: &str) -> bool {
37        kind.starts_with("pinocchio-")
38    }
39
40    pub(crate) fn present(self, program: &Program) -> bool {
41        program
42            .evidence
43            .iter()
44            .any(|item| item.kind == self.evidence_kind())
45    }
46
47    pub(crate) fn evidence(self, program: &Program) -> Option<Vec<Evidence>> {
48        let evidence: Vec<_> = program
49            .evidence
50            .iter()
51            .filter(|item| item.kind == self.evidence_kind())
52            .cloned()
53            .collect();
54        (!evidence.is_empty()).then_some(evidence)
55    }
56}