Module: Toml::Merge
- Defined in:
- lib/toml/merge.rb,
lib/toml/merge/version.rb
Defined Under Namespace
Modules: Version Classes: ParseError
Constant Summary collapse
- PACKAGE_NAME =
"toml-merge"- DESTINATION_WINS_ARRAY_POLICY =
{ surface: "array", name: "destination_wins_array" }.freeze
- VERSION =
Version::VERSION
Class Method Summary collapse
- .analyze_toml_source(source, dialect) ⇒ Object
- .available_toml_backends ⇒ Object
- .match_toml_owners(template, destination) ⇒ Object
- .merge_toml(template_source, destination_source, dialect, backend: nil) ⇒ Object
- .merge_toml_with_parser(template_source, destination_source, dialect, &parser) ⇒ Object
- .parse_toml(source, dialect, backend: nil) ⇒ Object
- .toml_backend_feature_profile(backend: nil) ⇒ Object
- .toml_feature_profile ⇒ Object
- .toml_plan_context(backend: nil) ⇒ Object
Class Method Details
.analyze_toml_source(source, dialect) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/toml/merge.rb', line 54 def analyze_toml_source(source, dialect) return unsupported_feature_result("Unsupported TOML dialect #{dialect}.") unless dialect == "toml" parsed = parse_toml_document(source) { ok: true, diagnostics: [], analysis: { kind: "toml", dialect: "toml", normalized_source: canonical_toml(parsed), root_kind: "table", owners: collect_toml_owners(parsed) }, policies: [] } rescue StandardError => e parse_error_result(e.) end |
.available_toml_backends ⇒ Object
26 27 28 |
# File 'lib/toml/merge.rb', line 26 def available_toml_backends [TreeHaver::KREUZBERG_LANGUAGE_PACK_BACKEND] end |
.match_toml_owners(template, destination) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/toml/merge.rb', line 88 def match_toml_owners(template, destination) destination_paths = destination[:owners].to_h { |owner| [owner[:path], true] } template_paths = template[:owners].to_h { |owner| [owner[:path], true] } { matched: template[:owners] .filter { |owner| destination_paths[owner[:path]] } .map { |owner| { template_path: owner[:path], destination_path: owner[:path] } }, unmatched_template: template[:owners].map { |owner| owner[:path] }.reject { |path| destination_paths[path] }, unmatched_destination: destination[:owners].map { |owner| owner[:path] }.reject { |path| template_paths[path] } } end |
.merge_toml(template_source, destination_source, dialect, backend: nil) ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'lib/toml/merge.rb', line 135 def merge_toml(template_source, destination_source, dialect, backend: nil) resolved_backend = resolve_backend(backend) return unsupported_feature_result("Unsupported TOML backend #{resolved_backend}.") unless resolved_backend == TreeHaver::KREUZBERG_LANGUAGE_PACK_BACKEND.id merge_toml_with_parser(template_source, destination_source, dialect) do |source, parse_dialect| parse_toml(source, parse_dialect, backend: resolved_backend) end end |
.merge_toml_with_parser(template_source, destination_source, dialect, &parser) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/toml/merge.rb', line 101 def merge_toml_with_parser(template_source, destination_source, dialect, &parser) template = parser.call(template_source, dialect) return { ok: false, diagnostics: template[:diagnostics], policies: [] } unless template[:ok] destination = parser.call(destination_source, dialect) unless destination[:ok] return { ok: false, diagnostics: destination[:diagnostics].map do |diagnostic| diagnostic[:category] == "parse_error" ? diagnostic.merge(category: "destination_parse_error") : diagnostic end, policies: [] } end merged = merge_toml_tables( parse_toml_document(template.dig(:analysis, :normalized_source)), parse_toml_document(destination.dig(:analysis, :normalized_source)) ) { ok: true, diagnostics: [], output: canonical_toml(merged), policies: [DESTINATION_WINS_ARRAY_POLICY] } rescue StandardError => e { ok: false, diagnostics: [{ severity: "error", category: "destination_parse_error", message: e. }], policies: [] } end |
.parse_toml(source, dialect, backend: nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/toml/merge.rb', line 74 def parse_toml(source, dialect, backend: nil) return unsupported_feature_result("Unsupported TOML dialect #{dialect}.") unless dialect == "toml" resolved_backend = resolve_backend(backend) return unsupported_feature_result("Unsupported TOML backend #{resolved_backend}.") unless resolved_backend == TreeHaver::KREUZBERG_LANGUAGE_PACK_BACKEND.id syntax_result = TreeHaver.parse_with_language_pack( TreeHaver::ParserRequest.new(source: source, language: "toml", dialect: dialect) ) return { ok: false, diagnostics: syntax_result[:diagnostics] } unless syntax_result[:ok] analyze_toml_source(source, dialect) end |
.toml_backend_feature_profile(backend: nil) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/toml/merge.rb', line 30 def toml_backend_feature_profile(backend: nil) resolved_backend = resolve_backend(backend) return unsupported_feature_result("Unsupported TOML backend #{resolved_backend}.") unless resolved_backend == TreeHaver::KREUZBERG_LANGUAGE_PACK_BACKEND.id toml_feature_profile.merge( backend: TreeHaver::KREUZBERG_LANGUAGE_PACK_BACKEND.id, backend_ref: TreeHaver::KREUZBERG_LANGUAGE_PACK_BACKEND.to_h ) end |
.toml_feature_profile ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/toml/merge.rb', line 18 def toml_feature_profile { family: "toml", supported_dialects: ["toml"], supported_policies: [DESTINATION_WINS_ARRAY_POLICY] } end |
.toml_plan_context(backend: nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/toml/merge.rb', line 40 def toml_plan_context(backend: nil) profile = toml_backend_feature_profile(backend: backend) return profile if profile[:ok] == false { family_profile: toml_feature_profile, feature_profile: { backend: profile[:backend], supports_dialects: false, supported_policies: profile[:supported_policies] } } end |