27#include <nlohmann/json.hpp>
38 using SILICON::core::Circuit;
39 using SILICON::project::Document;
43 materializeDocument(SILICON::conversion::SemanticDocument&& document)
45 if (
auto* source = std::get_if<SILICON::conversion::VerilogSource>(&document.payload))
46 return {std::move(document.path), std::move(
source->contents)};
47 if (
auto* source = std::get_if<SILICON::conversion::BinarySource>(&document.payload))
48 return {std::move(document.path), std::move(
source->contents)};
51 std::make_shared<Circuit>(std::get<Circuit>(std::move(document.payload)));
54 throw std::invalid_argument(
"Converted circuit has an invalid document path");
59 auto sceneJson = scene.serialize();
60 auto completed = nlohmann::ordered_json::parse(sceneJson);
63 return {std::move(document.path), completed.dump(2)};
67 constexpr bool YOSYS_AVAILABLE =
false;
68 constexpr std::string_view YOSYS_UNAVAILABLE_REASON =
69 "Conversion requires Yosys and is unavailable in the web build";
71 constexpr bool YOSYS_AVAILABLE =
true;
72 constexpr std::string_view YOSYS_UNAVAILABLE_REASON{};
75 constexpr std::array CONVERTERS{
77 .source = DocumentType::Circuit,
78 .target = DocumentType::Verilog,
79 .available = YOSYS_AVAILABLE,
80 .unavailableReason = YOSYS_UNAVAILABLE_REASON,
83 .source = DocumentType::Verilog,
84 .target = DocumentType::Circuit,
85 .available = YOSYS_AVAILABLE,
86 .unavailableReason = YOSYS_UNAVAILABLE_REASON,
96 std::ranges::find_if(CONVERTERS, [source, target](
const auto& converter) {
97 return converter.source == source && converter.target == target;
99 return found == CONVERTERS.end() ?
nullptr : std::to_address(found);
104 std::vector<const DocumentConverter*> result;
105 for (
const auto& converter : CONVERTERS) {
106 if (converter.source == source)
107 result.push_back(&converter);
112PreparedDocumentConversion
114 const std::span<const Document> projectDocuments,
120 throw std::invalid_argument(
"The selected document has no registered conversion");
121 if (!converter->available)
122 throw std::runtime_error(std::string(converter->unavailableReason));
125 source, target, projectDocuments, registry, resolver);
127 .choices = std::move(prepared.choices),
130 std::move(prepared.execute)](
const std::span<const std::string> selected) {
131 auto semantic = execute(selected);
132 std::vector<Document> documents;
133 documents.reserve(semantic.documents.size());
134 for (
auto& document : semantic.documents)
135 documents.push_back(materializeDocument(std::move(document)));
137 .activatePath = std::move(semantic.activatePath)};
Graphics scene for the circuit diagram editor.
void setSubcircuitDocumentMode(bool enabled)
Enables reusable-circuit boundary I/O editing for the scene.
static GUIComponentFactory & instance()
Gets the global singleton factory instance.
PreparedSemanticConversion prepareDocumentConversion(const Document &source, const DocumentType target, const std::span< const Document > projectDocuments, const SILICON::core::ComponentRegistry ®istry, const SILICON::core::CircuitResolver &resolver)
std::optional< std::string > documentSlugForPath(const std::string_view path)
nlohmann::ordered_json graphicalSubcircuitMetadataToJson(const GraphicalSubcircuitMetadata &metadata)
Serializes graphical metadata to the JSON shape/port object.
std::vector< const DocumentConverter * > documentConvertersFor(const DocumentType source)
PreparedDocumentConversion prepareDocumentConversion(const Document &source, const DocumentType target, const std::span< const Document > projectDocuments, const SILICON::core::ComponentRegistry ®istry, const SILICON::core::CircuitResolver &resolver)
GraphicalSubcircuitMetadata synchronizeGraphicalSubcircuitMetadata(const nlohmann::json &document, const GraphicalSubcircuitMetadata &fallbackMetadata, const CircuitResolver *resolver)
Reconciles saved graphical metadata with the current circuit document.
const DocumentConverter * documentConverterFor(const DocumentType source, const DocumentType target)