cli MVP
This commit is contained in:
parent
367c19386c
commit
1ea50c50bc
62 changed files with 9982 additions and 608 deletions
|
|
@ -1,5 +1,5 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro:Utils;
|
||||
|
||||
|
|
@ -19,8 +19,8 @@ export namespace Nostro {
|
|||
case '\r': out += "\\r"; break;
|
||||
case '\t': out += "\\t"; break;
|
||||
default:
|
||||
if (static_cast<unsigned char>(c) < 0x20) {
|
||||
out += std::format("\\u{:04x}", static_cast<unsigned char>(c));
|
||||
if (static_cast<std::uint8_t>(c) < 0x20) {
|
||||
out += std::format("\\u{:04x}", static_cast<std::uint8_t>(c));
|
||||
} else {
|
||||
out += c;
|
||||
}
|
||||
|
|
@ -31,13 +31,11 @@ export namespace Nostro {
|
|||
|
||||
constexpr std::chrono::system_clock::time_point ParseTimestamp(std::string_view text) {
|
||||
const auto field = [text](std::size_t offset, std::size_t length) {
|
||||
// substr cuts a field that would run past the end short, and the size check refuses it.
|
||||
const std::string_view digits = text.substr(offset, length);
|
||||
std::uint32_t value = 0;
|
||||
const char* const first = text.data() + offset;
|
||||
if(offset + length > text.size()) {
|
||||
throw std::runtime_error(std::format("malformed timestamp '{}'", text));
|
||||
}
|
||||
const std::from_chars_result result = std::from_chars(first, first + length, value);
|
||||
if(result.ec != std::errc() || result.ptr != first + length) {
|
||||
const std::from_chars_result result = std::from_chars(digits.data(), digits.data() + digits.size(), value);
|
||||
if(digits.size() != length || result.ec != std::errc() || result.ptr != digits.data() + digits.size()) {
|
||||
throw std::runtime_error(std::format("malformed timestamp '{}'", text));
|
||||
}
|
||||
return value;
|
||||
|
|
@ -66,4 +64,117 @@ export namespace Nostro {
|
|||
|
||||
return std::chrono::sys_days(date) + std::chrono::hours(field(11, 2)) + std::chrono::minutes(field(14, 2)) + std::chrono::seconds(field(17, 2)) + std::chrono::microseconds(micros);
|
||||
}
|
||||
|
||||
// The inverse of ParseTimestamp: "YYYY-MM-DD hh:mm:ss.ffffff" in UTC, the format bunq reads and writes.
|
||||
inline std::string FormatTimestamp(std::chrono::system_clock::time_point time) {
|
||||
return std::format("{:%Y-%m-%d %H:%M:%S}", std::chrono::floor<std::chrono::microseconds>(time));
|
||||
}
|
||||
|
||||
// "YYYY-MM-DD" or "YYYY-MM-DD hh:mm" in the local time zone, for dates typed by a person, moved `addDays` days on
|
||||
// by the calendar: ParseLocalDate(day, 1) is the midnight that ends `day`, 23 or 25 hours on when the clocks change.
|
||||
inline std::chrono::system_clock::time_point ParseLocalDate(std::string_view text, std::int32_t addDays = 0) {
|
||||
const auto number = [text](std::size_t offset, std::size_t length) {
|
||||
int value = 0;
|
||||
const char* const first = text.data() + offset;
|
||||
const std::from_chars_result result = std::from_chars(first, first + length, value);
|
||||
if(result.ec != std::errc() || result.ptr != first + length) {
|
||||
throw std::runtime_error(std::format("'{}' is not a date (YYYY-MM-DD or YYYY-MM-DD hh:mm)", text));
|
||||
}
|
||||
return value;
|
||||
};
|
||||
const bool hasTime = text.size() == 16;
|
||||
if((text.size() != 10 && !hasTime) || text[4] != '-' || text[7] != '-' || (hasTime && (text[10] != ' ' || text[13] != ':'))) {
|
||||
throw std::runtime_error(std::format("'{}' is not a date (YYYY-MM-DD or YYYY-MM-DD hh:mm)", text));
|
||||
}
|
||||
std::tm local{};
|
||||
local.tm_year = number(0, 4) - 1900;
|
||||
local.tm_mon = number(5, 2) - 1;
|
||||
local.tm_mday = number(8, 2);
|
||||
local.tm_hour = hasTime ? number(11, 2) : 0;
|
||||
local.tm_min = hasTime ? number(14, 2) : 0;
|
||||
local.tm_isdst = -1;
|
||||
const std::chrono::year_month_day date(std::chrono::year(local.tm_year + 1900), std::chrono::month(local.tm_mon + 1), std::chrono::day(local.tm_mday));
|
||||
if(!date.ok() || local.tm_hour > 23 || local.tm_min > 59) {
|
||||
throw std::runtime_error(std::format("'{}' is not a date (YYYY-MM-DD or YYYY-MM-DD hh:mm)", text));
|
||||
}
|
||||
// mktime normalises a day past the month's end, and works out the offset of the day it lands on.
|
||||
local.tm_mday += addDays;
|
||||
return std::chrono::system_clock::from_time_t(std::mktime(&local));
|
||||
}
|
||||
|
||||
// "YYYY-MM-DD hh:mm" in the local time zone.
|
||||
inline std::string FormatLocalTime(std::chrono::system_clock::time_point time) {
|
||||
const std::time_t seconds = std::chrono::system_clock::to_time_t(time);
|
||||
const std::tm local = *std::localtime(&seconds);
|
||||
return std::format("{:04}-{:02}-{:02} {:02}:{:02}", local.tm_year + 1900, local.tm_mon + 1, local.tm_mday, local.tm_hour, local.tm_min);
|
||||
}
|
||||
|
||||
// Percent-encodes everything but RFC 3986's unreserved characters, for a query parameter.
|
||||
constexpr std::string UrlEncode(std::string_view text) {
|
||||
constexpr std::string_view hex = "0123456789ABCDEF";
|
||||
std::string out;
|
||||
for(const char c : text) {
|
||||
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '.' || c == '_' || c == '~') {
|
||||
out += c;
|
||||
} else {
|
||||
const std::uint8_t byte = static_cast<std::uint8_t>(c);
|
||||
out += '%';
|
||||
out += hex[byte >> 4];
|
||||
out += hex[byte & 0xF];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Decodes %XX and, as in a query string, '+' for a space. A '%' not followed by two hex digits stays as it is.
|
||||
constexpr std::string UrlDecode(std::string_view text) {
|
||||
const auto digit = [](char c) -> std::int32_t {
|
||||
if(c >= '0' && c <= '9') return c - '0';
|
||||
if(c >= 'a' && c <= 'f') return c - 'a' + 10;
|
||||
if(c >= 'A' && c <= 'F') return c - 'A' + 10;
|
||||
return -1;
|
||||
};
|
||||
std::string out;
|
||||
for(std::size_t i = 0; i < text.size(); i++) {
|
||||
if(text[i] == '+') {
|
||||
out += ' ';
|
||||
} else if(text[i] == '%' && i + 2 < text.size() && digit(text[i + 1]) >= 0 && digit(text[i + 2]) >= 0) {
|
||||
out += static_cast<char>(digit(text[i + 1]) * 16 + digit(text[i + 2]));
|
||||
i += 2;
|
||||
} else {
|
||||
out += text[i];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// The decoded value of `name` in the query string of `url` (everything between '?' and '#'), if it is there.
|
||||
constexpr std::optional<std::string> QueryParameter(std::string_view url, std::string_view name) {
|
||||
const std::size_t question = url.find('?');
|
||||
if(question == std::string_view::npos) {
|
||||
return std::nullopt;
|
||||
}
|
||||
std::string_view query = url.substr(question + 1);
|
||||
query = query.substr(0, query.find('#'));
|
||||
for(const auto part : std::views::split(query, '&')) {
|
||||
const std::string_view pair(part.begin(), part.end());
|
||||
const std::size_t equals = pair.find('=');
|
||||
if(UrlDecode(pair.substr(0, equals)) == name) {
|
||||
return equals == std::string_view::npos ? std::string() : UrlDecode(pair.substr(equals + 1));
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// Where the library's warnings go: a connector that could not be refreshed, an account it skipped, a request made
|
||||
// but not read back. A line on stderr unless the program replaces it; a daemon sends them to the client that made
|
||||
// the call instead, as on stderr they would only reach its journal.
|
||||
std::function<void(std::string_view)> noteSink = [](std::string_view note) {
|
||||
std::println(std::cerr, "{}", note);
|
||||
};
|
||||
|
||||
// Hands `note` to noteSink.
|
||||
inline void Note(std::string_view note) {
|
||||
noteSink(note);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue