Plan app export targets in app core
This commit is contained in:
111
src/app_core/document_export.h
Normal file
111
src/app_core/document_export.h
Normal file
@@ -0,0 +1,111 @@
|
||||
#pragma once
|
||||
|
||||
#include "foundation/result.h"
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
namespace pp::app {
|
||||
|
||||
struct DocumentExportFileTarget {
|
||||
std::string path;
|
||||
std::string suggested_name;
|
||||
};
|
||||
|
||||
struct DocumentExportCollectionTarget {
|
||||
std::string directory;
|
||||
std::string stem_path;
|
||||
};
|
||||
|
||||
struct DocumentExportStemTarget {
|
||||
std::string stem_path;
|
||||
};
|
||||
|
||||
struct DocumentExportSuggestedName {
|
||||
std::string name;
|
||||
};
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Result<DocumentExportFileTarget> make_document_export_file_target(
|
||||
std::string_view work_directory,
|
||||
std::string_view document_name,
|
||||
std::string_view extension)
|
||||
{
|
||||
if (document_name.empty()) {
|
||||
return pp::foundation::Result<DocumentExportFileTarget>::failure(
|
||||
pp::foundation::Status::invalid_argument("document name must not be empty"));
|
||||
}
|
||||
|
||||
if (extension.empty()) {
|
||||
return pp::foundation::Result<DocumentExportFileTarget>::failure(
|
||||
pp::foundation::Status::invalid_argument("extension must not be empty"));
|
||||
}
|
||||
|
||||
DocumentExportFileTarget target;
|
||||
target.suggested_name.reserve(document_name.size() + extension.size());
|
||||
target.suggested_name += document_name;
|
||||
target.suggested_name += extension;
|
||||
target.path.reserve(work_directory.size() + target.suggested_name.size() + 1);
|
||||
target.path += work_directory;
|
||||
target.path += "/";
|
||||
target.path += target.suggested_name;
|
||||
return pp::foundation::Result<DocumentExportFileTarget>::success(std::move(target));
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Result<DocumentExportCollectionTarget> make_document_export_collection_target(
|
||||
std::string_view work_directory,
|
||||
std::string_view document_name,
|
||||
std::string_view suffix)
|
||||
{
|
||||
if (document_name.empty()) {
|
||||
return pp::foundation::Result<DocumentExportCollectionTarget>::failure(
|
||||
pp::foundation::Status::invalid_argument("document name must not be empty"));
|
||||
}
|
||||
|
||||
DocumentExportCollectionTarget target;
|
||||
target.directory.reserve(work_directory.size() + document_name.size() + suffix.size() + 1);
|
||||
target.directory += work_directory;
|
||||
target.directory += "/";
|
||||
target.directory += document_name;
|
||||
target.directory += suffix;
|
||||
target.stem_path.reserve(target.directory.size() + document_name.size() + 1);
|
||||
target.stem_path += target.directory;
|
||||
target.stem_path += "/";
|
||||
target.stem_path += document_name;
|
||||
return pp::foundation::Result<DocumentExportCollectionTarget>::success(std::move(target));
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Result<DocumentExportStemTarget> make_document_export_stem_target(
|
||||
std::string_view directory,
|
||||
std::string_view document_name)
|
||||
{
|
||||
if (document_name.empty()) {
|
||||
return pp::foundation::Result<DocumentExportStemTarget>::failure(
|
||||
pp::foundation::Status::invalid_argument("document name must not be empty"));
|
||||
}
|
||||
|
||||
DocumentExportStemTarget target;
|
||||
target.stem_path.reserve(directory.size() + document_name.size() + 1);
|
||||
target.stem_path += directory;
|
||||
target.stem_path += "/";
|
||||
target.stem_path += document_name;
|
||||
return pp::foundation::Result<DocumentExportStemTarget>::success(std::move(target));
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Result<DocumentExportSuggestedName> make_document_export_suggested_name(
|
||||
std::string_view document_name,
|
||||
std::string_view suffix)
|
||||
{
|
||||
if (document_name.empty()) {
|
||||
return pp::foundation::Result<DocumentExportSuggestedName>::failure(
|
||||
pp::foundation::Status::invalid_argument("document name must not be empty"));
|
||||
}
|
||||
|
||||
DocumentExportSuggestedName target;
|
||||
target.name.reserve(document_name.size() + suffix.size());
|
||||
target.name += document_name;
|
||||
target.name += suffix;
|
||||
return pp::foundation::Result<DocumentExportSuggestedName>::success(std::move(target));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user