Skip to main content

bindings_generat/sources/
mod.rs

1pub mod archives;
2
3use anyhow::Result;
4use std::path::PathBuf;
5
6/// Prepare the source for processing
7/// - If it's a URL, download it
8/// - If it's an archive, extract it
9/// - If it's a directory, use it as-is
10pub fn prepare_source(source: &str) -> Result<PreparedSource> {
11    // Check if it's a URL
12    if source.starts_with("http://") || source.starts_with("https://") {
13        return archives::download_and_extract(source);
14    }
15
16    let path = PathBuf::from(source);
17
18    // Check if it's a file (archive) or directory
19    if path.is_file() {
20        return archives::extract_archive(&path);
21    }
22
23    // It's a directory - use as-is
24    if path.is_dir() {
25        return Ok(PreparedSource {
26            path,
27            is_temporary: false,
28        });
29    }
30
31    anyhow::bail!("Source path does not exist: {}", source);
32}
33
34/// Result of source preparation
35pub struct PreparedSource {
36    /// Path to the source directory
37    pub path: PathBuf,
38
39    /// Whether this is a temporary directory that should be cleaned up
40    pub is_temporary: bool,
41}
42
43impl PreparedSource {
44    /// Get the path to the source directory
45    pub fn path(&self) -> &PathBuf {
46        &self.path
47    }
48}
49
50impl Drop for PreparedSource {
51    fn drop(&mut self) {
52        if self.is_temporary {
53            // Clean up temporary directory
54            if let Err(e) = std::fs::remove_dir_all(&self.path) {
55                tracing::warn!(
56                    "Failed to clean up temporary directory {}: {}",
57                    self.path.display(),
58                    e
59                );
60            }
61        }
62    }
63}