// =========================================================================== // component_manifest.cpp -- Turn declared component/subject series into a // concrete build artifact (NS-SST network/finance + NS-SST-0001 2.0 subjects). // =========================================================================== #include "component_manifest.h" #include #include "sst_parser.h" // network/finance enum <-> name mappings #include "subject_model.h" // evidence/work status names namespace nordshrift { namespace { using semantic::Subject; std::vector networkObjectNames(const NetworkSpec& n) { std::vector v; for (auto o : n.objects) v.push_back(networkObjectName(o)); return v; } std::vector networkTransportNames(const NetworkSpec& n) { std::vector v; for (auto t : n.transports) v.push_back(networkTransportName(t)); return v; } std::vector financeObjectNames(const FinanceSpec& f) { std::vector v; for (auto o : f.objects) v.push_back(financeObjectName(o)); return v; } std::vector reachVerbNames(const ReachSpec& r) { std::vector v; for (auto x : r.verbs) v.push_back(reachVerbName(x)); return v; } std::vector reachChannelNames(const ReachSpec& r) { std::vector v; for (auto x : r.channels) v.push_back(reachChannelName(x)); return v; } std::vector measureMetricNames(const MeasureSpec& m) { std::vector v; for (auto x : m.metrics) v.push_back(measureMetricName(x)); return v; } // ---- language-neutral join helpers --------------------------------------- std::string quotedList(const std::vector& xs) { std::string s; for (size_t i = 0; i < xs.size(); i++) { if (i) s += ", "; s += "\"" + xs[i] + "\""; } return s; } // Escape a user-provided String value for safe embedding in generated Java / C // / Sleela source (the value can contain quotes, backslashes, newlines, tabs). // network/finance names are closed enum identifiers and never need this, but // `strings:` values are arbitrary user text, so they must be escaped. std::string escapeForSource(const std::string& in) { std::string out; out.reserve(in.size() + 8); for (char c : in) { switch (c) { case '\\': out += "\\\\"; break; case '"': out += "\\\""; break; case '\n': out += "\\n"; break; case '\r': out += "\\r"; break; case '\t': out += "\\t"; break; default: out += c; break; } } return out; } std::string quotedEscapedList(const std::vector& xs) { std::string s; for (size_t i = 0; i < xs.size(); i++) { if (i) s += ", "; s += "\"" + escapeForSource(xs[i]) + "\""; } return s; } // ---- Java manifest -------------------------------------------------------- std::string emitJava(const Sheet& sheet, const std::string& pkg) { std::ostringstream o; o << "// Generated by Nordshrift (Java component manifest)\n"; if (!pkg.empty()) o << "package " << pkg << ";\n\n"; o << "public final class ComponentManifest {\n"; o << " private ComponentManifest() {}\n\n"; o << " public static final String SHEET = \"" << sheet.meta.name << "\";\n\n"; if (sheet.network.present) { o << " public static final String[] NETWORK_OBJECTS = { " << quotedList(networkObjectNames(sheet.network)) << " };\n"; o << " public static final String[] NETWORK_TRANSPORTS = { " << quotedList(networkTransportNames(sheet.network)) << " };\n"; o << " public static final String NETWORK_ADDRESS_FAMILY = \"" << networkAddressFamilyName(sheet.network.addressFamily) << "\";\n"; o << " public static final boolean NETWORK_TLS = " << (sheet.network.tls ? "true" : "false") << ";\n\n"; } if (sheet.finance.present) { o << " public static final String[] FINANCE_OBJECTS = { " << quotedList(financeObjectNames(sheet.finance)) << " };\n"; o << " public static final String FINANCE_CURRENCY = \"" << (sheet.finance.hasCurrency ? sheet.finance.currency : "") << "\";\n"; o << " public static final String FINANCE_PERIOD = \"" << financePeriodName(sheet.finance.period) << "\";\n"; o << " public static final String FINANCE_DISCOUNTING = \"" << financeDiscountingName(sheet.finance.discounting) << "\";\n\n"; } if (sheet.reach.present) { o << " public static final String[] REACH_VERBS = { " << quotedList(reachVerbNames(sheet.reach)) << " };\n"; o << " public static final String[] REACH_CHANNELS = { " << quotedList(reachChannelNames(sheet.reach)) << " };\n"; o << " public static final int REACH_MIN_VERBS = " << sheet.reach.minVerbs << ";\n"; o << " public static final int REACH_MAX_VERBS = " << sheet.reach.maxVerbs << ";\n"; o << " public static final boolean REACH_RECEIVABLE = " << (sheet.reach.receivable ? "true" : "false") << ";\n"; o << " public static final boolean REACH_COHERENT = " << (sheet.reach.coherent ? "true" : "false") << ";\n\n"; } if (sheet.measure.present) { o << " public static final String[] MEASURE_METRICS = { " << quotedList(measureMetricNames(sheet.measure)) << " };\n"; o << " public static final int MEASURE_TIMEOUT_MS = " << sheet.measure.timeoutMs << ";\n"; o << " public static final boolean MEASURE_HONEST = true;\n\n"; } if (!sheet.subjects.empty()) { std::vector ids; for (const auto& s : sheet.subjects) ids.push_back(s.identity); o << " public static final String[] SUBJECTS = { " << quotedList(ids) << " };\n"; } if (sheet.strings.present && !sheet.strings.vars.empty()) { std::vector names, values; for (const auto& v : sheet.strings.vars) { names.push_back(v.name); values.push_back(v.value); } o << "\n public static final String[] STRING_NAMES = { " << quotedList(names) << " };\n"; o << " public static final String[] STRING_VALUES = { " << quotedEscapedList(values) << " };\n"; } o << "}\n"; return o.str(); } // ---- C manifest ----------------------------------------------------------- void emitCArray(std::ostringstream& o, const std::string& name, const std::vector& xs) { o << "const char* const " << name << "[] = { "; for (size_t i = 0; i < xs.size(); i++) { if (i) o << ", "; o << "\"" << xs[i] << "\""; } if (!xs.empty()) o << ", "; o << "0 };\n"; o << "const int " << name << "_count = " << xs.size() << ";\n"; } std::string emitC(const Sheet& sheet) { std::ostringstream o; o << "/* Generated by Nordshrift (C component manifest) */\n"; o << "const char* const NS_SHEET = \"" << sheet.meta.name << "\";\n\n"; if (sheet.network.present) { emitCArray(o, "ns_network_objects", networkObjectNames(sheet.network)); emitCArray(o, "ns_network_transports", networkTransportNames(sheet.network)); o << "const char* const ns_network_address_family = \"" << networkAddressFamilyName(sheet.network.addressFamily) << "\";\n"; o << "const int ns_network_tls = " << (sheet.network.tls ? 1 : 0) << ";\n\n"; } if (sheet.finance.present) { emitCArray(o, "ns_finance_objects", financeObjectNames(sheet.finance)); o << "const char* const ns_finance_currency = \"" << (sheet.finance.hasCurrency ? sheet.finance.currency : "") << "\";\n"; o << "const char* const ns_finance_period = \"" << financePeriodName(sheet.finance.period) << "\";\n"; o << "const char* const ns_finance_discounting = \"" << financeDiscountingName(sheet.finance.discounting) << "\";\n\n"; } if (sheet.reach.present) { emitCArray(o, "ns_reach_verbs", reachVerbNames(sheet.reach)); emitCArray(o, "ns_reach_channels", reachChannelNames(sheet.reach)); o << "const int ns_reach_min_verbs = " << sheet.reach.minVerbs << ";\n"; o << "const int ns_reach_max_verbs = " << sheet.reach.maxVerbs << ";\n"; o << "const int ns_reach_receivable = " << (sheet.reach.receivable ? 1 : 0) << ";\n"; o << "const int ns_reach_coherent = " << (sheet.reach.coherent ? 1 : 0) << ";\n\n"; } if (sheet.measure.present) { emitCArray(o, "ns_measure_metrics", measureMetricNames(sheet.measure)); o << "const int ns_measure_timeout_ms = " << sheet.measure.timeoutMs << ";\n"; o << "const int ns_measure_honest = 1;\n\n"; } if (!sheet.subjects.empty()) { std::vector ids; for (const auto& s : sheet.subjects) ids.push_back(s.identity); emitCArray(o, "ns_subjects", ids); } if (sheet.strings.present && !sheet.strings.vars.empty()) { std::vector names, values; for (const auto& v : sheet.strings.vars) { names.push_back(v.name); values.push_back(escapeForSource(v.value)); } emitCArray(o, "ns_string_names", names); // values are pre-escaped; emit directly so escapes survive verbatim. o << "const char* const ns_string_values[] = { "; for (size_t i = 0; i < values.size(); i++) { if (i) o << ", "; o << "\"" << values[i] << "\""; } if (!values.empty()) o << ", "; o << "0 };\n"; o << "const int ns_string_values_count = " << values.size() << ";\n"; } return o.str(); } // ---- Sleela manifest ------------------------------------------------------ void emitSleelaList(std::ostringstream& o, const std::string& name, const std::vector& xs) { o << " // " << name << ": "; for (size_t i = 0; i < xs.size(); i++) { if (i) o << ", "; o << xs[i]; } o << "\n"; o << " String " << name << " = \""; for (size_t i = 0; i < xs.size(); i++) { if (i) o << ","; o << xs[i]; } o << "\";\n"; } std::string emitSleela(const Sheet& sheet) { std::ostringstream o; o << "// Generated by Nordshrift (Sleela component manifest)\n"; o << "class ComponentManifest {\n"; o << " String sheet = \"" << sheet.meta.name << "\";\n"; if (sheet.network.present) { emitSleelaList(o, "networkObjects", networkObjectNames(sheet.network)); emitSleelaList(o, "networkTransports", networkTransportNames(sheet.network)); o << " String networkAddressFamily = \"" << networkAddressFamilyName(sheet.network.addressFamily) << "\";\n"; o << " boolean networkTls = " << (sheet.network.tls ? "true" : "false") << ";\n"; } if (sheet.finance.present) { emitSleelaList(o, "financeObjects", financeObjectNames(sheet.finance)); o << " String financeCurrency = \"" << (sheet.finance.hasCurrency ? sheet.finance.currency : "") << "\";\n"; o << " String financePeriod = \"" << financePeriodName(sheet.finance.period) << "\";\n"; o << " String financeDiscounting = \"" << financeDiscountingName(sheet.finance.discounting) << "\";\n"; } if (sheet.reach.present) { emitSleelaList(o, "reachVerbs", reachVerbNames(sheet.reach)); emitSleelaList(o, "reachChannels", reachChannelNames(sheet.reach)); o << " int reachMinVerbs = " << sheet.reach.minVerbs << ";\n"; o << " int reachMaxVerbs = " << sheet.reach.maxVerbs << ";\n"; o << " boolean reachReceivable = " << (sheet.reach.receivable ? "true" : "false") << ";\n"; o << " boolean reachCoherent = " << (sheet.reach.coherent ? "true" : "false") << ";\n"; } if (sheet.measure.present) { emitSleelaList(o, "measureMetrics", measureMetricNames(sheet.measure)); o << " int measureTimeoutMs = " << sheet.measure.timeoutMs << ";\n"; o << " boolean measureHonest = true;\n"; } if (!sheet.subjects.empty()) { std::vector ids; for (const auto& s : sheet.subjects) ids.push_back(s.identity); emitSleelaList(o, "subjects", ids); } if (sheet.strings.present && !sheet.strings.vars.empty()) { std::vector names; for (const auto& v : sheet.strings.vars) names.push_back(v.name); emitSleelaList(o, "stringNames", names); // one accessor-style field per declared String value (escaped). for (const auto& v : sheet.strings.vars) { o << " String string_" << v.name << " = \"" << escapeForSource(v.value) << "\";\n"; } } o << "}\n"; return o.str(); } } // namespace bool hasComponentManifest(const Sheet& sheet) { return sheet.network.present || sheet.finance.present || sheet.reach.present || sheet.measure.present || !sheet.subjects.empty() || (sheet.strings.present && !sheet.strings.vars.empty()); } std::string emitComponentManifest(const Sheet& sheet, TargetLang lang, const std::string& packageRoot) { switch (lang) { case TargetLang::Java: return emitJava(sheet, packageRoot); case TargetLang::C: return emitC(sheet); case TargetLang::Sleela: return emitSleela(sheet); } return emitSleela(sheet); } std::string summarizeComponents(const Sheet& sheet) { std::ostringstream o; if (sheet.network.present) o << " manifest: network " << sheet.network.objects.size() << " object(s), " << sheet.network.transports.size() << " transport(s)\n"; if (sheet.finance.present) o << " manifest: finance " << sheet.finance.objects.size() << " object(s)\n"; if (sheet.reach.present) o << " manifest: reach " << sheet.reach.verbs.size() << " verb(s), " << sheet.reach.channels.size() << " channel(s) [Munction]\n"; if (sheet.measure.present) o << " manifest: measure " << sheet.measure.metrics.size() << " metric(s) [Synchro]\n"; if (!sheet.subjects.empty()) o << " manifest: " << sheet.subjects.size() << " subject(s)\n"; if (sheet.strings.present && !sheet.strings.vars.empty()) o << " manifest: strings " << sheet.strings.vars.size() << " value(s)\n"; return o.str(); } } // namespace nordshrift