Module: Citrus::Toml::Merge

Defined in:
lib/citrus/toml/merge.rb,
lib/citrus/toml/merge/version.rb,
sig/citrus/toml/merge.rbs

Defined Under Namespace

Modules: Version Classes: 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.

Returns:

  • (String)
Version::VERSION

Class Method Summary collapse

Class Method Details

.available_toml_backendsObject



33
34
35
# File 'lib/citrus/toml/merge.rb', line 33

def available_toml_backends
  [BACKEND]
end

.match_toml_owners(template, destination) ⇒ Object



69
70
71
# File 'lib/citrus/toml/merge.rb', line 69

def match_toml_owners(template, destination)
  ::Toml::Merge.match_toml_owners(template, destination)
end

.merge_toml(template_source, destination_source, dialect, backend: nil) ⇒ Object



73
74
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
# File 'lib/citrus/toml/merge.rb', line 73

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.message }],
    policies: [] }
rescue ::Toml::Merge::DestinationParseError => e
  { ok: false, diagnostics: [{ severity: 'error', category: 'destination_parse_error', message: e.message }],
    policies: [] }
rescue StandardError => e
  { ok: false, diagnostics: [{ severity: 'error', category: 'merge_error', message: e.message }], policies: [] }
end

.parse_toml(source, dialect, backend: nil) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/citrus/toml/merge.rb', line 61

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



116
117
118
119
120
121
# File 'lib/citrus/toml/merge.rb', line 116

def provider_parse_failure(category, parse_result)
  diagnostics = Array(parse_result[:diagnostics])
  message = diagnostics.map { |diagnostic| diagnostic[:message] || diagnostic['message'] }.compact.join('; ')
  message = 'provider parse failed' if message.empty?
  { ok: false, diagnostics: [{ severity: 'error', category: category.to_s, message: message }], policies: [] }
end

.register_backend!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/citrus/toml/merge.rb', line 15

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

.toml_backend_feature_profile(backend: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/citrus/toml/merge.rb', line 37

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_profileObject



29
30
31
# File 'lib/citrus/toml/merge.rb', line 29

def toml_feature_profile
  ::Toml::Merge.toml_feature_profile
end

.toml_plan_context(backend: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/citrus/toml/merge.rb', line 47

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



108
109
110
111
112
113
114
# File 'lib/citrus/toml/merge.rb', line 108

def unsupported_feature_result(message)
  {
    ok: false,
    diagnostics: [{ severity: 'error', category: 'unsupported_feature', message: message }],
    policies: []
  }
end