Refactor coral file API

This commit is contained in:
kayomn 2023-03-02 20:00:46 +00:00
parent 356aa6729b
commit e5e4f11004

View File

@ -3,115 +3,6 @@ export module coral.files;
import coral;
export namespace coral {
/**
* Platform-generalized identifier for a resource in a [fs].
*/
struct path {
/**
* Errors that may occur during a path joining operation.
*
* [join_error::overflow] signals that the given path join exceeds the maximum valid length of a path.
*/
enum class join_error {
overflow,
};
/**
* Maximum path length.
*/
static usize const max = u8_max;
/**
* Common path component separator.
*/
static char const seperator = '/';
constexpr path() {
this->buffer[max] = max;
}
template<usize text_size> constexpr path(char const(&text)[text_size]) {
static_assert(text_size <= max);
for (usize i = 0; i < text_size; i += 1) this->buffer[i] = text[i];
this->buffer[max] = max - text_size;
}
/**
* Returns a weak reference to the [path] as a [slice].
*/
constexpr slice<char const> as_slice() const {
return {this->buffer, this->filled()};
}
/**
* Returns the base pointer of the path name.
*
* *Note*: the returned buffer pointer is guaranteed to end with a zero terminator.
*/
char const * begin() const {
return reinterpret_cast<char const *>(this->buffer);
}
/**
* Compares the path to `that`, returning the difference between the two paths or `0` if they are identical.
*/
constexpr size compare(path const & that) const {
return coral::compare(this->as_slice().as_bytes(), that.as_slice().as_bytes());
}
/**
* Returns the tail pointer of the path name.
*/
char const * end() const {
return this->buffer + this->filled();
}
/**
* Tests the path against `that` for equality, returning `true` if they are identical, otherwise `false`.
*/
constexpr bool equals(path const & that) const {
return coral::equals(this->as_slice().as_bytes(), that.as_slice().as_bytes());
}
/**
* Returns the number of characters composing the path.
*/
constexpr usize filled() const {
return max - this->buffer[max];
}
/**
* Returns the path hash code.
*
* *Note:* the returned hash code is not guaranteed to be unique.
*/
constexpr u64 hash() const {
return coral::hash(this->as_slice().as_bytes());
}
/**
* Attempts to create a new path composed from the current path joined with `text`, returning it or a
* [join_error] if it failed.
*/
constexpr expected<path, join_error> joined(slice<char const> const & text) const {
if (text.length > this->buffer[max]) return join_error::overflow;
path joined_path = *this;
for (char const c : text) {
joined_path.buffer[joined_path.filled()] = c;
joined_path.buffer[max] -= 1;
}
return joined_path;
}
private:
char buffer[max + 1]{0};
};
/**
* [reader] that has a known range of data and may attempt to traverse it freely.
*/
@ -130,21 +21,21 @@ export namespace coral {
};
/**
* Enumerator a file tree recursively, returning the absolute path of each file within.
* Enumerates a file tree recursively, returning the absolute path of each file within.
*/
struct file_walker {
struct file_walker : public enumerator<expected<slice<char const>, io_error>> {
virtual ~file_walker() {}
/**
* Returns `true` if there are paths pending enumeration, otherwise `false`.
* Returns the last-enumerated path as a sequence of `char`s or an [io_error] if the last enumeration did not
* successfully complete.
*/
virtual bool has_next() = 0;
virtual expected<slice<char const>, io_error> value() = 0;
/**
* Attempts to enumerate over the next absolute [path] in the file tree, returning it or an [io_error] if the
* operation failed for whatever reason.
* See [enumerator::enumerate].
*/
virtual expected<path, io_error> next() = 0;
virtual bool enumerate() = 0;
};
/**
@ -167,8 +58,8 @@ export namespace coral {
/**
* Platform-generalized file system interface.
*/
struct fs {
virtual ~fs() {};
struct file_system {
virtual ~file_system() {};
/**
* Attempts to read the file in `target_path`, calling `then` if it was successfully opened for reading and
@ -176,7 +67,7 @@ export namespace coral {
*
* See [file_reader] for more information on how to read from the file.
*/
virtual void read_file(path const & target_path, closure<void(file_reader &)> const & then) {}
virtual void read_file(slice<char const> const & target_path, closure<void(file_reader &)> const & then) {}
/**
* Attempts to walk the file tree from `target_path`, calling `then` if it was successfully opened for walking
@ -184,7 +75,7 @@ export namespace coral {
*
* See [file_walker] for more information on how to traverse the file tree.
*/
virtual void walk_files(path const & target_path, closure<void(file_walker &)> const & then) {}
virtual void walk_files(slice<char const> const & target_path, closure<void(file_walker &)> const & then) {}
/**
* Attempts to write a file in the file system located at `target_path`, calling `then` if it was successfully
@ -194,6 +85,6 @@ export namespace coral {
*
* *Note*: Any file already existing at `target_path` will be overwritten to create a new file for writing.
*/
virtual void write_file(path const & target_path, closure<void(file_writer &)> const & then) {}
virtual void write_file(slice<char const> const & target_path, closure<void(file_writer &)> const & then) {}
};
}