1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-11-27 00:43:47 +00:00
kamokan/test/simulation/fake_storage.cpp
King_DuckZ c86df2de07 Move some pastie fetching logic into Storage.
I'm implementing an "edit" page, so I also need to retrieve
a pastie from there and I want to minimize code duplication.
2017-06-16 08:11:26 +01:00

84 lines
2.6 KiB
C++

/* 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 {
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,
uint32_t parExpiry,
const boost::string_view& parLang,
bool parSelfDestruct,
const std::string& parRemoteIP
) const {
SubmittedPastie pastie;
std::string token = num_to_token(m_submission_num++);
pastie.text = std::string(parText);
pastie.expiry = parExpiry;
pastie.lang = std::string(parLang);
pastie.remote_ip = parRemoteIP;
pastie.token = token;
pastie.self_destruct = parSelfDestruct;
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 {
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);
else
return RetrievedPastie (boost::make_optional(it_found->text), it_found->self_destruct, true);
}
const std::vector<FakeStorage::SubmittedPastie>& FakeStorage::submitted_pasties() const {
return m_submitted_pasties;
}
} //namespace kamokan