use std::cell::{Ref, RefCell};
use cxx::UniquePtr;
#[allow(non_upper_case_globals, non_snake_case)]
pub mod RecordField {
pub const Package: &str = "Package";
pub const Source: &str = "Source";
pub const Version: &str = "Version";
pub const InstalledSize: &str = "Installed-Size";
pub const Homepage: &str = "Homepage";
pub const Essential: &str = "Essential";
pub const Maintainer: &str = "Maintainer";
pub const OriginalMaintainer: &str = "Original-Maintainer";
pub const Architecture: &str = "Architecture";
pub const Replaces: &str = "Replaces";
pub const Provides: &str = "Provides";
pub const PreDepends: &str = "Pre-Depends";
pub const Depends: &str = "Depends";
pub const Recommends: &str = "Recommends";
pub const Suggests: &str = "Suggests";
pub const Breaks: &str = "Breaks";
pub const Conflicts: &str = "Conflicts";
pub const Description: &str = "Description";
pub const DescriptionMD5: &str = "Description-md5";
pub const Tag: &str = "Tag";
pub const MultiArch: &str = "Multi-Arch";
pub const Section: &str = "Section";
pub const Priority: &str = "Priority";
pub const Filename: &str = "Filename";
pub const Size: &str = "Size";
pub const MD5sum: &str = "MD5sum";
pub const SHA256: &str = "SHA256";
}
pub struct PackageRecords {
pub(crate) ptr: UniquePtr<raw::PkgRecords>,
parser: RefCell<UniquePtr<raw::Parser>>,
index: RefCell<u64>,
}
impl PackageRecords {
pub fn new(ptr: UniquePtr<raw::PkgRecords>) -> PackageRecords {
PackageRecords {
ptr,
parser: RefCell::new(UniquePtr::null()),
index: RefCell::new(0),
}
}
fn replace_index(&self, index: u64) -> bool {
if self.index.borrow().eq(&index) {
return false;
}
self.index.replace(index);
true
}
fn parser(&self) -> Ref<'_, UniquePtr<raw::Parser>> {
if self.parser.borrow().is_null() {
panic!("You must call ver_lookup or desc_lookup first!")
}
self.parser.borrow()
}
pub fn ver_lookup(&self, file: &raw::VerFileIterator) -> &PackageRecords {
if self.replace_index(file.index()) {
unsafe { self.parser.replace(self.ptr.ver_lookup(file)) };
}
self
}
pub fn desc_lookup(&self, file: &raw::DescIterator) -> &PackageRecords {
if self.replace_index(file.index()) {
unsafe { self.parser.replace(self.ptr.desc_lookup(file)) };
}
self
}
pub fn short_desc(&self) -> Option<String> { self.parser().short_desc().ok() }
pub fn long_desc(&self) -> Option<String> { self.parser().long_desc().ok() }
pub fn filename(&self) -> String { self.parser().filename() }
pub fn get_field(&self, field: String) -> Option<String> { self.parser().get_field(field).ok() }
pub fn hash_find(&self, hash_type: String) -> Option<String> {
self.parser().hash_find(hash_type).ok()
}
}
type SourceParser<'a> = Ref<'a, UniquePtr<raw::SourceParser>>;
pub struct SourceRecords {
ptr: UniquePtr<raw::SourceRecords>,
parser: RefCell<UniquePtr<raw::SourceParser>>,
}
impl SourceRecords {
pub fn new(ptr: UniquePtr<raw::SourceRecords>) -> SourceRecords {
SourceRecords {
ptr,
parser: RefCell::new(UniquePtr::null()),
}
}
pub fn restart(&self) { self.ptr.restart() }
pub fn lookup(&self, name: String, src_only: bool) -> Option<SourceParser<'_>> {
unsafe {
self.parser.replace(self.ptr.find(name, src_only));
}
if self.parser.borrow().end() {
self.restart();
return None;
}
Some(self.parser.borrow())
}
}
#[cxx::bridge]
pub(crate) mod raw {
impl UniquePtr<IndexFile> {}
impl UniquePtr<SourceRecords> {}
unsafe extern "C++" {
include!("rust-apt/apt-pkg-c/records.h");
type PkgRecords;
type Parser;
type SourceRecords;
type SourceParser;
type IndexFile;
type VerFileIterator = crate::iterators::VerFileIterator;
type DescIterator = crate::iterators::DescIterator;
unsafe fn ver_lookup(self: &PkgRecords, ver_file: &VerFileIterator) -> UniquePtr<Parser>;
unsafe fn desc_lookup(self: &PkgRecords, desc_file: &DescIterator) -> UniquePtr<Parser>;
pub fn filename(self: &Parser) -> String;
pub fn long_desc(self: &Parser) -> Result<String>;
pub fn short_desc(self: &Parser) -> Result<String>;
pub fn get_field(self: &Parser, field: String) -> Result<String>;
pub fn hash_find(self: &Parser, hash_type: String) -> Result<String>;
pub fn archive_uri(self: &IndexFile, filename: &str) -> String;
pub fn is_trusted(self: &IndexFile) -> bool;
pub fn restart(self: &SourceRecords);
unsafe fn find(
self: &SourceRecords,
name: String,
src_only: bool,
) -> UniquePtr<SourceParser>;
fn as_str(self: &SourceParser) -> String;
fn package(self: &SourceParser) -> String;
fn version(self: &SourceParser) -> String;
fn maintainer(self: &SourceParser) -> String;
fn section(self: &SourceParser) -> String;
fn end(self: &SourceParser) -> bool;
}
}