Skip to main content

abi_loader/fetcher/
mod.rs

1//! Import Fetcher Infrastructure
2//!
3//! This module provides a pluggable fetcher system for resolving ABI imports
4//! from various sources: local paths, git repositories, HTTP URLs, and on-chain.
5
6#[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/* ============================================================================
18   Fetcher Configuration
19   ============================================================================ */
20
21/* Configuration for which import types are allowed */
22#[derive(Debug, Clone)]
23pub struct FetcherConfig {
24    /* Allow local path imports */
25    pub allow_path: bool,
26    /* Allow git repository imports */
27    pub allow_git: bool,
28    /* Allow HTTP/HTTPS URL imports */
29    pub allow_http: bool,
30    /* Allow on-chain imports */
31    pub allow_onchain: bool,
32
33    /* Git-specific configuration */
34    pub git_config: GitFetcherConfig,
35
36    /* On-chain specific configuration */
37    pub onchain_config: OnchainFetcherConfig,
38
39    /* Caching configuration */
40    pub cache_config: CacheConfig,
41}
42
43impl Default for FetcherConfig {
44    fn default() -> Self {
45        Self::cli_default()
46    }
47}
48
49impl FetcherConfig {
50    /* Default configuration for CLI usage - all import types allowed */
51    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    /* Configuration for WASM runtime - no remote fetching */
64    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    /* Configuration for production builds - only on-chain allowed */
77    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    /* Configuration for local development - only path imports */
90    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    /* Check if a given import source is allowed by this configuration */
103    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/* Git fetcher configuration */
114#[derive(Debug, Clone, Default)]
115pub struct GitFetcherConfig {
116    /* Path to SSH key for authentication (optional, uses ssh-agent by default) */
117    pub ssh_key_path: Option<PathBuf>,
118    /* Use git credential helper for HTTPS auth */
119    pub use_credential_helper: bool,
120    /* HTTP/HTTPS proxy URL */
121    pub proxy: Option<String>,
122    /* Timeout for git operations in seconds */
123    pub timeout_seconds: u64,
124}
125
126impl GitFetcherConfig {
127    /* Create with default timeout */
128    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/* On-chain fetcher configuration */
139#[derive(Debug, Clone)]
140pub struct OnchainFetcherConfig {
141    /* Map of network name to RPC endpoint URL */
142    pub rpc_endpoints: std::collections::HashMap<String, String>,
143    /* Default network to use if not specified in import */
144    pub default_network: String,
145    /* Timeout for RPC calls in seconds */
146    pub timeout_seconds: u64,
147    /* ABI manager program public key (Thru address) */
148    pub abi_manager_program_id: String,
149    /* Whether ABI manager accounts are ephemeral */
150    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    /* Get the RPC endpoint for a given network */
174    pub fn get_endpoint(&self, network: &str) -> Option<&str> {
175        self.rpc_endpoints.get(network).map(|s| s.as_str())
176    }
177
178    /* Add or update an RPC endpoint */
179    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/* Cache configuration */
185#[derive(Debug, Clone)]
186pub struct CacheConfig {
187    /* Enable caching */
188    pub enabled: bool,
189    /* Directory for cached imports */
190    pub cache_dir: PathBuf,
191    /* Maximum age of cached items in seconds (0 = no expiry) */
192    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, /* 1 hour */
201        }
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    /* Create a disabled cache configuration */
220    pub fn disabled() -> Self {
221        Self {
222            enabled: false,
223            cache_dir: PathBuf::new(),
224            max_age_seconds: 0,
225        }
226    }
227
228    /* Create with custom cache directory */
229    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/* ============================================================================
239   Fetch Context
240   ============================================================================ */
241
242/* Context passed to fetchers during resolution */
243#[derive(Debug, Clone)]
244pub struct FetchContext {
245    /* Base path for resolving relative path imports */
246    pub base_path: Option<PathBuf>,
247    /* True if the parent import was from a remote source */
248    pub parent_is_remote: bool,
249    /* Include directories for path resolution */
250    pub include_dirs: Vec<PathBuf>,
251}
252
253impl FetchContext {
254    /* Create a new fetch context for a root file */
255    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    /* Create a child context for an import from this context */
264    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/* ============================================================================
274   Fetch Result
275   ============================================================================ */
276
277/* Result of successfully fetching an ABI file */
278#[derive(Debug, Clone)]
279pub struct FetchResult {
280    /* Raw YAML content of the ABI file */
281    pub content: String,
282    /* Canonical location identifier (for caching and cycle detection) */
283    pub canonical_location: String,
284    /* Whether the source is remote (git, http, onchain) */
285    pub is_remote: bool,
286    /* Resolved file path (for path imports only) */
287    pub resolved_path: Option<PathBuf>,
288}
289
290/* ============================================================================
291   Fetch Error
292   ============================================================================ */
293
294/* Errors that can occur during fetching */
295#[derive(Debug)]
296pub enum FetchError {
297    /* Import source type not supported by this fetcher */
298    UnsupportedSource(String),
299    /* Import source type not allowed by configuration */
300    NotAllowed(ImportSource),
301    /* Local import from remote parent not allowed */
302    LocalFromRemote(String),
303    /* File not found */
304    NotFound(String),
305    /* IO error */
306    Io(std::io::Error),
307    /* Git operation failed */
308    Git(String),
309    /* HTTP request failed */
310    Http { status: u16, message: String },
311    /* On-chain fetch failed */
312    Onchain(String),
313    /* Parse error */
314    Parse(String),
315    /* Network not configured */
316    UnknownNetwork(String),
317    /* Revision mismatch */
318    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
360/* ============================================================================
361   Fetcher Trait
362   ============================================================================ */
363
364/* Trait for import source fetchers */
365pub trait ImportFetcher: Send + Sync {
366    /* Check if this fetcher handles the given import source type */
367    fn handles(&self, source: &ImportSource) -> bool;
368
369    /* Fetch the ABI content from the source */
370    fn fetch(&self, source: &ImportSource, ctx: &FetchContext) -> Result<FetchResult, FetchError>;
371}
372
373/* ============================================================================
374   Composite Fetcher
375   ============================================================================ */
376
377/* Composite fetcher that delegates to the appropriate backend */
378pub struct CompositeFetcher {
379    fetchers: Vec<Box<dyn ImportFetcher>>,
380    config: FetcherConfig,
381}
382
383impl CompositeFetcher {
384    /* Create a new composite fetcher with the given configuration */
385    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    /* Fetch an import source */
408    pub fn fetch(
409        &self,
410        source: &ImportSource,
411        ctx: &FetchContext,
412    ) -> Result<FetchResult, FetchError> {
413        /* Check if source type is allowed */
414        if !self.config.is_allowed(source) {
415            return Err(FetchError::NotAllowed(source.clone()));
416        }
417
418        /* Find appropriate fetcher */
419        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    /* Get the configuration */
429    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}