summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/sass-embedded-1.101.0-arm64-darwin/lib/sass/compiler/session/protofier.rb
blob: 47d6688e31365f85f7a3c9e8aa6cf13403b7394b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# frozen_string_literal: true

module Sass
  class Compiler
    class Session
      # The {Protofier} class.
      #
      # It converts Pure Ruby types and Protobuf Ruby types.
      class Protofier
        def initialize(function_registry)
          @function_registry = function_registry
        end

        def to_proto(obj)
          case obj
          when Sass::Value::String
            EmbeddedProtocol::Value.new(
              string: EmbeddedProtocol::Value::String.new(
                text: obj.text.to_str,
                quoted: obj.quoted?
              )
            )
          when Sass::Value::Number
            EmbeddedProtocol::Value.new(
              number: Number.to_proto(obj)
            )
          when Sass::Value::Color
            EmbeddedProtocol::Value.new(
              color: EmbeddedProtocol::Value::Color.new(
                channel1: obj.send(:channel0_or_nil),
                channel2: obj.send(:channel1_or_nil),
                channel3: obj.send(:channel2_or_nil),
                alpha: obj.send(:alpha_or_nil),
                space: obj.space
              )
            )
          when Sass::Value::ArgumentList
            if obj.instance_variable_get(:@compile_context) == @function_registry.compile_context
              EmbeddedProtocol::Value.new(
                argument_list: EmbeddedProtocol::Value::ArgumentList.new(
                  id: obj.instance_variable_get(:@id)
                )
              )
            else
              EmbeddedProtocol::Value.new(
                argument_list: EmbeddedProtocol::Value::ArgumentList.new(
                  contents: obj.to_a.map { |element| to_proto(element) },
                  keywords: obj.keywords.each_with_object({}) { |(key, value), hash| hash[key.to_s] = to_proto(value) },
                  separator: ListSeparator.to_proto(obj.separator)
                )
              )
            end
          when Sass::Value::List
            EmbeddedProtocol::Value.new(
              list: EmbeddedProtocol::Value::List.new(
                contents: obj.to_a.map { |element| to_proto(element) },
                separator: ListSeparator.to_proto(obj.separator),
                has_brackets: obj.bracketed?
              )
            )
          when Sass::Value::Map
            EmbeddedProtocol::Value.new(
              map: EmbeddedProtocol::Value::Map.new(
                entries: obj.contents.map do |key, value|
                  EmbeddedProtocol::Value::Map::Entry.new(
                    key: to_proto(key),
                    value: to_proto(value)
                  )
                end
              )
            )
          when Sass::Value::Function
            if obj.instance_variable_defined?(:@id)
              EmbeddedProtocol::Value.new(
                compiler_function: EmbeddedProtocol::Value::CompilerFunction.new(
                  id: assert_compiler_value(obj).instance_variable_get(:@id)
                )
              )
            else
              EmbeddedProtocol::Value.new(
                host_function: EmbeddedProtocol::Value::HostFunction.new(
                  id: @function_registry.register(obj.callback),
                  signature: obj.signature
                )
              )
            end
          when Sass::Value::Mixin
            EmbeddedProtocol::Value.new(
              compiler_mixin: EmbeddedProtocol::Value::CompilerMixin.new(
                id: assert_compiler_value(obj).instance_variable_get(:@id)
              )
            )
          when Sass::Value::Calculation
            EmbeddedProtocol::Value.new(
              calculation: Calculation.to_proto(obj)
            )
          when Sass::Value::Boolean
            EmbeddedProtocol::Value.new(
              singleton: obj.value ? :TRUE : :FALSE
            )
          when Sass::Value::Null
            EmbeddedProtocol::Value.new(
              singleton: :NULL
            )
          else
            raise Sass::ScriptError, "Unknown Sass::Value #{obj}"
          end
        end

        def from_proto(proto)
          oneof = proto.value
          obj = proto.public_send(oneof)
          case oneof
          when :string
            Sass::Value::String.new(
              obj.text,
              quoted: obj.quoted
            )
          when :number
            Number.from_proto(obj)
          when :color
            Sass::Value::Color.send(
              :for_space,
              obj.space,
              obj.has_channel1? ? obj.channel1 : nil,
              obj.has_channel2? ? obj.channel2 : nil,
              obj.has_channel3? ? obj.channel3 : nil,
              obj.has_alpha? ? obj.alpha : nil
            )
          when :argument_list
            compiler_value(
              Sass::Value::ArgumentList.new(
                obj.contents.map do |element|
                  from_proto(element)
                end,
                obj.keywords.to_enum.with_object({}) do |(key, value), hash|
                  hash[key.to_sym] = from_proto(value)
                end,
                ListSeparator.from_proto(obj.separator)
              ),
              obj.id
            )
          when :list
            Sass::Value::List.new(
              obj.contents.map do |element|
                from_proto(element)
              end,
              separator: ListSeparator.from_proto(obj.separator),
              bracketed: obj.has_brackets
            )
          when :map
            Sass::Value::Map.new(
              obj.entries.to_enum.with_object({}) do |entry, hash|
                hash[from_proto(entry.key)] = from_proto(entry.value)
              end
            )
          when :compiler_function
            compiler_value(Sass::Value::Function.allocate, obj.id)
          when :host_function
            raise Sass::ScriptError, 'The compiler may not send Value.host_function to host'
          when :compiler_mixin
            compiler_value(Sass::Value::Mixin.allocate, obj.id)
          when :calculation
            Calculation.from_proto(obj)
          when :singleton
            case obj
            when :TRUE
              Sass::Value::Boolean::TRUE
            when :FALSE
              Sass::Value::Boolean::FALSE
            when :NULL
              Sass::Value::Null::NULL
            else
              raise Sass::ScriptError, "Unknown Value.singleton #{obj}"
            end
          else
            raise Sass::ScriptError, "Unknown Value.value #{obj}"
          end
        end

        private

        def assert_compiler_value(value)
          unless value.instance_variable_get(:@compile_context) == @function_registry.compile_context
            raise Sass::ScriptError, "Value #{value} does not belong to this compilation"
          end

          value
        end

        def compiler_value(value, id)
          value.instance_variable_set(:@compile_context, @function_registry.compile_context)
          value.instance_variable_set(:@id, id)
          value
        end

        # The {Number} Protofier.
        module Number
          module_function

          def to_proto(obj)
            EmbeddedProtocol::Value::Number.new(
              value: obj.value.to_f,
              numerators: obj.numerator_units,
              denominators: obj.denominator_units
            )
          end

          def from_proto(obj)
            Sass::Value::Number.new(
              obj.value, {
                numerator_units: obj.numerators.to_a,
                denominator_units: obj.denominators.to_a
              }
            )
          end
        end

        private_constant :Number

        # The {Calculation} Protofier.
        module Calculation
          module_function

          def to_proto(obj)
            EmbeddedProtocol::Value::Calculation.new(
              name: obj.name,
              arguments: obj.arguments.map { |argument| CalculationValue.to_proto(argument) }
            )
          end

          def from_proto(obj)
            Sass::Value::Calculation.send(
              :new,
              obj.name,
              obj.arguments.map { |argument| CalculationValue.from_proto(argument) }
            )
          end
        end

        private_constant :Calculation

        # The {CalculationValue} Protofier.
        module CalculationValue
          module_function

          def to_proto(value)
            case value
            when Sass::Value::Number
              EmbeddedProtocol::Value::Calculation::CalculationValue.new(
                number: Number.to_proto(value)
              )
            when Sass::Value::Calculation
              EmbeddedProtocol::Value::Calculation::CalculationValue.new(
                calculation: Calculation.to_proto(value)
              )
            when Sass::Value::String
              EmbeddedProtocol::Value::Calculation::CalculationValue.new(
                string: value.text
              )
            when Sass::CalculationValue::CalculationOperation
              EmbeddedProtocol::Value::Calculation::CalculationValue.new(
                operation: EmbeddedProtocol::Value::Calculation::CalculationOperation.new(
                  operator: CalculationOperator.to_proto(value.operator),
                  left: to_proto(value.left),
                  right: to_proto(value.right)
                )
              )
            else
              raise Sass::ScriptError, "Unknown CalculationValue #{value}"
            end
          end

          def from_proto(value)
            oneof = value.value
            obj = value.public_send(oneof)
            case oneof
            when :number
              Number.from_proto(obj)
            when :calculation
              Calculation.from_proto(obj)
            when :string
              Sass::Value::String.new(obj, quoted: false)
            when :operation
              Sass::CalculationValue::CalculationOperation.new(
                CalculationOperator.from_proto(obj.operator),
                from_proto(obj.left),
                from_proto(obj.right)
              )
            else
              raise Sass::ScriptError, "Unknown CalculationValue #{value}"
            end
          end
        end

        private_constant :CalculationValue

        # The {CalculationOperator} Protofier.
        module CalculationOperator
          module_function

          def to_proto(operator)
            case operator
            when '+'
              :PLUS
            when '-'
              :MINUS
            when '*'
              :TIMES
            when '/'
              :DIVIDE
            else
              raise Sass::ScriptError, "Unknown CalculationOperator #{separator}"
            end
          end

          def from_proto(operator)
            case operator
            when :PLUS
              '+'
            when :MINUS
              '-'
            when :TIMES
              '*'
            when :DIVIDE
              '/'
            else
              raise Sass::ScriptError, "Unknown CalculationOperator #{separator}"
            end
          end
        end

        private_constant :CalculationOperator

        # The {ListSeparator} Protofier.
        module ListSeparator
          module_function

          def to_proto(separator)
            case separator
            when ','
              :COMMA
            when ' '
              :SPACE
            when '/'
              :SLASH
            when nil
              :UNDECIDED
            else
              raise Sass::ScriptError, "Unknown ListSeparator #{separator}"
            end
          end

          def from_proto(separator)
            case separator
            when :COMMA
              ','
            when :SPACE
              ' '
            when :SLASH
              '/'
            when :UNDECIDED
              nil
            else
              raise Sass::ScriptError, "Unknown ListSeparator #{separator}"
            end
          end
        end

        private_constant :ListSeparator
      end

      private_constant :Protofier
    end
  end
end