aboutsummaryrefslogtreecommitdiffstats
path: root/liblfvm/disk_config.ccm
blob: 975f6b9257ed8ddf78f6dc7972142f1fecc49514 (plain) (blame)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * This source code is released into the public domain.
 */

module;

#include <expected>
#include <filesystem>
#include <string>

export module liblfvm:disk_config;

import nihil;
import nihil.ucl;

import :context;

namespace lfvm {

/*
 * Represents a disk which can be attached to the VM.
 */
export struct disk_config {
	/*
	 * The name of this disk.  The name is arbitrary, it doesn't need
	 * to match the filename.
	 */
	[[nodiscard]] auto name(this disk_config const &) -> std::string_view;

	/*
	 * The on-disk filename of this disk.  This is verified to exist
	 * at creation time, but it may not exist later, e.g. if the user
	 * deletes the file.
	 */
	[[nodiscard]] auto path(this disk_config const &)
		-> std::filesystem::path const &;

	/*
	 * Check if the disk's backing file exists.
	 */
	[[nodiscard]] auto exists(this disk_config const &) -> bool;

	/*
	 * Serialize a disk to a UCL string.
	 */
	[[nodiscard]] auto serialize(this disk_config const &)
		-> std::expected<std::string, nihil::error>;

	/*
	 * Deserialize a UCL string into a disk.
	 */
	[[nodiscard]] static auto deserialize(std::string_view)
		-> std::expected<disk_config, nihil::error>;

private:
	friend auto make_disk_config(std::string_view, std::string_view)
		-> std::expected<disk_config, nihil::error>;

	disk_config(std::string_view name, std::filesystem::path path);

	std::string		m_name;
	std::filesystem::path	m_path;
};

/*
 * Create a new disk from a name and path.
 */
export [[nodiscard]] auto make_disk_config(std::string_view name,
					   std::string_view path)
	-> std::expected<disk_config, nihil::error>;

/*
 * Load an existing disk.
 */
export [[nodiscard]] auto disk_load(context const &, std::string_view name)
	-> std::expected<disk_config, nihil::error>;

/*
 * Save a new disk.
 */
export [[nodiscard]] auto disk_create(context const &, disk_config const &)
	-> std::expected<void, nihil::error>;

/*
 * Save an existing disk.
 */
export [[nodiscard]] auto disk_save(context const &, disk_config const &)
	-> std::expected<void, nihil::error>;

/*
 * List existing disks.
 */
export [[nodiscard]] auto disk_list(context const &)
	-> std::expected<std::vector<std::string>, nihil::error>;

} // namespace lfvm