Furrent
bencode_parser.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 #include "bencode_value.hpp"
7 #include "util/result.hpp"
8 
9 namespace fur::bencode {
10 
11 enum class BencodeParserError {
16  IntFormat,
18  IntValue,
22  ListFormat,
24  DictFormat,
26  DictKey,
29 };
30 
32 std::string error_to_string(BencodeParserError error);
33 
35 using BencodeResult =
37 
39  private:
41  int64_t _index{};
43  std::string _tokens;
45  BencodeResult decode();
47  BencodeResult decode_int();
49  BencodeResult decode_string();
51  BencodeResult decode_list();
53  BencodeResult decode_dict();
54 
55  public:
56  BencodeParser() = default;
57  ~BencodeParser() = default;
61  BencodeResult decode(std::string const& decoded);
62 
64  static std::string encode(BencodeValue const& value);
65 };
66 
67 } // namespace fur::bencode
fur::bencode::BencodeParserError::StringFormat
@ StringFormat
A string was not in the form ['length', ':', 'string'].
fur::bencode::BencodeParserError
BencodeParserError
Definition: bencode_parser.hpp:11
fur::bencode::BencodeParserError::DictKey
@ DictKey
A dictionary key was not a string.
fur::bencode
Contains the structure for decoding and encoding bencode data.
Definition: bencode_parser.cpp:7
fur::bencode::error_to_string
std::string error_to_string(const BencodeParserError error)
Function to translate a BencodeParserError into a string.
Definition: bencode_parser.cpp:8
fur::bencode::BencodeParserError::InvalidString
@ InvalidString
fur::bencode::BencodeParser::~BencodeParser
~BencodeParser()=default
fur::bencode::BencodeParser
Definition: bencode_parser.hpp:38
fur::bencode::BencodeParser::encode
static std::string encode(BencodeValue const &value)
Encodes a BencodeValue object into a bencode string.
Definition: bencode_parser.cpp:31
fur::bencode::BencodeParser::BencodeParser
BencodeParser()=default
fur::bencode::BencodeParserError::IntValue
@ IntValue
The integer was not a valid integer.
fur::bencode::BencodeParserError::ListFormat
@ ListFormat
A list was not in the form ['l', 'bencode_value', ... , 'e'].
fur::util::Result
Used to handle errors without using exceptions.
Definition: result.hpp:18
fur::bencode::BencodeParserError::IntFormat
@ IntFormat
A integer was not in the form ['i', 'number', 'e'].
fur::bencode::BencodeParserError::DictFormat
@ DictFormat
A dictionary was not in the form ['d', 'bencode_value', ... , 'e'].
bencode_value.hpp
fur::bencode::BencodeValue
Definition: bencode_value.hpp:14
fur::bencode::BencodeParserError::DictKeyOrder
@ DictKeyOrder
The keys of the dictionary were not in lexicographical order.
fur::bencode::BencodeResult
util::Result< std::unique_ptr< BencodeValue >, BencodeParserError > BencodeResult
Result of a parsing operation.
Definition: bencode_parser.hpp:36
result.hpp