Module: Citrus::Toml::Merge
- Defined in:
- lib/citrus/toml/merge/provider.rb,
lib/citrus/toml/merge.rb,
lib/citrus/toml/merge/version.rb,
sig/citrus/toml/merge.rbs
Overview
Citrus-backed TOML structural merge provider integration.
Defined Under Namespace
Modules: Version Classes: Provider, SmartMerger
Constant Summary collapse
- PACKAGE_NAME =
'citrus-toml-merge'- BACKEND =
TreeHaver::CITRUS_BACKEND
- BACKEND_REGISTRY =
Struct.new(:registered, :mutex).new(false, Mutex.new)
- VERSION =
Current gem version exposed at the traditional constant location.
Version::VERSION
Class Method Summary collapse
- .available_toml_backends ⇒ Object
- .match_toml_owners(template, destination) ⇒ Object
- .merge_provider ⇒ Provider
- .merge_toml(template_source, destination_source, dialect, backend: nil) ⇒ Object
- .parse_toml(source, dialect, backend: nil) ⇒ Object
- .provider_parse_failure(category, parse_result) ⇒ Object
- .register_backend! ⇒ Object
- .register_provider!(replace: false) ⇒ Object
- .toml_backend_feature_profile(backend: nil) ⇒ Object
- .toml_feature_profile ⇒ Object
- .toml_plan_context(backend: nil) ⇒ Object
- .unsupported_feature_result(message) ⇒ Object
Class Method Details
.available_toml_backends ⇒ Object
35 36 37 |
# File 'lib/citrus/toml/merge.rb', line 35 def available_toml_backends [BACKEND] end |
.match_toml_owners(template, destination) ⇒ Object
71 72 73 |
# File 'lib/citrus/toml/merge.rb', line 71 def match_toml_owners(template, destination) ::Toml::Merge.match_toml_owners(template, destination) end |
.merge_provider ⇒ Provider
18 19 20 |
# File 'lib/citrus/toml/merge/provider.rb', line 18 def merge_provider @merge_provider ||= Provider.new end |
.merge_toml(template_source, destination_source, dialect, backend: nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/citrus/toml/merge.rb', line 75 def merge_toml(template_source, destination_source, dialect, backend: nil) requested = backend.to_s.empty? ? BACKEND.id : backend.to_s return unsupported_feature_result("Unsupported TOML backend #{requested}.") unless requested == BACKEND.id return unsupported_feature_result("Unsupported TOML dialect #{dialect}.") unless dialect == 'toml' template_parse = parse_toml(template_source, dialect, backend: BACKEND.id) return provider_parse_failure(:template_parse_error, template_parse) unless template_parse[:ok] destination_parse = parse_toml(destination_source, dialect, backend: BACKEND.id) return provider_parse_failure(:destination_parse_error, destination_parse) unless destination_parse[:ok] output = SmartMerger.new( template_source, destination_source, preference: :destination, add_template_only_nodes: true ).merge_result.to_toml { ok: true, diagnostics: [], output: output, policies: [::Toml::Merge::DESTINATION_WINS_ARRAY_POLICY] } rescue ::Toml::Merge::TemplateParseError => e { ok: false, diagnostics: [{ severity: 'error', category: 'template_parse_error', message: e. }], policies: [] } rescue ::Toml::Merge::DestinationParseError => e { ok: false, diagnostics: [{ severity: 'error', category: 'destination_parse_error', message: e. }], policies: [] } rescue StandardError => e { ok: false, diagnostics: [{ severity: 'error', category: 'merge_error', message: e. }], policies: [] } end |
.parse_toml(source, dialect, backend: nil) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/citrus/toml/merge.rb', line 63 def parse_toml(source, dialect, backend: nil) requested = backend.to_s.empty? ? BACKEND.id : backend.to_s return unsupported_feature_result("Unsupported TOML backend #{requested}.") unless requested == BACKEND.id return unsupported_feature_result("Unsupported TOML dialect #{dialect}.") unless dialect == 'toml' TreeHaver.with_backend(BACKEND.id) { ::Toml::Merge.analyze_toml_source(source, dialect) } end |
.provider_parse_failure(category, parse_result) ⇒ Object
118 119 120 121 122 123 |
# File 'lib/citrus/toml/merge.rb', line 118 def provider_parse_failure(category, parse_result) diagnostics = Array(parse_result[:diagnostics]) = diagnostics.map { |diagnostic| diagnostic[:message] || diagnostic['message'] }.compact.join('; ') = 'provider parse failed' if .empty? { ok: false, diagnostics: [{ severity: 'error', category: category.to_s, message: }], policies: [] } end |
.register_backend! ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/citrus/toml/merge.rb', line 17 def register_backend! BACKEND_REGISTRY.mutex.synchronize do return if BACKEND_REGISTRY.registered TreeHaver.register_language( :toml, grammar_module: TomlRB::Document, gem_name: 'toml-rb' ) BACKEND_REGISTRY.registered = true end end |
.register_provider!(replace: false) ⇒ Object
22 23 24 |
# File 'lib/citrus/toml/merge/provider.rb', line 22 def register_provider!(replace: false) Ast::Merge.register_provider(merge_provider, replace: replace) end |
.toml_backend_feature_profile(backend: nil) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/citrus/toml/merge.rb', line 39 def toml_backend_feature_profile(backend: nil) requested = backend.to_s.empty? ? BACKEND.id : backend.to_s return unsupported_feature_result("Unsupported TOML backend #{requested}.") unless requested == BACKEND.id toml_feature_profile.merge( backend: BACKEND.id, backend_ref: BACKEND.to_h ) end |
.toml_feature_profile ⇒ Object
31 32 33 |
# File 'lib/citrus/toml/merge.rb', line 31 def toml_feature_profile ::Toml::Merge.toml_feature_profile end |
.toml_plan_context(backend: nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/citrus/toml/merge.rb', line 49 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 |
.unsupported_feature_result(message) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/citrus/toml/merge.rb', line 110 def unsupported_feature_result() { ok: false, diagnostics: [{ severity: 'error', category: 'unsupported_feature', message: }], policies: [] } end |