7 #include "utilities/utils.h"
9 std::ostream &operator<<(std::ostream &os,
const StorageType stype) {
10 std::string stype_str;
12 case StorageType::kFileSystem:
13 stype_str =
"filesystem";
15 case StorageType::kSqlite:
19 stype_str =
"unknown";
22 os <<
'"' << stype_str <<
'"';
26 std::string TimeToString(
struct tm time) {
27 std::array<char, 22> formatted{};
28 strftime(formatted.data(), 22,
"%Y-%m-%dT%H:%M:%SZ", &time);
29 return std::string(formatted.data());
36 struct tm time_struct {};
38 gmtime_r(&raw_time, &time_struct);
44 if (rfc3339.length() != 20 || rfc3339[19] !=
'Z') {
52 bool TimeStamp::IsValid()
const {
return time_.length() != 0; }
54 bool TimeStamp::IsExpiredAt(
const TimeStamp &now)
const {
64 bool TimeStamp::operator<(
const TimeStamp &other)
const {
return IsValid() && other.IsValid() && time_ < other.time_; }
66 bool TimeStamp::operator>(
const TimeStamp &other)
const {
return (other < *
this); }
68 std::ostream &operator<<(std::ostream &os,
const TimeStamp &t) {
75 const std::map<data::ResultCode::Numeric, const char *> data::ResultCode::string_repr{
76 {ResultCode::Numeric::kOk,
"OK"},
83 {ResultCode::Numeric::kNeedCompletion,
"NEED_COMPLETION"},
84 {ResultCode::Numeric::kCustomError,
"CUSTOM_ERROR"},
85 {ResultCode::Numeric::kUnknown,
"UNKNOWN"},
88 std::string data::ResultCode::toRepr()
const {
89 std::string s = toString();
91 if (s.find(
'\"') != std::string::npos) {
92 throw std::runtime_error(
"Result code cannot contain double quotes");
95 return "\"" + s +
"\"" +
":" + std::to_string(
static_cast<int>(num_code));
98 ResultCode data::ResultCode::fromRepr(
const std::string &repr) {
99 size_t quote_n = repr.find(
'"');
103 if (quote_n < repr.size() - 1) {
104 size_t end_quote_n = repr.find(
'"', quote_n + 1);
105 col_n = repr.find(
':', end_quote_n + 1);
106 s = repr.substr(quote_n + 1, end_quote_n - quote_n - 1);
109 col_n = repr.find(
':');
110 s = repr.substr(0, col_n);
113 if (col_n >= repr.size() - 1) {
114 return ResultCode(Numeric::kUnknown, s);
117 int num = std::stoi(repr.substr(col_n + 1));
119 return ResultCode(
static_cast<Numeric
>(num), s);
122 Json::Value InstallationResult::toJson()
const {
124 json[
"success"] = success;
125 json[
"code"] = result_code.toString();
126 json[
"description"] = description;
130 std::ostream &operator<<(std::ostream &os,
const ResultCode &result_code) {
131 os << result_code.toRepr();
139 boost::filesystem::path utils::BasedPath::get(
const boost::filesystem::path &base)
const {
141 return Utils::absolutePath(base, p_);