Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
eoan-ermine committed Mar 22, 2024
1 parent 500ba72 commit bf11c8a
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions tftp_common/details/parsers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,26 @@
namespace tftp_common::packets {

/// The result of parsing a single packet
template <typename T>
struct ParseResult {
template <typename T> struct ParseResult {
T Packet;
std::size_t BytesRead;
};

/// Return type of `parse` functions
template <typename T>
struct ParseReturn : public std::variant<ParseResult<T>, std::nullopt_t> {
template <typename T> struct ParseReturn : public std::variant<ParseResult<T>, std::nullopt_t> {
using base = std::variant<ParseResult<T>, std::nullopt_t>;
using base::base;

ParseResult<T> get() const noexcept {
return std::get<ParseResult<T>>(*this);
}
ParseResult<T> get() const noexcept { return std::get<ParseResult<T>>(*this); }

bool isSuccess() const noexcept {
return !std::holds_alternative<std::nullopt_t>(*this);
}
bool isSuccess() const noexcept { return !std::holds_alternative<std::nullopt_t>(*this); }
};

template <typename T>
struct Parser {
template <typename T> struct Parser {
using PacketType = T;
};

template <>
struct Parser<Request> {
template <> struct Parser<Request> {
/// Parse read/write request packet from buffer converting all fields to host byte order
/// @param[Buffer] Assumptions: \p Buffer is not a nullptr, it's size is greater or equal than \p Len
/// @param[Len] Assumptions: \p Len is greater than zero
Expand Down Expand Up @@ -84,7 +76,8 @@ struct Parser<Request> {
case 3:
if (Byte == 0u) {
if (Idx == Len - 1) {
return ParseResult<Request>{Request{(types::Type) Type_, std::move(Filename), std::move(Mode)}, BytesRead};
return ParseResult<Request>{Request{(types::Type)Type_, std::move(Filename), std::move(Mode)},
BytesRead};
}
Step++;
} else {
Expand All @@ -106,7 +99,9 @@ struct Parser<Request> {
OptionsValues.push_back(std::move(Value));

if (Idx == Len - 1) {
return ParseResult<Request>{Request{(types::Type) Type_, std::move(Filename), std::move(Mode), std::move(OptionsNames), std::move(OptionsValues)}, BytesRead};
return ParseResult<Request>{Request{(types::Type)Type_, std::move(Filename), std::move(Mode),
std::move(OptionsNames), std::move(OptionsValues)},
BytesRead};
}
Step--;
} else {
Expand All @@ -121,8 +116,7 @@ struct Parser<Request> {
}
};

template <>
struct Parser<Data> {
template <> struct Parser<Data> {
/// Parse data packet from buffer converting all fields to host byte order
/// @param[Buffer] Assumptions: \p Buffer is not a nullptr, it's size is greater or equal than \p Len
/// @param[Len] Assumptions: \p Len is greater than zero
Expand All @@ -135,7 +129,6 @@ struct Parser<Data> {
std::uint16_t Block;
std::vector<std::uint8_t> DataBuffer;


std::size_t Step = 0;
std::size_t BytesRead = 0;
for (std::size_t Idx = 0; Idx != Len; ++Idx) {
Expand Down Expand Up @@ -184,8 +177,7 @@ struct Parser<Data> {
}
};

template <>
struct Parser<Acknowledgment> {
template <> struct Parser<Acknowledgment> {
/// Parse acknowledgment packet from buffer converting all fields to host byte order
/// @param[Buffer] Assumptions: \p Buffer is not a nullptr, it's size is greater or equal than \p Len
/// @param[Len] Assumptions: \p Len is greater than zero
Expand Down Expand Up @@ -235,8 +227,7 @@ struct Parser<Acknowledgment> {
}
};

template <>
struct Parser<Error> {
template <> struct Parser<Error> {
/// Parse error packet from buffer converting all fields to host byte order
/// @param[Buffer] Assumptions: \p Buffer is not a nullptr, it's size is greater or equal than \p Len
/// @param[Len] Assumptions: \p Len is greater than zero
Expand Down Expand Up @@ -296,8 +287,7 @@ struct Parser<Error> {
}
};

template <>
struct Parser<OptionAcknowledgment> {
template <> struct Parser<OptionAcknowledgment> {

/// Parse error packet from buffer converting all fields to host byte order
/// @param[Buffer] Assumptions: \p Buffer is not a nullptr, it's size is greater or equal than \p Len
Expand Down

0 comments on commit bf11c8a

Please sign in to comment.