This commit is contained in:
shibazaki 2025-10-03 21:14:20 +02:00
parent 5190262192
commit d28f1d0032
4 changed files with 74 additions and 2 deletions

View File

@ -0,0 +1,3 @@
pub mod packages;
pub mod pin;
pub mod sources;

View File

@ -0,0 +1,59 @@
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
use crate::structs::{AptSource, RepoType};
/// Loads Debian APT sources from a given file path (e.g., "/etc/apt/sources.list")
pub fn load_sources(path: &str) -> io::Result<Vec<AptSource>> {
let file = File::open(Path::new(path))?;
let reader = io::BufReader::new(file);
let mut sources = Vec::new();
let mut counter = 1;
for line in reader.lines() {
let line = line?;
let trimmed = line.trim();
// Skip empty lines and comments
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
let parts: Vec<&str> = trimmed.split_whitespace().collect();
if parts.len() < 3 {
eprintln!("Skipping malformed line: {}", trimmed);
continue;
}
// Determine repository type
let repo_type = match parts[0] {
"deb" => RepoType::Bin,
"deb-src" => RepoType::Src,
other => {
eprintln!("Unknown repo type: {}", other);
continue;
}
};
// parts[1] = URI
// parts[2] = distribution (bookworm, trixie, etc.)
// parts[3..] = components
let uri = parts[1].to_string();
let debian_version = parts[2].to_string();
let components = parts[3..].iter().map(|s| s.to_string()).collect();
sources.push(AptSource {
name: Some(format!("repo{}", counter)),
bin_repo: repo_type,
uri,
debian_version,
components,
});
counter += 1;
}
Ok(sources)
}

View File

@ -1,3 +1,12 @@
fn main() {
println!("Hello, world!");
mod loaders;
mod structs;
use crate::loaders::sources::load_sources;
fn main() -> std::io::Result<()> {
let sources = load_sources("/etc/apt/sources.list")?;
for source in sources {
println!("{:?}", source);
}
Ok(())
}

View File

@ -8,6 +8,7 @@ pub struct System {
pub arch: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RepoType {
Bin, // deb
Src, // deb-src