File: records.h

package info (click to toggle)
rust-rust-apt 0.7.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 524 kB
  • sloc: cpp: 773; makefile: 8
file content (75 lines) | stat: -rw-r--r-- 2,537 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include <apt-pkg/cachefile.h>
#include <apt-pkg/indexfile.h>
#include <apt-pkg/pkgcache.h>
#include <apt-pkg/pkgrecords.h>
#include <apt-pkg/pkgsystem.h>
#include <apt-pkg/sourcelist.h>
#include <memory>
#include "rust/cxx.h"

/// Package Record Management:
struct Records {
	pkgRecords mutable records;
	pkgRecords::Parser mutable* parser;
	u_int64_t mutable last;

	inline bool already_has(u_int64_t index) const {
		if (last == index) { return true; }
		last = index;
		return false;
	}

	/// Moves the Records into the correct place.
	inline void ver_file_lookup(const VersionFile& ver_file) const {
		if (this->already_has(ver_file.index())) { return; }
		this->parser = &records.Lookup(*ver_file.ptr);
	}

	/// Moves the Records into the correct place.
	inline void desc_file_lookup(const DescriptionFile& desc_file) const {
		if (this->already_has(desc_file.index())) { return; }
		this->parser = &records.Lookup(*desc_file.ptr);
	}

	/// Return the URI for a version as determined by it's package file.
	/// A version could have multiple package files and multiple URIs.
	inline rust::string ver_uri(const PackageFile& pkg_file) const {
		if (!pkg_file.index_file) {
			throw std::runtime_error("You have to run 'cache.find_index()' first!");
		}
		if (!parser) {
			throw std::runtime_error(
				"You have to run 'cache.ver_lookup()' or 'desc_lookup()' first!"
			);
		}
		return (*pkg_file.index_file)->ArchiveURI(parser->FileName());
	}

	/// Return the translated long description of a Package.
	inline rust::string long_desc() const { return handle_string(parser->LongDesc()); }

	/// Return the translated short description of a Package.
	inline rust::string short_desc() const { return handle_string(parser->ShortDesc()); }

	/// Return the Source package version string.
	inline rust::string get_field(rust::string field) const {
		return handle_string(parser->RecordField(field.c_str()));
	}

	/// Find the hash of a Version. Returns Result if there is no hash.
	inline rust::string hash_find(rust::string hash_type) const {
		auto hashes = parser->Hashes();
		auto hash = hashes.find(hash_type.c_str());
		if (hash == NULL) { throw std::runtime_error("Hash Not Found"); }
		return handle_string(hash->HashValue());
	}

	Records(const std::unique_ptr<pkgCacheFile>& cache)
		: records(*safe_get_pkg_cache(cache.get())), parser(0), last(0){};

	/// UniquePtr Constructor
	static std::unique_ptr<Records> Unique(const std::unique_ptr<pkgCacheFile>& cache) {
		return std::make_unique<Records>(cache);
	};
};