7个稳定版本

1.0.6 2023年4月24日
1.0.3 2023年4月23日
1.0.2 2023年4月19日
1.0.0 2023年2月12日

#578解析器实现

每月 49 次下载
用于 backseat-signed

MIT 许可证

42KB
1K SLoC

APT解析器

寻找TypeScript版本? 它已被移动到这里...

apt-parser 是一个用于解析 APT 列表文件的库。
APT仓库通常由一个Release文件、一个Packages文件和压缩的二进制包组成。
该库能够解析这些文件,并将它们作为 serde 序列化的结构体返回。

安装

请确保您有一个现代版本的Rust (1.56+),并使用 rustup
然后,将以下内容添加到您的 Cargo.toml

[dependencies]
# You can also use the latest version
apt-parser = "1.0.0"

Release文件解析

Release文件是APT仓库的主要入口点。
Release 结构体对 Release 文件 中所有文档化的字段具有严格的数据类型。
如果您需要访问未定义的字段,可以使用 get 方法。
以下是一个简单的示例

use apt_parser::Release;
use surf::get;

let data = get("http://archive.ubuntu.com/ubuntu/dists/jammy/Release")
    .await?
    .body_string()
    .await?;

let release = match Release::from(data) {
    Ok(release) => release,
    Err(err) => panic!("Failed to parse Release file: {}", err),
}

assert_eq!(release.origin, "Ubuntu");
assert_eq!(release.version, "22.04");
assert_eq!(release.get("InvalidKey"), None);
struct Release {
    architectures: Vec<String>, // => Architectures
    no_support_for_architecture_all: Option<bool>, // => No-Support-For-Architecture-All
    description: Option<String>, // => Description
    origin: Option<String>, // => Origin
    label: Option<String>, // => Label
    suite: Option<String>, // => Suite
    version: Option<String>, // => Version
    codename: Option<String>, // => Codename
    date: Option<String>, // => Date
    valid_until: Option<String>, // => Valid-Until
    components: Vec<String>, // => Components
    md5sum: Option<Vec<ReleaseHash>>, // => MD5Sum
    sha1sum: Option<Vec<ReleaseHash>>, // => SHA1
    sha256sum: Option<Vec<ReleaseHash>>, // => SHA256
    sha512sum: Option<Vec<ReleaseHash>>, // => SHA512
    not_automatic: Option<bool>, // => NotAutomatic
    but_automatic_upgrades: Option<bool>, // => ButAutomaticUpgrades
    acquire_by_hash: Option<bool>, // => Acquire-By-Hash
    signed_by: Option<String>, // => Signed-By
    packages_require_authorization: Option<bool>, // => Packages-Require-Authorization

    fn from(data: &str) -> Result<Self, APTError>; // => Parse a Release file
    fn get(&self, key: &str) -> Option<&str>; // => Retrieve a raw field value
}

// A struct for holding the hash information for a Release file
struct ReleaseHash {
    filename: String,
    hash: String,
    size: u64,
}

Control文件解析

Control文件用于描述二进制包的内容。
Control 结构体对 control 文件 中所有文档化的字段具有严格的数据类型。
如果您需要访问未定义的字段,可以使用 get 方法。
以下是一个简单的示例

use apt_parser::Control;

let data = "
Package: com.amywhile.signalreborn
Architecture: iphoneos-arm
Description: Visualise your nearby cell towers
Depends: firmware (>= 12.2) | org.swift.libswift
Maintainer: Amy While <[email protected]>
Section: Applications
Version: 2.2.1-2
Installed-Size: 1536
Custom-Key: cool-value
";

let control = match Control::from(data) {
    Ok(control) => control,
    Err(err) => panic!("Failed to parse Control file: {}", err),
}

assert_eq!(control.version, "2.2.1-2");
assert_eq!(control.package, "com.amywhile.signalreborn");
assert_eq!(control.get("Custom-Key"), Some("cool-value"));
assert_eq!(control.get("Invalid-Key"), None);
struct Control {
    package: String, // => Package
    source: Option<String>, // => Source
    version: String, // => Version
    section: Option<String>, // => Section
    priority: Option<String>, // => Priority
    architecture: String, // => Architecture
    is_essential: Option<bool>, // => Essential
    depends: Option<Vec<String>>, // => Depends
    pre_depends: Option<Vec<String>>, // => Pre-Depends
    recommends: Option<Vec<String>>, // => Recommends
    suggests: Option<Vec<String>>, // => Suggests
    replaces: Option<Vec<String>>, // => Replaces
    enhances: Option<Vec<String>>, // => Enhances
    breaks: Option<Vec<String>>, // => Breaks
    conflicts: Option<Vec<String>>, // => Conflicts
    installed_size: Option<i64>, // => Installed-Size
    maintainer: Option<String>, // => Maintainer
    description: Option<String>, // => Description
    homepage: Option<String>, // => Homepage
    built_using: Option<String>, // => Built-Using
    package_type: Option<String>, // => Package-Type
    tags: Option<Vec<String>>, // => Tags

    fn from(data: &str) -> Result<Self, APTError>; // => Parse a Control file
    fn get(&self, key: &str) -> Option<&str>; // => Retrieve a raw field value
}

Packages文件解析

Packages文件用于描述仓库的内容。
Packages 结构体实现了一个迭代器,并具有访问包的方法。
Package 结构体对 Packages 文件 中所有文档化的字段具有严格的数据类型。
如果您需要访问未定义的字段,可以使用 get 方法。
以下是一个简单的示例

use apt_parser::Packages;
use surf::get;

let data = get("https://repo.chariz.com/Packages")
    .await?
    .body_string()
    .await?;

let packages = match Packages::from(&data) {
    Ok(packages) => packages,
    Err(err) => panic!("Failed to parse Packages file: {}", err),
}

assert_eq!(packages.len(), 419);

for package in packages {
    println!("{}: {}", package.package, package.version);
}
struct Packages {
    packages: Vec<Package>,

    fn from(data: &str) -> Result<Self, APTError>; // => Parse a Packages file
    fn len(&self) -> usize; // => Get the number of packages
}

impl Iterator for Packages;
impl Index for Packages;

struct Package {
    package: String, // => Package
    source: Option<String>, // => Source
    version: String, // => Version
    section: Option<String>, // => Section
    priority: Option<String>, // => Priority
    architecture: String, // => Architecture
    is_essential: Option<bool>, // => Essential
    depends: Option<Vec<String>>, // => Depends
    pre_depends: Option<Vec<String>>, // => Pre-Depends
    recommends: Option<Vec<String>>, // => Recommends
    suggests: Option<Vec<String>>, // => Suggests
    replaces: Option<Vec<String>>, // => Replaces
    enhances: Option<Vec<String>>, // => Enhances
    breaks: Option<Vec<String>>, // => Breaks
    conflicts: Option<Vec<String>>, // => Conflicts
    installed_size: Option<i64>, // => Installed-Size
    maintainer: Option<String>, // => Maintainer
    description: Option<String>, // => Description
    homepage: Option<String>, // => Homepage
    built_using: Option<String>, // => Built-Using
    package_type: Option<String>, // => Package-Type
    tags: Option<Vec<String>>, // => Tags
    filename: String, // => Filename
    size: i64, // => Size
    md5sum: Option<String>, // => MD5sum
    sha1sum: Option<String>, // => SHA1
    sha256sum: Option<String>, // => SHA256
    sha512sum: Option<String>, // => SHA512
    description_md5sum: Option<String>, // => Description-md5

    fn get(&self, key: &str) -> Option<&str>; // => Retrieve a raw field value
}

版权所有 (c) 2023 Aarnav Tale

依赖项

~3.5–4.5MB
~79K SLoC