1#[cfg(not(target_arch = "wasm32"))]
7pub mod git;
8#[cfg(not(target_arch = "wasm32"))]
9pub mod http;
10#[cfg(not(target_arch = "wasm32"))]
11pub mod onchain;
12pub mod path;
13
14use crate::file::ImportSource;
15use std::path::PathBuf;
16
17#[derive(Debug, Clone)]
23pub struct FetcherConfig {
24 pub allow_path: bool,
26 pub allow_git: bool,
28 pub allow_http: bool,
30 pub allow_onchain: bool,
32
33 pub git_config: GitFetcherConfig,
35
36 pub onchain_config: OnchainFetcherConfig,
38
39 pub cache_config: CacheConfig,
41}
42
43impl Default for FetcherConfig {
44 fn default() -> Self {
45 Self::cli_default()
46 }
47}
48
49impl FetcherConfig {
50 pub fn cli_default() -> Self {
52 Self {
53 allow_path: true,
54 allow_git: true,
55 allow_http: true,
56 allow_onchain: true,
57 git_config: GitFetcherConfig::default(),
58 onchain_config: OnchainFetcherConfig::default(),
59 cache_config: CacheConfig::default(),
60 }
61 }
62
63 pub fn wasm_default() -> Self {
65 Self {
66 allow_path: false,
67 allow_git: false,
68 allow_http: false,
69 allow_onchain: false,
70 git_config: GitFetcherConfig::default(),
71 onchain_config: OnchainFetcherConfig::default(),
72 cache_config: CacheConfig::disabled(),
73 }
74 }
75
76 pub fn production_build() -> Self {
78 Self {
79 allow_path: false,
80 allow_git: false,
81 allow_http: false,
82 allow_onchain: true,
83 git_config: GitFetcherConfig::default(),
84 onchain_config: OnchainFetcherConfig::default(),
85 cache_config: CacheConfig::default(),
86 }
87 }
88
89 pub fn local_only() -> Self {
91 Self {
92 allow_path: true,
93 allow_git: false,
94 allow_http: false,
95 allow_onchain: false,
96 git_config: GitFetcherConfig::default(),
97 onchain_config: OnchainFetcherConfig::default(),
98 cache_config: CacheConfig::disabled(),
99 }
100 }
101
102 pub fn is_allowed(&self, source: &ImportSource) -> bool {
104 match source {
105 ImportSource::Path { .. } => self.allow_path,
106 ImportSource::Git { .. } => self.allow_git,
107 ImportSource::Http { .. } => self.allow_http,
108 ImportSource::Onchain { .. } => self.allow_onchain,
109 }
110 }
111}
112
113#[derive(Debug, Clone, Default)]
115pub struct GitFetcherConfig {
116 pub ssh_key_path: Option<PathBuf>,
118 pub use_credential_helper: bool,
120 pub proxy: Option<String>,
122 pub timeout_seconds: u64,
124}
125
126impl GitFetcherConfig {
127 pub fn new() -> Self {
129 Self {
130 ssh_key_path: None,
131 use_credential_helper: true,
132 proxy: None,
133 timeout_seconds: 60,
134 }
135 }
136}
137
138#[derive(Debug, Clone)]
140pub struct OnchainFetcherConfig {
141 pub rpc_endpoints: std::collections::HashMap<String, String>,
143 pub default_network: String,
145 pub timeout_seconds: u64,
147 pub abi_manager_program_id: String,
149 pub abi_manager_is_ephemeral: bool,
151}
152
153impl Default for OnchainFetcherConfig {
154 fn default() -> Self {
155 let mut rpc_endpoints = std::collections::HashMap::new();
156 rpc_endpoints.insert("mainnet".to_string(), "https://rpc.thru.network".to_string());
157 rpc_endpoints.insert(
158 "testnet".to_string(),
159 "https://rpc-testnet.thru.network".to_string(),
160 );
161
162 Self {
163 rpc_endpoints,
164 default_network: "mainnet".to_string(),
165 timeout_seconds: 30,
166 abi_manager_program_id: "taAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACrG7".to_string(),
167 abi_manager_is_ephemeral: false,
168 }
169 }
170}
171
172impl OnchainFetcherConfig {
173 pub fn get_endpoint(&self, network: &str) -> Option<&str> {
175 self.rpc_endpoints.get(network).map(|s| s.as_str())
176 }
177
178 pub fn set_endpoint(&mut self, network: impl Into<String>, endpoint: impl Into<String>) {
180 self.rpc_endpoints.insert(network.into(), endpoint.into());
181 }
182}
183
184#[derive(Debug, Clone)]
186pub struct CacheConfig {
187 pub enabled: bool,
189 pub cache_dir: PathBuf,
191 pub max_age_seconds: u64,
193}
194
195impl Default for CacheConfig {
196 fn default() -> Self {
197 Self {
198 enabled: true,
199 cache_dir: default_cache_dir(),
200 max_age_seconds: 3600, }
202 }
203}
204
205#[cfg(not(target_arch = "wasm32"))]
206fn default_cache_dir() -> PathBuf {
207 dirs::home_dir()
208 .unwrap_or_else(|| PathBuf::from("."))
209 .join(".thru")
210 .join("abi-cache")
211}
212
213#[cfg(target_arch = "wasm32")]
214fn default_cache_dir() -> PathBuf {
215 PathBuf::new()
216}
217
218impl CacheConfig {
219 pub fn disabled() -> Self {
221 Self {
222 enabled: false,
223 cache_dir: PathBuf::new(),
224 max_age_seconds: 0,
225 }
226 }
227
228 pub fn with_dir(cache_dir: PathBuf) -> Self {
230 Self {
231 enabled: true,
232 cache_dir,
233 max_age_seconds: 3600,
234 }
235 }
236}
237
238#[derive(Debug, Clone)]
244pub struct FetchContext {
245 pub base_path: Option<PathBuf>,
247 pub parent_is_remote: bool,
249 pub include_dirs: Vec<PathBuf>,
251}
252
253impl FetchContext {
254 pub fn for_root(file_path: Option<PathBuf>, include_dirs: Vec<PathBuf>) -> Self {
256 Self {
257 base_path: file_path,
258 parent_is_remote: false,
259 include_dirs,
260 }
261 }
262
263 pub fn child_context(&self, source: &ImportSource, resolved_path: Option<PathBuf>) -> Self {
265 Self {
266 base_path: resolved_path,
267 parent_is_remote: source.is_remote(),
268 include_dirs: self.include_dirs.clone(),
269 }
270 }
271}
272
273#[derive(Debug, Clone)]
279pub struct FetchResult {
280 pub content: String,
282 pub canonical_location: String,
284 pub is_remote: bool,
286 pub resolved_path: Option<PathBuf>,
288}
289
290#[derive(Debug)]
296pub enum FetchError {
297 UnsupportedSource(String),
299 NotAllowed(ImportSource),
301 LocalFromRemote(String),
303 NotFound(String),
305 Io(std::io::Error),
307 Git(String),
309 Http { status: u16, message: String },
311 Onchain(String),
313 Parse(String),
315 UnknownNetwork(String),
317 RevisionMismatch { required: String, actual: u64 },
319}
320
321impl std::fmt::Display for FetchError {
322 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
323 match self {
324 FetchError::UnsupportedSource(s) => write!(f, "Unsupported import source: {}", s),
325 FetchError::NotAllowed(s) => write!(f, "Import type not allowed: {:?}", s),
326 FetchError::LocalFromRemote(s) => {
327 write!(f, "Local import '{}' not allowed from remote source", s)
328 }
329 FetchError::NotFound(s) => write!(f, "Import not found: {}", s),
330 FetchError::Io(e) => write!(f, "IO error: {}", e),
331 FetchError::Git(s) => write!(f, "Git error: {}", s),
332 FetchError::Http { status, message } => {
333 write!(f, "HTTP error {}: {}", status, message)
334 }
335 FetchError::Onchain(s) => write!(f, "On-chain fetch error: {}", s),
336 FetchError::Parse(s) => write!(f, "Parse error: {}", s),
337 FetchError::UnknownNetwork(s) => write!(f, "Unknown network: {}", s),
338 FetchError::RevisionMismatch { required, actual } => {
339 write!(f, "Revision mismatch: required {}, got {}", required, actual)
340 }
341 }
342 }
343}
344
345impl std::error::Error for FetchError {
346 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
347 match self {
348 FetchError::Io(e) => Some(e),
349 _ => None,
350 }
351 }
352}
353
354impl From<std::io::Error> for FetchError {
355 fn from(e: std::io::Error) -> Self {
356 FetchError::Io(e)
357 }
358}
359
360pub trait ImportFetcher: Send + Sync {
366 fn handles(&self, source: &ImportSource) -> bool;
368
369 fn fetch(&self, source: &ImportSource, ctx: &FetchContext) -> Result<FetchResult, FetchError>;
371}
372
373pub struct CompositeFetcher {
379 fetchers: Vec<Box<dyn ImportFetcher>>,
380 config: FetcherConfig,
381}
382
383impl CompositeFetcher {
384 pub fn new(config: FetcherConfig) -> Result<Self, FetchError> {
386 let mut fetchers: Vec<Box<dyn ImportFetcher>> = Vec::new();
387
388 if config.allow_path {
389 fetchers.push(Box::new(path::PathFetcher::new()));
390 }
391 #[cfg(not(target_arch = "wasm32"))]
392 if config.allow_git {
393 fetchers.push(Box::new(git::GitFetcher::new(&config.git_config)));
394 }
395 #[cfg(not(target_arch = "wasm32"))]
396 if config.allow_http {
397 fetchers.push(Box::new(http::HttpFetcher::new()?));
398 }
399 #[cfg(not(target_arch = "wasm32"))]
400 if config.allow_onchain {
401 fetchers.push(Box::new(onchain::OnchainFetcher::new(&config.onchain_config)));
402 }
403
404 Ok(Self { fetchers, config })
405 }
406
407 pub fn fetch(
409 &self,
410 source: &ImportSource,
411 ctx: &FetchContext,
412 ) -> Result<FetchResult, FetchError> {
413 if !self.config.is_allowed(source) {
415 return Err(FetchError::NotAllowed(source.clone()));
416 }
417
418 for fetcher in &self.fetchers {
420 if fetcher.handles(source) {
421 return fetcher.fetch(source, ctx);
422 }
423 }
424
425 Err(FetchError::UnsupportedSource(format!("{:?}", source)))
426 }
427
428 pub fn config(&self) -> &FetcherConfig {
430 &self.config
431 }
432}
433
434#[cfg(test)]
435mod tests {
436 use super::*;
437
438 #[test]
439 fn test_fetcher_config_is_allowed() {
440 let config = FetcherConfig::local_only();
441
442 let path_import = ImportSource::Path {
443 path: "test.abi.yaml".to_string(),
444 };
445 let git_import = ImportSource::Git {
446 url: "https://github.com/test/repo".to_string(),
447 git_ref: "main".to_string(),
448 path: "abi.yaml".to_string(),
449 };
450
451 assert!(config.is_allowed(&path_import));
452 assert!(!config.is_allowed(&git_import));
453 }
454
455 #[test]
456 fn test_cache_config_default() {
457 let config = CacheConfig::default();
458 assert!(config.enabled);
459 assert!(config.cache_dir.to_string_lossy().contains(".thru"));
460 }
461}