spudplate
Template scaffolding compiler for spudlang .spud files
Loading...
Searching...
No Matches
spudpack.h
1#ifndef SPUDPLATE_SPUDPACK_H
2#define SPUDPLATE_SPUDPACK_H
3
4#include <cstddef>
5#include <cstdint>
6#include <filesystem>
7#include <optional>
8#include <stdexcept>
9#include <string>
10#include <string_view>
11#include <vector>
12
13namespace spudplate {
14
23 std::string path;
24 std::uint16_t mode;
25 std::vector<std::uint8_t> data;
26};
27
44 std::string name;
45 std::vector<std::uint8_t> bytes;
46 std::uint32_t version_tag{1};
47};
48
57struct Spudpack {
58 std::string source;
59 std::vector<std::uint8_t> program_bytes;
60 std::vector<SpudpackAsset> assets;
61 std::vector<SpudpackDep> deps;
62 std::uint8_t version{4};
63 std::uint32_t version_tag{1};
64};
65
72class SpudpackError : public std::runtime_error {
73 public:
75 SpudpackError(std::string message, std::optional<std::size_t> offset = std::nullopt);
77 std::optional<std::size_t> offset() const noexcept { return offset_; }
78
79 private:
80 std::optional<std::size_t> offset_;
81};
82
96std::vector<std::uint8_t> spudpack_encode(const Spudpack& pack);
97
111Spudpack spudpack_decode(const std::uint8_t* data, std::size_t size);
112
119void spudpack_write_file(const std::filesystem::path& path, const Spudpack& pack);
120
122Spudpack spudpack_read_file(const std::filesystem::path& path);
123
133std::string normalize_asset_path(std::string_view raw);
134
141bool is_normalized_asset_path(std::string_view path) noexcept;
142
150bool is_valid_dep_name(std::string_view name) noexcept;
151
161inline constexpr std::string_view kArchiveDir = ".archive";
162
170std::filesystem::path archive_path_for(const std::filesystem::path& install_root,
171 std::string_view name,
172 std::uint32_t version_tag);
173
174} // namespace spudplate
175
176#endif // SPUDPLATE_SPUDPACK_H
Raised on any encode or decode failure.
Definition spudpack.h:72
std::optional< std::size_t > offset() const noexcept
Byte offset where decoding failed, if known.
Definition spudpack.h:77
One bundled asset entry inside a spudpack.
Definition spudpack.h:22
std::uint16_t mode
POSIX mode bits, masked to 0o0777.
Definition spudpack.h:24
std::vector< std::uint8_t > data
Raw asset bytes; empty for an empty leaf directory.
Definition spudpack.h:25
std::string path
Normalised asset path inside the pack.
Definition spudpack.h:23
One bundled dependency inside a spudpack.
Definition spudpack.h:43
std::uint32_t version_tag
Monotonic install counter the dep carried when bundled. Defaults to 1 for v3 decodes.
Definition spudpack.h:46
std::string name
Bare include name, matching <name>.spp on the install root.
Definition spudpack.h:44
std::vector< std::uint8_t > bytes
Full byte stream of the bundled dep's spudpack.
Definition spudpack.h:45
A decoded spudpack - the source text, the opaque compiled program, every bundled asset,...
Definition spudpack.h:57
std::vector< std::uint8_t > program_bytes
Opaque serialised AST; decoded by the binary serializer.
Definition spudpack.h:59
std::uint8_t version
Spudpack format version that produced these bytes. Threaded into the binary serializer so trailing-op...
Definition spudpack.h:62
std::string source
Original .spud source text.
Definition spudpack.h:58
std::uint32_t version_tag
Monotonic install counter for this template. Bumped on each install that produces different content....
Definition spudpack.h:63
std::vector< SpudpackAsset > assets
Every bundled asset referenced by the program.
Definition spudpack.h:60
std::vector< SpudpackDep > deps
Every bundled dependency referenced by include statements.
Definition spudpack.h:61