-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsubmission.cpp
More file actions
86 lines (77 loc) · 2.25 KB
/
submission.cpp
File metadata and controls
86 lines (77 loc) · 2.25 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
#include "mine/submission.hpp"
#include <sstream>
#include <stdexcept>
#include "lang/parser.hpp"
std::string getStringField(const jute::jValue& json, const std::string& name,
bool required = true) {
auto field = const_cast<jute::jValue&>(json)[name];
if (field.get_type() == jute::JSTRING) {
return field.as_string();
} else if (required) {
throw std::runtime_error("Missing or invalid '" + name +
"' field in submission JSON");
}
return "";
}
Submission Submission::fromJson(const jute::jValue& json) {
Submission submission;
submission.id = UID(getStringField(json, "id"));
submission.type = typeFromString(getStringField(json, "type"));
submission.mode = modeFromString(getStringField(json, "mode"));
submission.content = getStringField(json, "content", false);
submission.submitter = getStringField(json, "submitter", false);
return submission;
}
Program Submission::toProgram() const {
Program program;
if (content.empty()) {
return program;
}
Parser parser;
std::istringstream parse_stream(content);
return parser.parse(parse_stream);
}
Submission::Mode Submission::modeFromString(const std::string& mode_str) {
if (mode_str == "add") {
return Mode::ADD;
} else if (mode_str == "update") {
return Mode::UPDATE;
} else if (mode_str == "remove") {
return Mode::REMOVE;
} else {
throw std::runtime_error("Invalid submission mode: " + mode_str);
}
}
Submission::Type Submission::typeFromString(const std::string& type_str) {
if (type_str == "program") {
return Type::PROGRAM;
} else if (type_str == "sequence") {
return Type::SEQUENCE;
} else if (type_str == "bfile") {
return Type::BFILE;
} else {
throw std::runtime_error("Invalid submission type: " + type_str);
}
}
std::string Submission::modeToString(Mode mode) {
switch (mode) {
case Mode::ADD:
return "add";
case Mode::UPDATE:
return "update";
case Mode::REMOVE:
return "remove";
}
return "unknown";
}
std::string Submission::typeToString(Type type) {
switch (type) {
case Type::PROGRAM:
return "program";
case Type::SEQUENCE:
return "sequence";
case Type::BFILE:
return "bfile";
}
return "unknown";
}