Module: Parslet::Toml::Merge

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

Overview

Parslet-backed TOML structural merge provider integration.

Defined Under Namespace

Modules: Version Classes: Provider, SmartMerger

Constant Summary collapse

PACKAGE_NAME =
'parslet-toml-merge'
BACKEND =
TreeHaver::PARSLET_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



35
36
37
# File 'lib/parslet/toml/merge.rb', line 35

def available_toml_backends
  [BACKEND]
end

.match_toml_owners(template, destination) ⇒ Object



71
72
73
# File 'lib/parslet/toml/merge.rb', line 71

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

.merge_providerProvider

Returns:



18
19
20
# File 'lib/parslet/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/parslet/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.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



63
64
65
66
67
68
69
# File 'lib/parslet/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/parslet/toml/merge.rb', line 118

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



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/parslet/toml/merge.rb', line 17

def register_backend!
  BACKEND_REGISTRY.mutex.synchronize do
    return if BACKEND_REGISTRY.registered

    TreeHaver.register_language(
      :toml,
      grammar_class: TOML::Parslet,
      gem_name: 'toml'
    )

    BACKEND_REGISTRY.registered = true
  end
end

.register_provider!(replace: false) ⇒ Object

Parameters:

  • replace: (Boolean) (defaults to: false)

Returns:

  • (Object)


22
23
24
# File 'lib/parslet/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/parslet/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_profileObject



31
32
33
# File 'lib/parslet/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/parslet/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/parslet/toml/merge.rb', line 110

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