@@ -46,10 +46,7 @@ class Protofier {
46
46
..text = value.text
47
47
..quoted = value.hasQuotes;
48
48
} else if (value is sass.SassNumber ) {
49
- var number = Value_Number ()..value = value.value * 1.0 ;
50
- number.numerators.addAll (value.numeratorUnits);
51
- number.denominators.addAll (value.denominatorUnits);
52
- result.number = number;
49
+ result.number = _protofyNumber (value);
53
50
} else if (value is sass.SassColor ) {
54
51
if (value.hasCalculatedHsl) {
55
52
result.hslColor = Value_HslColor ()
@@ -88,6 +85,8 @@ class Protofier {
88
85
..value = protofy (value));
89
86
});
90
87
result.map = map;
88
+ } else if (value is sass.SassCalculation ) {
89
+ result.calculation = _protofyCalculation (value);
91
90
} else if (value is sass.SassFunction ) {
92
91
result.compilerFunction = _functions.protofy (value);
93
92
} else if (value == sass.sassTrue) {
@@ -102,6 +101,14 @@ class Protofier {
102
101
return result;
103
102
}
104
103
104
+ /// Converts [number] to its protocol buffer representation.
105
+ Value_Number _protofyNumber (sass.SassNumber number) {
106
+ var value = Value_Number ()..value = number.value * 1.0 ;
107
+ value.numerators.addAll (number.numeratorUnits);
108
+ value.denominators.addAll (number.denominatorUnits);
109
+ return value;
110
+ }
111
+
105
112
/// Converts [separator] to its protocol buffer representation.
106
113
ListSeparator _protofySeparator (sass.ListSeparator separator) {
107
114
switch (separator) {
@@ -118,6 +125,55 @@ class Protofier {
118
125
}
119
126
}
120
127
128
+ /// Converts [calculation] to its protocol buffer representation.
129
+ Value_Calculation _protofyCalculation (sass.SassCalculation calculation) =>
130
+ Value_Calculation ()
131
+ ..name = calculation.name
132
+ ..arguments.addAll ([
133
+ for (var argument in calculation.arguments)
134
+ _protofyCalculationValue (argument)
135
+ ]);
136
+
137
+ /// Converts a calculation value that appears within a `SassCalculation` to
138
+ /// its protocol buffer representation.
139
+ Value_Calculation_CalculationValue _protofyCalculationValue (Object value) {
140
+ var result = Value_Calculation_CalculationValue ();
141
+ if (value is sass.SassNumber ) {
142
+ result.number = _protofyNumber (value);
143
+ } else if (value is sass.SassCalculation ) {
144
+ result.calculation = _protofyCalculation (value);
145
+ } else if (value is sass.SassString ) {
146
+ result.string = value.text;
147
+ } else if (value is sass.CalculationOperation ) {
148
+ result.operation = Value_Calculation_CalculationOperation ()
149
+ ..operator = _protofyCalculationOperator (value.operator )
150
+ ..left = _protofyCalculationValue (value.left)
151
+ ..right = _protofyCalculationValue (value.right);
152
+ } else if (value is sass.CalculationInterpolation ) {
153
+ result.interpolation = value.value;
154
+ } else {
155
+ throw "Unknown calculation value $value " ;
156
+ }
157
+ return result;
158
+ }
159
+
160
+ /// Converts [operator] to its protocol buffer representation.
161
+ CalculationOperator _protofyCalculationOperator (
162
+ sass.CalculationOperator operator ) {
163
+ switch (operator ) {
164
+ case sass.CalculationOperator .plus:
165
+ return CalculationOperator .PLUS ;
166
+ case sass.CalculationOperator .minus:
167
+ return CalculationOperator .MINUS ;
168
+ case sass.CalculationOperator .times:
169
+ return CalculationOperator .TIMES ;
170
+ case sass.CalculationOperator .dividedBy:
171
+ return CalculationOperator .DIVIDE ;
172
+ default :
173
+ throw "Unknown CalculationOperator $operator " ;
174
+ }
175
+ }
176
+
121
177
/// Converts [response] 's return value to its Sass representation.
122
178
sass.Value deprotofyResponse (InboundMessage_FunctionCallResponse response) {
123
179
for (var id in response.accessedArgumentLists) {
@@ -138,9 +194,7 @@ class Protofier {
138
194
: sass.SassString (value.string.text, quotes: value.string.quoted);
139
195
140
196
case Value_Value .number:
141
- return sass.SassNumber .withUnits (value.number.value,
142
- numeratorUnits: value.number.numerators,
143
- denominatorUnits: value.number.denominators);
197
+ return _deprotofyNumber (value.number);
144
198
145
199
case Value_Value .rgbColor:
146
200
return sass.SassColor .rgb (value.rgbColor.red, value.rgbColor.green,
@@ -221,6 +275,9 @@ class Protofier {
221
275
_compilationId, value.hostFunction.signature,
222
276
id: value.hostFunction.id));
223
277
278
+ case Value_Value .calculation:
279
+ return _deprotofyCalculation (value.calculation);
280
+
224
281
case Value_Value .singleton:
225
282
switch (value.singleton) {
226
283
case SingletonValue .TRUE :
@@ -254,6 +311,12 @@ class Protofier {
254
311
}
255
312
}
256
313
314
+ /// Converts [number] to its Sass representation.
315
+ sass.SassNumber _deprotofyNumber (Value_Number number) =>
316
+ sass.SassNumber .withUnits (number.value,
317
+ numeratorUnits: number.numerators,
318
+ denominatorUnits: number.denominators);
319
+
257
320
/// Returns the argument list in [_argumentLists] that corresponds to [id] .
258
321
sass.SassArgumentList _argumentListForId (int id) {
259
322
if (id < 1 ) {
@@ -283,4 +346,94 @@ class Protofier {
283
346
throw "Unknown separator $separator " ;
284
347
}
285
348
}
349
+
350
+ /// Converts [calculation] to its Sass representation.
351
+ sass.Value _deprotofyCalculation (Value_Calculation calculation) {
352
+ if (calculation.name == "calc" ) {
353
+ if (calculation.arguments.length != 1 ) {
354
+ throw paramsError (
355
+ "Value.Calculation.arguments must have exactly one argument for "
356
+ "calc()." );
357
+ }
358
+
359
+ return sass.SassCalculation .calc (
360
+ _deprotofyCalculationValue (calculation.arguments[0 ]));
361
+ } else if (calculation.name == "clamp" ) {
362
+ if (calculation.arguments.length != 3 ) {
363
+ throw paramsError (
364
+ "Value.Calculation.arguments must have exactly 3 arguments for "
365
+ "clamp()." );
366
+ }
367
+
368
+ return sass.SassCalculation .clamp (
369
+ _deprotofyCalculationValue (calculation.arguments[0 ]),
370
+ _deprotofyCalculationValue (calculation.arguments[1 ]),
371
+ _deprotofyCalculationValue (calculation.arguments[2 ]));
372
+ } else if (calculation.name == "min" ) {
373
+ if (calculation.arguments.isEmpty) {
374
+ throw paramsError (
375
+ "Value.Calculation.arguments must have at least 1 argument for "
376
+ "min()." );
377
+ }
378
+
379
+ return sass.SassCalculation .min (
380
+ calculation.arguments.map (_deprotofyCalculationValue));
381
+ } else if (calculation.name == "max" ) {
382
+ if (calculation.arguments.isEmpty) {
383
+ throw paramsError (
384
+ "Value.Calculation.arguments must have at least 1 argument for "
385
+ "max()." );
386
+ }
387
+
388
+ return sass.SassCalculation .max (
389
+ calculation.arguments.map (_deprotofyCalculationValue));
390
+ } else {
391
+ throw paramsError (
392
+ 'Value.Calculation.name "${calculation .name }" is not a recognized '
393
+ 'calculation type.' );
394
+ }
395
+ }
396
+
397
+ /// Converts [value] to its Sass representation.
398
+ Object _deprotofyCalculationValue (Value_Calculation_CalculationValue value) {
399
+ switch (value.whichValue ()) {
400
+ case Value_Calculation_CalculationValue_Value .number:
401
+ return _deprotofyNumber (value.number);
402
+
403
+ case Value_Calculation_CalculationValue_Value .calculation:
404
+ return _deprotofyCalculation (value.calculation);
405
+
406
+ case Value_Calculation_CalculationValue_Value .string:
407
+ return sass.SassString (value.string, quotes: false );
408
+
409
+ case Value_Calculation_CalculationValue_Value .operation:
410
+ return sass.SassCalculation .operate (
411
+ _deprotofyCalculationOperator (value.operation.operator ),
412
+ _deprotofyCalculationValue (value.operation.left),
413
+ _deprotofyCalculationValue (value.operation.right));
414
+
415
+ case Value_Calculation_CalculationValue_Value .interpolation:
416
+ return sass.CalculationInterpolation (value.interpolation);
417
+
418
+ case Value_Calculation_CalculationValue_Value .notSet:
419
+ throw mandatoryError ("Value.Calculation.value" );
420
+ }
421
+ }
422
+
423
+ /// Converts [operator] to its Sass representation.
424
+ sass.CalculationOperator _deprotofyCalculationOperator (
425
+ CalculationOperator operator ) {
426
+ switch (operator ) {
427
+ case CalculationOperator .PLUS :
428
+ return sass.CalculationOperator .plus;
429
+ case CalculationOperator .MINUS :
430
+ return sass.CalculationOperator .minus;
431
+ case CalculationOperator .TIMES :
432
+ return sass.CalculationOperator .times;
433
+ case CalculationOperator .DIVIDE :
434
+ return sass.CalculationOperator .dividedBy;
435
+ default :
436
+ throw "Unknown CalculationOperator $operator " ;
437
+ }
438
+ }
286
439
}
0 commit comments