Plan new document creation in app core

This commit is contained in:
2026-06-02 23:14:35 +02:00
parent fd1772a417
commit 853307697a
10 changed files with 284 additions and 21 deletions

View File

@@ -3,6 +3,7 @@
#include "app_core/document_route.h"
#include "foundation/result.h"
#include <array>
#include <cctype>
#include <cstdio>
#include <string>
@@ -65,6 +66,12 @@ struct DocumentVersionTarget {
std::string path;
};
struct NewDocumentPlan {
DocumentFileTarget target;
int resolution = 0;
DocumentFileWriteDecision write_decision = DocumentFileWriteDecision::save_now;
};
[[nodiscard]] constexpr ProjectOpenDecision plan_project_open(bool has_unsaved_changes) noexcept
{
return has_unsaved_changes
@@ -175,6 +182,41 @@ struct DocumentVersionTarget {
: DocumentFileWriteDecision::save_now;
}
[[nodiscard]] constexpr pp::foundation::Result<int> document_resolution_from_index(int index) noexcept
{
constexpr std::array<int, 6> resolutions{ 512, 1024, 1536, 2048, 4096, 8192 };
if (index < 0 || static_cast<std::size_t>(index) >= resolutions.size()) {
return pp::foundation::Result<int>::failure(
pp::foundation::Status::out_of_range("document resolution index is out of range"));
}
return pp::foundation::Result<int>::success(resolutions[static_cast<std::size_t>(index)]);
}
template <typename ExistsPredicate>
[[nodiscard]] pp::foundation::Result<NewDocumentPlan> plan_new_document(
std::string_view work_directory,
std::string_view document_name,
int resolution_index,
ExistsPredicate&& exists)
{
const auto resolution = document_resolution_from_index(resolution_index);
if (!resolution) {
return pp::foundation::Result<NewDocumentPlan>::failure(resolution.status());
}
auto target = make_document_file_target(work_directory, document_name);
if (!target) {
return pp::foundation::Result<NewDocumentPlan>::failure(target.status());
}
NewDocumentPlan plan;
plan.target = std::move(target.value());
plan.resolution = resolution.value();
plan.write_decision = plan_document_file_write(exists(plan.target.path));
return pp::foundation::Result<NewDocumentPlan>::success(std::move(plan));
}
[[nodiscard]] inline bool has_legacy_two_character_version_suffix(std::string_view document_name) noexcept
{
const auto dot = document_name.rfind('.');