summaryrefslogtreecommitdiff
path: root/src/line.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-09-22 23:38:21 +0200
committerJoel Klinghed <the_jk@spawned.biz>2025-09-22 23:38:21 +0200
commitce271f82f16ee89a18e7bfc9ed8eab7cbd6f37bc (patch)
tree3e568faf83ae750aa244cca87b55951c7401ef03 /src/line.cc
parent50348284f5d82ccfd65b0c803ba0ba895912ceff (diff)
Change io::Reader and company to return ReadError::Eof instead of 0.
It's debatable if Eof should be considered an error or not. But it is pretty clear it generally is a special response that needs special handling, so easier to keep with the unexpected lot. Also keeps better at higher abstraction levels, such as the line reader.
Diffstat (limited to 'src/line.cc')
-rw-r--r--src/line.cc23
1 files changed, 7 insertions, 16 deletions
diff --git a/src/line.cc b/src/line.cc
index 8ee7134..23370fc 100644
--- a/src/line.cc
+++ b/src/line.cc
@@ -29,7 +29,7 @@ class ReaderImpl : public Reader {
search_(rptr_),
end_(buffer_.get() + check::add(max_len, static_cast<size_t>(2))) {}
- [[nodiscard]] std::expected<std::string_view, ReadError> read() override {
+ [[nodiscard]] std::expected<std::string_view, io::ReadError> read() override {
while (true) {
search_ = std::find_first_of(search_, wptr_, kLineTerminators,
kLineTerminators + 2);
@@ -45,12 +45,11 @@ class ReaderImpl : public Reader {
if (search_ + 1 == wptr_) {
make_space_if_needed();
auto got = fill();
- if (got.has_value()) {
- if (got.value() == 0) {
+ if (!got.has_value()) {
+ if (got.error() == io::ReadError::Eof) {
return line(search_ - rptr_, 1);
}
- } else {
- return std::unexpected(ReadError(got.error()));
+ return std::unexpected(got.error());
}
}
if (search_[1] == '\n') {
@@ -67,15 +66,11 @@ class ReaderImpl : public Reader {
make_space_if_needed();
auto got = fill();
- if (got.has_value()) {
- if (got.value() == 0) {
- if (rptr_ == wptr_) {
- return std::unexpected(ReadError());
- }
+ if (!got.has_value()) {
+ if (got.error() == io::ReadError::Eof && rptr_ != wptr_) {
return line(wptr_ - rptr_, 0);
}
- } else {
- return std::unexpected(ReadError(got.error()));
+ return std::unexpected(got.error());
}
}
}
@@ -124,10 +119,6 @@ class ReaderImpl : public Reader {
} // namespace
-ReadError::ReadError() : eof(true) {}
-
-ReadError::ReadError(io::ReadError error) : eof(false), io_error(error) {}
-
std::unique_ptr<Reader> open(std::unique_ptr<io::Reader> reader,
size_t max_len) {
return std::make_unique<ReaderImpl>(std::move(reader), max_len);