SILICON
Loading...
Searching...
No Matches
projectContext.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "projectContext.hpp"
19
20#include <algorithm>
21#include <format>
22#include <ranges>
23#include <stdexcept>
24#include <unordered_set>
25#include <utility>
26
27#include <nlohmann/json.hpp>
28
29#include <core/memory.hpp>
30#include <core/subcircuit.hpp>
31
32namespace SILICON::project {
33namespace {
34
35 void validateUniquePaths(const std::vector<Document>& documents)
36 {
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()));
42 }
43 }
44
45 void rewriteDocumentReferences(Document& document, const DocumentType renamedType,
46 const std::string_view oldSlug,
47 const std::string_view newSlug,
48 const bool renamedDocument)
49 {
50 if (document.getType() != DocumentType::Circuit)
51 return;
52
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())
56 circuit = &*it;
57
58 bool changed = false;
59 if (renamedDocument && renamedType == DocumentType::Circuit) {
60 (*circuit)["name"] = std::string(newSlug);
61 changed = true;
62 }
63
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())
68 continue;
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())
73 continue;
74
75 const auto propertyName =
76 renamedType == DocumentType::Circuit ? "slug" : "binaryContents";
77 const auto expectedType = renamedType == DocumentType::Circuit
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);
85 changed = true;
86 }
87 }
88 }
89
90 if (changed)
91 document.setContents(json.dump(2));
92 }
93
94} // namespace
95
97{
98 return documentStore;
99}
100
105
106void ProjectContext::setDocuments(std::vector<Document> nextDocuments)
107{
108 validateUniquePaths(nextDocuments);
109 CircuitDependencyGraph nextDependencies;
110 nextDependencies.rebuildFromProject(nextDocuments);
111 commit(std::move(nextDocuments), std::move(nextDependencies),
112 {.kind = DocumentChangeKind::Reset, .path = std::nullopt});
113}
114
116{
117 auto nextDocuments = documentStore.getDocuments();
118 const auto existing =
119 std::ranges::find(nextDocuments, document.getPath(), &Document::getPath);
120 const auto kind = existing == nextDocuments.end() ? DocumentChangeKind::Added
122 const auto path = document.getPath();
123 if (existing == nextDocuments.end())
124 nextDocuments.push_back(std::move(document));
125 else
126 *existing = std::move(document);
127
128 CircuitDependencyGraph nextDependencies;
129 nextDependencies.rebuildFromProject(nextDocuments);
130 commit(std::move(nextDocuments), std::move(nextDependencies),
131 {.kind = kind, .path = path});
132}
133
134void ProjectContext::insertDocument(Document document, const std::size_t index)
135{
136 if (documentStore.contains(document.getPath()))
137 throw std::invalid_argument(
138 std::format("Duplicate project document path: {}", document.getPath()));
139
140 auto nextDocuments = documentStore.getDocuments();
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));
145
146 CircuitDependencyGraph nextDependencies;
147 nextDependencies.rebuildFromProject(nextDocuments);
148 commit(std::move(nextDocuments), std::move(nextDependencies),
149 {.kind = DocumentChangeKind::Added, .path = path});
150}
151
152void ProjectContext::renameDocument(const std::string_view oldPath,
153 const std::string_view newPath)
154{
155 const auto oldType = documentTypeForPath(oldPath);
156 const auto newType = documentTypeForPath(newPath);
157 if (!oldType || !newType || *oldType != *newType)
158 throw std::invalid_argument("A document rename must preserve its document type");
159 if (!documentStore.contains(oldPath))
160 throw std::invalid_argument(std::format("Unknown project document: {}", oldPath));
161 if (oldPath == newPath)
162 return;
163 if (documentStore.contains(newPath))
164 throw std::invalid_argument(
165 std::format("Project document already exists: {}", newPath));
166
167 const auto oldSlug = documentSlugForPath(oldPath);
168 const auto newSlug = documentSlugForPath(newPath);
169 if (!oldSlug || !newSlug)
170 throw std::invalid_argument("A document rename requires canonical document paths");
171
172 auto nextDocuments = documentStore.getDocuments();
173 for (auto& document : nextDocuments) {
174 const bool renamedDocument = document.getPath() == oldPath;
175 if (renamedDocument)
176 document = Document(std::string(newPath), document.getContents());
177 if (*oldType == DocumentType::Circuit || *oldType == DocumentType::RawBinary)
178 rewriteDocumentReferences(document, *oldType, *oldSlug, *newSlug, renamedDocument);
179 }
180
181 CircuitDependencyGraph nextDependencies;
182 nextDependencies.rebuildFromProject(nextDocuments);
183 commit(std::move(nextDocuments), std::move(nextDependencies),
184 {.kind = DocumentChangeKind::Reset, .path = std::nullopt});
185}
186
187void ProjectContext::removeDocument(const std::string_view documentPath)
188{
189 if (!documentStore.contains(documentPath))
190 return;
191
192 dependencyGraph.validateDocumentRemoval(documentPath);
193 auto nextDocuments = documentStore.getDocuments();
194 std::erase_if(nextDocuments, [&](const Document& document) {
195 return document.getPath() == documentPath;
196 });
197
198 CircuitDependencyGraph nextDependencies;
199 nextDependencies.rebuildFromProject(nextDocuments);
200 commit(std::move(nextDocuments), std::move(nextDependencies),
201 {.kind = DocumentChangeKind::Removed, .path = std::string(documentPath)});
202}
203
204void ProjectContext::commit(std::vector<Document> nextDocuments,
205 CircuitDependencyGraph nextDependencies,
206 const DocumentChange& change) noexcept
207{
208 dependencyGraph = std::move(nextDependencies);
209 documentStore.commitDocuments(std::move(nextDocuments), change);
210}
211
212} // namespace SILICON::project
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
Definition memory.hpp:32
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)
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)