proa_source_tools/lint/
rsjs_strategy.rs1use super::*;
5
6#[derive(Debug, Clone)]
7pub struct RsjsStrategyCacheOptions {
8 pub root: PathBuf,
9 pub require_fresh: bool,
10}
11
12#[derive(Debug)]
13pub(crate) struct RsjsStrategyCache {
14 pub(crate) sidecars: Vec<crate::rsjs::StrategySidecarSummary>,
15 pub(crate) require_fresh: bool,
16}
17
18#[derive(Debug)]
19pub(crate) enum RsjsStrategyMatch<'a> {
20 Found(&'a crate::rsjs::StrategySidecarSummary),
21 Missing,
22 Stale(Vec<&'a crate::rsjs::StrategySidecarSummary>),
23 Ambiguous(Vec<&'a crate::rsjs::StrategySidecarSummary>),
24}
25
26impl RsjsStrategyCache {
27 pub(crate) fn load(options: &RsjsStrategyCacheOptions) -> Self {
28 Self {
29 sidecars: crate::rsjs::strategy_sidecar_summaries(&options.root),
30 require_fresh: options.require_fresh,
31 }
32 }
33
34 pub(crate) fn lookup_impl(&self, item_impl: &ItemImpl) -> RsjsStrategyMatch<'_> {
35 let source_fingerprint = rsjs_input_fingerprint_for_impl(item_impl);
36 if let Some(fingerprint) = &source_fingerprint {
37 let fingerprint_matches: Vec<_> = self
38 .sidecars
39 .iter()
40 .filter(|sidecar| {
41 sidecar.input_fingerprint.as_deref() == Some(fingerprint.as_str())
42 && sidecar_source_matches_impl(sidecar.source_anchor.as_ref(), item_impl)
43 })
44 .collect();
45 if fingerprint_matches.len() == 1 {
46 return RsjsStrategyMatch::Found(fingerprint_matches[0]);
47 }
48 if fingerprint_matches.len() > 1 {
49 return RsjsStrategyMatch::Ambiguous(fingerprint_matches);
50 }
51 }
52
53 let self_ty = item_impl.self_ty.to_token_stream().to_string();
54 let exact: Vec<_> = self
55 .sidecars
56 .iter()
57 .filter(|sidecar| sidecar.struct_path == self_ty)
58 .collect();
59 if exact.len() == 1 {
60 if sidecar_is_stale_for_impl(exact[0], source_fingerprint.as_deref(), item_impl) {
61 return RsjsStrategyMatch::Stale(exact);
62 }
63 return RsjsStrategyMatch::Found(exact[0]);
64 }
65 if exact.len() > 1 {
66 if source_fingerprint.is_some()
67 && exact
68 .iter()
69 .all(|sidecar| sidecar.input_fingerprint.is_some())
70 {
71 return RsjsStrategyMatch::Stale(exact);
72 }
73 return RsjsStrategyMatch::Ambiguous(exact);
74 }
75
76 let Some(struct_name) = type_path_last_ident(&item_impl.self_ty) else {
77 return RsjsStrategyMatch::Missing;
78 };
79 let named: Vec<_> = self
80 .sidecars
81 .iter()
82 .filter(|sidecar| sidecar.struct_name == struct_name)
83 .collect();
84 if named.len() == 1 {
85 if sidecar_is_stale_for_impl(named[0], source_fingerprint.as_deref(), item_impl) {
86 return RsjsStrategyMatch::Stale(named);
87 }
88 RsjsStrategyMatch::Found(named[0])
89 } else if named.len() > 1 {
90 if source_fingerprint.is_some()
91 && named
92 .iter()
93 .all(|sidecar| sidecar.input_fingerprint.is_some())
94 {
95 return RsjsStrategyMatch::Stale(named);
96 }
97 RsjsStrategyMatch::Ambiguous(named)
98 } else {
99 RsjsStrategyMatch::Missing
100 }
101 }
102}
103
104pub(crate) fn sidecar_is_stale_for_impl(
105 sidecar: &crate::rsjs::StrategySidecarSummary,
106 source_fingerprint: Option<&str>,
107 item_impl: &ItemImpl,
108) -> bool {
109 let fingerprint_stale = match (sidecar.input_fingerprint.as_deref(), source_fingerprint) {
110 (Some(sidecar), Some(source)) => sidecar != source,
111 _ => false,
112 };
113 let source_anchor_stale = sidecar
114 .source_anchor
115 .as_ref()
116 .is_some_and(|anchor| !sidecar_source_matches_impl(Some(anchor), item_impl));
117 fingerprint_stale || source_anchor_stale
118}
119
120pub(crate) fn sidecar_source_matches_impl(
121 anchor: Option<&crate::rsjs::StrategySourceAnchor>,
122 item_impl: &ItemImpl,
123) -> bool {
124 let Some(anchor) = anchor else {
125 return true;
126 };
127 let start = item_impl.impl_token.span.start();
128 let line_matches = anchor.impl_start_line.is_none_or(|line| line == start.line);
129 let column_matches = anchor
130 .impl_start_column
131 .is_none_or(|column| column == start.column + 1);
132 line_matches && column_matches
133}
134
135pub(crate) fn rsjs_input_fingerprint_for_impl(item_impl: &ItemImpl) -> Option<String> {
136 let rsjs_attr = item_impl
137 .attrs
138 .iter()
139 .find(|attr| has_attr(std::slice::from_ref(attr), &["rsjs"]))?;
140 let attr_tokens = rsjs_attr_argument_tokens(rsjs_attr)?;
141 let mut item_without_rsjs_attr = item_impl.clone();
142 item_without_rsjs_attr
143 .attrs
144 .retain(|attr| !has_attr(std::slice::from_ref(attr), &["rsjs"]));
145 Some(rsjs_strategy_input_fingerprint(
146 &attr_tokens,
147 &item_without_rsjs_attr.to_token_stream(),
148 ))
149}
150
151pub(crate) fn rsjs_attr_argument_tokens(attr: &SynAttribute) -> Option<TokenStream> {
152 match &attr.meta {
153 syn::Meta::Path(_) => Some(TokenStream::new()),
154 syn::Meta::List(list) => Some(list.tokens.clone()),
155 syn::Meta::NameValue(_) => None,
156 }
157}
158
159pub(crate) fn rsjs_strategy_input_fingerprint(
160 attr_tokens: &TokenStream,
161 impl_tokens: &TokenStream,
162) -> String {
163 let mut hasher = Sha256::new();
164 hasher.update(b"rsjs.strategy.input.v1");
165 hasher.update(b"\0attr\0");
166 hasher.update(normalized_token_string(attr_tokens).as_bytes());
167 hasher.update(b"\0impl\0");
168 hasher.update(normalized_token_string(impl_tokens).as_bytes());
169 let digest = hasher.finalize();
170 let hex: String = digest.iter().map(|byte| format!("{byte:02x}")).collect();
171 format!("sha256:{hex}")
172}
173
174pub(crate) fn normalized_token_string(tokens: &TokenStream) -> String {
175 tokens
176 .to_string()
177 .chars()
178 .filter(|ch| !ch.is_whitespace())
179 .collect()
180}