27 std::expected<std::string, FileError>
read(
const std::string& filepath)
29 const auto path = std::filesystem::path(filepath);
30 if (!std::filesystem::exists(path))
32 return std::unexpected(
FileError(
"read",
"File does not exist.", path));
35 if (!std::filesystem::is_regular_file(path))
37 return std::unexpected(
FileError(
"read",
"Path is an irregular file or a directory", path));
40 std::ifstream input {path, std::ifstream::in};
43 return std::unexpected(
FileError(
"read",
"Permission denied.", path));
46 std::string buffer(std::filesystem::file_size(path),
'\0');
47 input.read(buffer.data(), buffer.size());
50 return std::unexpected(
FileError(
"read",
"Failed to read file.", path));