File: records.h

package info (click to toggle)
rust-rust-apt 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 620 kB
  • sloc: cpp: 703; makefile: 8
file content (64 lines) | stat: -rw-r--r-- 2,040 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
#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"

#include "package.h"
#include "types.h"

struct IndexFile {
	pkgIndexFile* ptr;

	String archive_uri(str filename) const { return ptr->ArchiveURI(std::string(filename)); }
	bool is_trusted() const { return ptr->IsTrusted(); }

	IndexFile(pkgIndexFile* file) : ptr(file){};
};

struct Parser {
	pkgRecords::Parser& ptr;

	String short_desc() const { return handle_string(ptr.ShortDesc()); }
	String long_desc() const { return handle_string(ptr.LongDesc()); }
	String filename() const { return ptr.FileName(); }

	// TODO: Maybe look into this more if there is time. I was trying to save an allocation
	// ptr.RecordField(field.begin())
	// This will work with String.as_str()
	// cache.records().get_field(&"Maintainer".to_string())
	// This will not work with just "Maintainer" string literal

	/// Return the Source package version String.
	String get_field(String field) const { return handle_string(ptr.RecordField(field.c_str())); }

	// TODO: Lets Go Ahead and Bind HashStrings while we're here ffs
	/// Find the hash of a Version. Returns Result if there is no hash.
	String hash_find(String hash_type) const {
		auto hashes = ptr.Hashes();
		auto hash = hashes.find(hash_type.c_str());
		if (hash == NULL) { throw std::runtime_error("Hash Not Found"); }
		return handle_string(hash->HashValue());
	}

	Parser(pkgRecords::Parser& parser) : ptr(parser){};
};

struct PkgRecords {
	pkgRecords mutable records;

	UniquePtr<Parser> ver_lookup(const VerFileIterator& file) const {
		return std::make_unique<Parser>(records.Lookup(file));
	}

	/// Moves the Records into the correct place.
	UniquePtr<Parser> desc_lookup(const DescIterator& desc) const {
		return std::make_unique<Parser>(records.Lookup(desc.FileList()));
	}

	PkgRecords(pkgCacheFile* cache) : records(*cache->GetPkgCache()){};
};