Files
clice/include/Async/FileSystem.h
2025-02-03 01:26:37 +08:00

88 lines
2.3 KiB
C++

#pragma once
#include <chrono>
#include "libuv.h"
#include "Coroutine.h"
#include "Support/JSON.h"
#include "Support/Enum.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/FunctionExtras.h"
namespace clice::async {
struct None {};
template <typename T>
using Result = std::conditional_t<std::is_void_v<T>,
std::expected<None, std::error_code>,
std::expected<T, std::error_code>>;
template <typename T>
using AsyncResult = std::conditional_t<std::is_void_v<T>,
Task<std::expected<None, std::error_code>>,
Task<std::expected<T, std::error_code>>>;
namespace fs {
using handle = uv_file;
struct Mode : refl::Enum<Mode, true> {
enum Kind {
/// Open the file for reading.
Read = 0,
/// Open the file for writing.
Write,
/// Open the file for reading and writing.
ReadWrite,
/// If the file does not exist, create it.
Create,
/// If the file exists, append the data to the end of the file.
Append,
/// If the file exists, truncate the file to zero length.
Truncate,
/// If the file exists, fail the open.
Exclusive,
};
using Enum::Enum;
};
/// Open the file asynchronously.
[[nodiscard]] AsyncResult<handle> open(std::string path, Mode mode);
/// Close the file asynchronously.
[[nodiscard]] AsyncResult<void> close(handle file);
/// Read the file asynchronously, make sure the buffer is valid until the task is done.
[[nodiscard]] AsyncResult<ssize_t> read(handle file, char* buffer, std::size_t size);
[[nodiscard]] AsyncResult<std::string> read(std::string path, Mode mode = Mode::Read);
/// Write the file asynchronously, make sure the buffer is valid until the task is done.
[[nodiscard]] AsyncResult<void> write(handle file, char* buffer, std::size_t size);
[[nodiscard]] AsyncResult<void> write(std::string path,
char* buffer,
std::size_t size,
Mode mode = Mode(Mode::Write, Mode::Create, Mode::Truncate));
struct Stats {
std::chrono::milliseconds mtime;
};
AsyncResult<Stats> stat(std::string path);
} // namespace fs
} // namespace clice::async