-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand.cpp
More file actions
89 lines (77 loc) · 1.78 KB
/
Copy pathcommand.cpp
File metadata and controls
89 lines (77 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <cstddef>
#include <stdexcept>
#include <string_view>
#include <vector>
#include "command.hpp"
namespace auth
{
namespace server
{
namespace
{
using Lexemes = std::vector<std::string_view>;
Lexemes split(std::string_view str)
{
Lexemes lexemes{};
std::size_t start = 0;
while (true)
{
size_t pos = str.find(' ', start);
if (pos == std::string_view::npos)
{
lexemes.emplace_back(str.substr(start));
break;
}
lexemes.emplace_back(str.substr(start, pos - start));
start = pos + 1;
}
return lexemes;
}
void assert_arguments_number(const Lexemes& lexemes,
const std::size_t expectedArgsNumber)
{
if (lexemes.size() != expectedArgsNumber)
{
throw std::runtime_error("invalid arguments number");
}
}
} // namespace
Command::Command(std::string_view str)
{
const auto lexemes = split(str);
if (lexemes.size() < 1)
{
throw std::runtime_error("empty command");
}
const auto action = lexemes[0];
if (action == "login")
{
assert_arguments_number(lexemes, 3);
args_ = args::Login{lexemes[1], lexemes[2]};
}
else if (action == "logout")
{
assert_arguments_number(lexemes, 2);
args_ = args::Logout{lexemes[1]};
}
else if (action == "adduser")
{
assert_arguments_number(lexemes, 4);
args_ = args::Adduser{lexemes[1], lexemes[2], lexemes[3]};
}
else if (action == "deluser")
{
assert_arguments_number(lexemes, 3);
args_ = args::Deluser{lexemes[1], lexemes[2]};
}
else
{
throw std::runtime_error("unknown command");
}
}
const Command::Args& Command::GetArgs() const noexcept
{
return args_;
}
} // namespace server
} // namespace auth