Rise
The Vieneo Province
Loading...
Searching...
No Matches
ImmigrationEmail.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5
7{
8 inline std::wstring Normalize(std::wstring text)
9 {
10 const auto first = text.find_first_not_of(L" \t\r\n\v\f");
11 if (first == std::wstring::npos) return {};
12 text = text.substr(first, text.find_last_not_of(L" \t\r\n\v\f") - first + 1);
13 for (auto& ch : text)
14 if (ch >= L'A' && ch <= L'Z') ch += L'a' - L'A';
15 return text;
16 }
17
18 class State
19 {
20 public:
21 enum { Invalid = 0, Checking = 1, Passed = 2, Unavailable = 3 };
22 enum class Reply { Ignored, Validation, SaveFailed };
23 static constexpr int MaximumToken = 16777215;
24 static constexpr std::uint64_t TimeoutMs = 15000;
25
26 void Reset(const std::wstring& saved)
27 {
28 original = current = Normalize(saved);
29 pendingToken = 0;
30 submitting = false;
31 status = current.empty() ? Invalid : Passed;
32 // Keep the sequence across form visits so old replies cannot approve a new visit.
33 }
34
35 void Edit(const std::wstring& text)
36 {
37 const auto value = Normalize(text);
38 if (submitting || value == current) return;
39 current = value;
40 pendingToken = 0;
41 status = !original.empty() && current == original ? Passed : Invalid;
42 }
43
44 bool CanEdit(bool annual) const { return !submitting && (annual || original.empty()); }
45 bool PendingSave() const { return submitting; }
46 int Status() const { return status; }
47 const std::wstring& Value() const { return current; }
48 void RejectInput() { if (!submitting) { pendingToken = 0; status = Invalid; } }
49
50 int BeginCheck(std::uint64_t now)
51 {
52 if (submitting || status == Passed || status == Checking || current.empty()) return 0;
55 started = now;
57 return pendingToken;
58 }
59
60 bool Expire(std::uint64_t now)
61 {
62 if (status != Checking || now - started < TimeoutMs) return false;
63 pendingToken = 0;
65 return true;
66 }
67
68 Reply Receive(float token, bool passed, bool unavailable)
69 {
70 if (submitting)
71 {
72 // Token zero is reserved for a rejection of the actual final submission.
73 if (token != 0.0f || passed) return Reply::Ignored;
74 submitting = false;
75 status = unavailable ? Unavailable : Invalid;
76 return Reply::SaveFailed;
77 }
78 if (status != Checking || !pendingToken || token != static_cast<float>(pendingToken))
79 return Reply::Ignored;
80 pendingToken = 0;
81 status = passed ? Passed : (unavailable ? Unavailable : Invalid);
82 return Reply::Validation;
83 }
84
85 bool BeginSave()
86 {
87 if (submitting || status != Passed || current.empty()) return false;
88 pendingToken = 0;
89 submitting = true;
90 return true;
91 }
92
93 void Saved()
94 {
96 submitting = false;
97 status = Passed;
98 }
99
100 private:
101 std::wstring original, current;
103 std::uint64_t started = 0;
104 bool submitting = false;
105 };
106}
bool CanEdit(bool annual) const
bool Expire(std::uint64_t now)
static constexpr std::uint64_t TimeoutMs
const std::wstring & Value() const
void Reset(const std::wstring &saved)
static constexpr int MaximumToken
void Edit(const std::wstring &text)
int BeginCheck(std::uint64_t now)
Reply Receive(float token, bool passed, bool unavailable)
std::wstring Normalize(std::wstring text)