24#include <unordered_set>
27#include <nlohmann/json.hpp>
35 void validateUniquePaths(
const std::vector<Document>& documents)
37 std::unordered_set<std::string_view> paths;
38 for (
const auto& document : documents) {
39 if (!paths.insert(document.getPath()).second)
40 throw std::invalid_argument(
41 std::format(
"Duplicate project document path: {}", document.getPath()));
46 const std::string_view oldSlug,
47 const std::string_view newSlug,
48 const bool renamedDocument)
53 auto json = nlohmann::ordered_json::parse(document.
getContents());
54 auto* circuit = &json;
55 if (
const auto it = json.find(
"circuit"); it != json.end() && it->is_object())
60 (*circuit)[
"name"] = std::string(newSlug);
64 const auto components = circuit->find(
"components");
65 if (components != circuit->end() && components->is_array()) {
66 for (
auto& component : *components) {
67 if (!component.is_object())
69 const auto type = component.find(
"type");
70 const auto properties = component.find(
"properties");
71 if (type == component.end() || !type->is_string() || properties == component.end()
72 || !properties->is_object())
75 const auto propertyName =
80 auto property = properties->find(propertyName);
81 if (type->get_ref<
const std::string&>() == expectedType
82 && property != properties->end() && property->is_string()
83 && property->get_ref<
const std::string&>() == oldSlug) {
84 *
property = std::string(newSlug);
108 validateUniquePaths(nextDocuments);
111 commit(std::move(nextDocuments), std::move(nextDependencies),
118 const auto existing =
122 const auto path = document.
getPath();
123 if (existing == nextDocuments.end())
124 nextDocuments.push_back(std::move(document));
126 *existing = std::move(document);
130 commit(std::move(nextDocuments), std::move(nextDependencies),
131 {.kind = kind, .path = path});
137 throw std::invalid_argument(
138 std::format(
"Duplicate project document path: {}", document.
getPath()));
141 const auto path = document.
getPath();
142 const auto offset = std::min(index, nextDocuments.size());
143 nextDocuments.insert(nextDocuments.begin() +
static_cast<std::ptrdiff_t
>(offset),
144 std::move(document));
148 commit(std::move(nextDocuments), std::move(nextDependencies),
153 const std::string_view newPath)
157 if (!oldType || !newType || *oldType != *newType)
158 throw std::invalid_argument(
"A document rename must preserve its document type");
160 throw std::invalid_argument(std::format(
"Unknown project document: {}", oldPath));
161 if (oldPath == newPath)
164 throw std::invalid_argument(
165 std::format(
"Project document already exists: {}", newPath));
169 if (!oldSlug || !newSlug)
170 throw std::invalid_argument(
"A document rename requires canonical document paths");
173 for (
auto& document : nextDocuments) {
174 const bool renamedDocument = document.
getPath() == oldPath;
178 rewriteDocumentReferences(document, *oldType, *oldSlug, *newSlug, renamedDocument);
183 commit(std::move(nextDocuments), std::move(nextDependencies),
194 std::erase_if(nextDocuments, [&](
const Document& document) {
195 return document.
getPath() == documentPath;
200 commit(std::move(nextDocuments), std::move(nextDependencies),
209 documentStore.commitDocuments(std::move(nextDocuments), change);
void setContents(std::string contents)
DocumentType getType() const noexcept
const std::string & getPath() const noexcept
const std::string & getContents() const noexcept
static constexpr std::string_view Type
static constexpr std::string_view Type
Runtime-only graph of Circuit-to-Circuit instantiation dependencies.
void rebuildFromProject(const std::vector< Document > &documents)
Rebuilds the graph from a full project description.
const std::string & getPath() const noexcept
void upsertDocument(Document document)
void renameDocument(std::string_view oldPath, std::string_view newPath)
void insertDocument(Document document, std::size_t index)
DocumentStore documentStore
void removeDocument(std::string_view documentPath)
void setDocuments(std::vector< Document > documents)
const CircuitDependencyGraph & circuitDependencies() const noexcept
void commit(std::vector< Document > documents, CircuitDependencyGraph dependencies, const DocumentChange &change) noexcept
const DocumentStore & documents() const noexcept
CircuitDependencyGraph dependencyGraph
std::optional< std::string > documentSlugForPath(const std::string_view path)
std::optional< DocumentType > documentTypeForPath(const std::string_view path)