takane
Validators for ArtifactDB file formats
Loading...
Searching...
No Matches
rds_file.hpp
Go to the documentation of this file.
1#ifndef TAKANE_RDS_FILE_HPP
2#define TAKANE_RDS_FILE_HPP
3
4#include "ritsuko/ritsuko.hpp"
5
6#include <filesystem>
7#include <stdexcept>
8#include <exception>
9#include <string>
10
11#include "utils_files.hpp"
12
18namespace takane {
19
28inline void validate_rds_file(const std::filesystem::path& path, const ObjectMetadata& metadata, const Options& options) {
29 const std::string type_name = "rds_file"; // use a separate variable to avoid dangling reference warnings from GCC.
30
31 try {
32 const auto& rdsmap = extract_json_object(metadata.other, type_name);
33 try {
34 const std::string& vstring = extract_json_version_string(rdsmap);
35 auto version = ritsuko::parse_version_string(vstring.c_str(), vstring.size(), /* skip_patch = */ true);
36 if (version.major != 1) {
37 throw std::runtime_error("unsupported version string '" + vstring + "'");
38 }
39 } catch (...) {
40 std::throw_with_nested(std::runtime_error("failed to validate 'version'"));
41 }
42 } catch (...) {
43 std::throw_with_nested(std::runtime_error("failed to validate '" + type_name + "' in the object metadata"));
44 }
45
46 auto fpath = path / "file.rds";
47 try {
48 // Check magic numbers.
49 check_gzip_file_signature(fpath);
50 check_gunzipped_file_signature(fpath, "X\n", 2, "an RDS file");
51 } catch (...) {
52 std::throw_with_nested(std::runtime_error("failed to validate '" + fpath.filename().string() + "'"));
53 }
54
55 if (options.rds_file_strict_check) {
56 options.rds_file_strict_check(path, metadata, options);
57 }
58}
59
60}
61
62#endif
takane validation functions.
Definition _derived_from.hpp:15
void validate_rds_file(const std::filesystem::path &path, const ObjectMetadata &metadata, const Options &options)
Definition rds_file.hpp:28
Object metadata, including the type and other fields.
Definition utils_public.hpp:29
std::unordered_map< std::string, std::shared_ptr< millijson::Base > > other
Definition utils_public.hpp:38
Validation options.
Definition utils_public.hpp:99
std::function< void(const std::filesystem::path &, const ObjectMetadata &, const Options &)> rds_file_strict_check
Definition utils_public.hpp:235