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) {
74 Json::Value Package::toJson()
const {
77 json[
"version"] = version;
81 Package Package::fromJson(
const std::string &json_str) {
82 std::istringstream jsonss(json_str);
84 Json::parseFromStream(Json::CharReaderBuilder(), jsonss, &json,
nullptr);
86 package.name = json[
"name"].asString();
87 package.version = json[
"version"].asString();
91 const std::map<data::ResultCode::Numeric, const char *> data::ResultCode::string_repr{
92 {ResultCode::Numeric::kOk,
"OK"},
99 {ResultCode::Numeric::kNeedCompletion,
"NEED_COMPLETION"},
100 {ResultCode::Numeric::kCustomError,
"CUSTOM_ERROR"},
101 {ResultCode::Numeric::kUnknown,
"UNKNOWN"},
104 std::string data::ResultCode::toRepr()
const {
105 std::string s = toString();
107 if (s.find(
'\"') != std::string::npos) {
108 throw std::runtime_error(
"Result code cannot contain double quotes");
111 return "\"" + s +
"\"" +
":" + std::to_string(
static_cast<int>(num_code));
114 ResultCode data::ResultCode::fromRepr(
const std::string &repr) {
115 size_t quote_n = repr.find(
'"');
119 if (quote_n < repr.size() - 1) {
120 size_t end_quote_n = repr.find(
'"', quote_n + 1);
121 col_n = repr.find(
':', end_quote_n + 1);
122 s = repr.substr(quote_n + 1, end_quote_n - quote_n - 1);
125 col_n = repr.find(
':');
126 s = repr.substr(0, col_n);
129 if (col_n >= repr.size() - 1) {
130 return ResultCode(Numeric::kUnknown, s);
133 int num = std::stoi(repr.substr(col_n + 1));
135 return ResultCode(
static_cast<Numeric
>(num), s);
138 Json::Value InstallationResult::toJson()
const {
140 json[
"success"] = success;
141 json[
"code"] = result_code.toString();
142 json[
"description"] = description;
146 std::ostream &operator<<(std::ostream &os,
const ResultCode &result_code) {
147 os << result_code.toRepr();
155 boost::filesystem::path utils::BasedPath::get(
const boost::filesystem::path &base)
const {
157 return Utils::absolutePath(base, p_);