Echo Writes Code

fs.hpp

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
#ifndef CRUCIBLE_WINDOWS_FS_HPP
#define CRUCIBLE_WINDOWS_FS_HPP

#include "crucible/core/constant_error.inl"
#include "crucible/core/error_chain.inl"
#include "crucible/core/none.hpp"
#include "crucible/core/result.inl"
#include "crucible/windows/windows_error.inl"

#include <cstddef>
#include <string>
#include <vector>

namespace crucible::windows::fs {
  struct CreateDirectoryWFailed final : windows_error::WindowsError<CreateDirectoryWFailed> {
    static constexpr char const function[] = "CreateDirectoryW";
  };

  struct CreateFileWFailed final : windows_error::WindowsError<CreateFileWFailed> {
    static constexpr char const function[] = "CreateFileW";
  };

  struct GetFileAttributesWFailed final : windows_error::WindowsError<GetFileAttributesWFailed> {
    static constexpr char const function[] = "GetFileAttributesW";
  };

  struct GetFileSizeExFailed final : windows_error::WindowsError<GetFileSizeExFailed> {
    static constexpr char const function[] = "GetFileSizeEx";
  };

  struct FindFirstFileWFailed final : windows_error::WindowsError<FindFirstFileWFailed> {
    static constexpr char const function[] = "FindFirstFileW";
  };

  struct FindNextFileWFailed final : windows_error::WindowsError<FindNextFileWFailed> {
    static constexpr char const function[] = "FindNextFileW";
  };

  struct ReadFileFailed final : windows_error::WindowsError<ReadFileFailed> {
    static constexpr char const function[] = "ReadFile";
  };

  struct RemoveDirectoryWFailed final : windows_error::WindowsError<RemoveDirectoryWFailed> {
    static constexpr char const function[] = "RemoveDirectoryW";
  };

  struct DeleteFileWFailed final : windows_error::WindowsError<DeleteFileWFailed> {
    static constexpr char const function[] = "DeleteFileW";
  };

  struct WriteFileFailed final : windows_error::WindowsError<WriteFileFailed> {
    static constexpr char const function[] = "WriteFile";
  };

  struct NegativeFileSize final : core::constant_error::ConstantError<NegativeFileSize> {
    static constexpr char const message[] = "Negative file size reported by the operating system";
  };

  struct FSError final : core::error_chain::ErrorChain<FSError, CreateDirectoryWFailed, CreateFileWFailed, GetFileAttributesWFailed, GetFileSizeExFailed, FindFirstFileWFailed, FindNextFileWFailed, ReadFileFailed, RemoveDirectoryWFailed, DeleteFileWFailed, WriteFileFailed, NegativeFileSize> {
    using ErrorChain::ErrorChain;

    static constexpr char const message[] = "Filesystem error";
  };

  template<typename T>
  using FSResult = core::result::Result<FSError, T>;

  [[nodiscard]]
  auto create_directory(std::wstring const &path) -> FSResult<core::none::None>;

  [[nodiscard]]
  auto create_file(std::wstring const &path, std::vector<std::byte> const &contents) -> FSResult<core::none::None>;

  [[nodiscard]]
  auto delete_directory(std::wstring const &path) -> FSResult<core::none::None>;

  [[nodiscard]]
  auto delete_file(std::wstring const &path) -> FSResult<core::none::None>;

  [[nodiscard]]
  auto is_directory(std::wstring const &path) -> FSResult<bool>;

  [[nodiscard]]
  auto is_file(std::wstring const &path) -> FSResult<bool>;

  [[nodiscard]]
  auto read_directory_list(std::wstring const &path) -> FSResult<std::vector<std::wstring>>;

  [[nodiscard]]
  auto read_file_data(std::wstring const &path) -> FSResult<std::vector<std::byte>>;

  [[nodiscard]]
  auto read_file_size(std::wstring const &path) -> FSResult<std::size_t>;
}

#endif // CRUCIBLE_WINDOWS_FS_HPP