1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-12-27 21:35:41 +00:00
kamokan/test/simulation/fake_storage.cpp

89 lines
2.7 KiB
C++
Raw Normal View History

2017-06-05 23:56:01 +00:00
/* Copyright 2017, Michele Santullo
* This file is part of "kamokan".
*
* "kamokan" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "kamokan" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "kamokan". If not, see <http://www.gnu.org/licenses/>.
*/
#include "fake_storage.hpp"
#include "num_to_token.hpp"
#include <utility>
#include <algorithm>
namespace kamokan {
2017-06-05 23:56:01 +00:00
FakeStorage::FakeStorage (const Kakoune::SafePtr<SettingsBag>& parSettings, bool parItsConnected) :
Storage(parSettings),
m_submission_num(1),
m_its_connected(parItsConnected)
{
}
FakeStorage::~FakeStorage() = default;
void FakeStorage::connect_async() {
}
bool FakeStorage::is_connected() const {
return m_its_connected;
}
void FakeStorage::finalize_connection() {
}
Storage::SubmissionResult FakeStorage::submit_pastie (
const boost::string_view& parText,
2017-06-05 23:56:01 +00:00
uint32_t parExpiry,
const boost::string_view& parLang,
bool parSelfDestruct,
2017-06-05 23:56:01 +00:00
const std::string& parRemoteIP
) const {
SubmittedPastie pastie;
std::string token = num_to_token(m_submission_num++);
2017-06-06 23:19:53 +00:00
pastie.text = std::string(parText);
2017-06-05 23:56:01 +00:00
pastie.expiry = parExpiry;
2017-06-06 23:19:53 +00:00
pastie.lang = std::string(parLang);
2017-06-05 23:56:01 +00:00
pastie.remote_ip = parRemoteIP;
pastie.token = token;
pastie.self_destruct = parSelfDestruct;
2017-06-05 23:56:01 +00:00
m_submitted_pasties.push_back(std::move(pastie));
Storage::SubmissionResult submission_res;
submission_res.token = token;
return submission_res;
}
Storage::RetrievedPastie FakeStorage::retrieve_pastie (
const boost::string_view& parToken,
uint32_t parMaxTokenLen,
const boost::string_view& parLang
) const {
2017-06-05 23:56:01 +00:00
auto it_found = std::find_if(
m_submitted_pasties.begin(),
m_submitted_pasties.end(),
[&](const SubmittedPastie& pastie) {
return pastie.token == parToken;
}
);
if (parToken.size() > static_cast<std::size_t>(parMaxTokenLen))
return RetrievedPastie (boost::optional<std::string>(), false, false);
else if (m_submitted_pasties.end() == it_found)
return RetrievedPastie (boost::optional<std::string>(), false, true);
2017-06-05 23:56:01 +00:00
else
return RetrievedPastie (boost::make_optional(it_found->text), it_found->self_destruct, true);
2017-06-05 23:56:01 +00:00
}
const std::vector<FakeStorage::SubmittedPastie>& FakeStorage::submitted_pasties() const {
return m_submitted_pasties;
}
} //namespace kamokan