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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use std::{
  env, fs,
  path::{Path, PathBuf},
  time::{Duration, SystemTime},
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use urlencoding::decode;

use crate::{
  legacy::url_to_file_path_cross_platform, CompileResult, Options,
  StringOptions, Syntax, Url,
};
pub use crate::{BoxLogger, Logger, OutputStyle};

use super::{
  BoxLegacyImporter, LegacyImporter, END_OF_LOAD_PROTOCOL,
  LEGACY_IMPORTER_PROTOCOL,
};

/// The platform-specific file delimiter, ';' for windows.
#[cfg(target_family = "windows")]
pub const PATH_DELIMITER: &str = ";";
/// The platform-specific file delimiter, ':' for unix.
#[cfg(target_family = "unix")]
pub const PATH_DELIMITER: &str = ":";

/// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyFileOptions#indentType)
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum IndentType {
  /// Space IndentType.
  Space,
  /// Tab IndentType.
  Tab,
}

impl Default for IndentType {
  fn default() -> Self {
    Self::Space
  }
}

/// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyFileOptions#linefeed)
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum LineFeed {
  /// 'cr' uses U+000D CARRIAGE RETURN.
  CR,
  /// 'crlf' uses U+000D CARRIAGE RETURN followed by U+000A LINE FEED.
  CRLF,
  /// 'lf' uses U+000A LINE FEED.
  LF,
  /// 'lfcr' uses U+000A LINE FEED followed by U+000D CARRIAGE RETURN.
  LFCR,
}

impl Default for LineFeed {
  fn default() -> Self {
    Self::LF
  }
}

/// A partially-constructed [LegacyResult] object.
#[derive(Debug, Clone)]
pub struct LegacyPluginThisOptionsResult {
  /// Partial information about the compilation in progress.
  pub stats: LegacyPluginThisOptionsResultStats,
}

/// Partial information about the compilation in progress.
#[derive(Debug, Clone)]
pub struct LegacyPluginThisOptionsResultStats {
  /// The number of milliseconds between 1 January 1970 at 00:00:00 UTC and
  /// the time at which Sass compilation began.
  pub start: SystemTime,
  /// [LegacyOptions.file] if it was passed, otherwise the string `"data"`.
  pub entry: String,
}

/// A partial representation of the options passed to [Sass::render].
#[derive(Debug, Clone)]
pub struct LegacyPluginThisOptions {
  /// The value passed to [LegacyOptions.file].
  pub file: Option<PathBuf>,
  /// The value passed to [LegacyOptions.data].
  pub data: Option<String>,
  /// The value passed to [LegacyOptions.include_paths] separated by
  /// `";"` on Windows or `":"` on other operating systems. This always
  /// includes the current working directory as the first entry.
  pub include_paths: String,
  /// Always the number 10.
  pub precision: u8,
  /// Always the number 1.
  pub style: u8,
  /// The value passed to [LegacyOptions.indent_type], [IndentType::Space] otherwise.
  pub indent_type: IndentType,
  /// The value passed to [LegacyOptions.indent_width], or `2` otherwise.
  pub indent_width: usize,
  /// The value passed to [LegacyOptions.linefeed], or `"\n"` otherwise.
  pub linefeed: LineFeed,
  /// A partially-constructed [LegacyResult] object.
  pub result: LegacyPluginThisOptionsResult,
}

/// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyPluginThis)
#[derive(Debug, Clone)]
pub struct LegacyPluginThis {
  /// A partial representation of the options passed to [Sass::render].
  pub options: LegacyPluginThisOptions,
}

impl LegacyPluginThis {
  /// Creates a new [LegacyPluginThis].
  pub fn new(options: &LegacyOptions) -> Self {
    let mut include_paths =
      vec![env::current_dir().unwrap().to_string_lossy().to_string()];
    include_paths.extend(
      options
        .include_paths
        .iter()
        .map(|p| p.to_str().unwrap().to_string()),
    );
    Self {
      options: LegacyPluginThisOptions {
        file: options.file.clone(),
        data: options.data.clone(),
        include_paths: include_paths.join(PATH_DELIMITER),
        precision: 10,
        style: 1,
        indent_type: IndentType::Space,
        indent_width: 2,
        linefeed: LineFeed::LF,
        result: LegacyPluginThisOptionsResult {
          stats: LegacyPluginThisOptionsResultStats {
            start: SystemTime::now(),
            entry: options
              .file
              .as_ref()
              .map(|file| file.to_str().unwrap().to_string())
              .unwrap_or_else(|| "data".to_owned()),
          },
        },
      },
    }
  }
}

/// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyImporterThis)
pub struct LegacyImporterThis {
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyImporterThis#options)
  pub options: LegacyPluginThisOptions,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyImporterThis#fromImporter)
  pub from_import: bool,
}

/// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/modules#LegacyImporterResult)
pub enum LegacyImporterResult {
  /// An object with the key file whose value is a path on disk. This causes
  /// Sass to load that file as though it had been imported directly.
  File(PathBuf),
  /// An object with the key contents whose value is the contents of a stylesheet
  /// (in SCSS syntax). This causes Sass to load that stylesheet’s contents.
  Contents {
    /// The contents of the stylesheet.
    contents: String,
    /// The path to the stylesheet.
    file: Option<PathBuf>,
  },
}

impl LegacyImporterResult {
  /// Creates a new [LegacyImporterResult] from a [PathBuf].
  pub fn file(path: impl Into<PathBuf>) -> Self {
    Self::File(path.into())
  }

  /// Creates a new [LegacyImporterResult] from contents [String].
  pub fn contents(contents: impl Into<String>) -> Self {
    Self::Contents {
      contents: contents.into(),
      file: None,
    }
  }

  /// Creates a new [LegacyImporterResult] from contents [String] and a [PathBuf].
  pub fn both(contents: impl Into<String>, file: impl Into<PathBuf>) -> Self {
    Self::Contents {
      contents: contents.into(),
      file: Some(file.into()),
    }
  }
}

/// A builder for [LegacyOptionsBuilder].
#[derive(Debug, Default)]
pub struct LegacyOptionsBuilder {
  options: LegacyOptions,
}

impl LegacyOptionsBuilder {
  /// Creates a new [LegacyOptionsBuilder].
  pub fn new() -> Self {
    Self::default()
  }

  /// Builds a [LegacyOptions].
  pub fn build(self) -> LegacyOptions {
    if self.options.data.is_none() && self.options.file.is_none() {
      panic!("Either options.data or options.file must be set.");
    }
    self.options
  }

  /// Sets the [LegacyOptions]'s [include_paths] field.
  pub fn include_paths(mut self, arg: &[impl AsRef<Path>]) -> Self {
    self.options.include_paths =
      arg.iter().map(|p| p.as_ref().to_owned()).collect();
    self
  }

  /// Adds a path to the [LegacyOptions]'s [include_paths] field.
  pub fn include_path(mut self, arg: impl AsRef<Path>) -> Self {
    self.options.include_paths.push(arg.as_ref().to_owned());
    self
  }

  /// Sets the [LegacyOptions]'s [indent_type] field.
  pub fn indent_type(mut self, arg: impl Into<IndentType>) -> Self {
    self.options.indent_type = arg.into();
    self
  }

  /// Sets the [LegacyOptions]'s [indent_width] field.
  pub fn indent_width(mut self, arg: impl Into<usize>) -> Self {
    self.options.indent_width = arg.into();
    self
  }

  /// Sets the [LegacyOptions]'s [linefeed] field.
  pub fn linefeed(mut self, arg: impl Into<LineFeed>) -> Self {
    self.options.linefeed = arg.into();
    self
  }

  /// Sets the [LegacyOptions]'s [output_style] field.
  pub fn output_style(mut self, arg: impl Into<OutputStyle>) -> Self {
    self.options.output_style = arg.into();
    self
  }

  /// Sets the [LegacyOptions]'s [source_map] field.
  pub fn source_map(mut self, arg: impl Into<bool>) -> Self {
    self.options.source_map = arg.into();
    self
  }

  /// Sets the [LegacyOptions]'s [source_map_contents] field.
  pub fn source_map_contents(mut self, arg: impl Into<bool>) -> Self {
    self.options.source_map_contents = arg.into();
    self
  }

  /// Sets the [LegacyOptions]'s [sass_importers] field with [SassLegacyImporter]s.
  pub fn sass_importers(
    mut self,
    arg: impl IntoIterator<Item = impl Into<BoxLegacyImporter>>,
  ) -> Self {
    self.options.importers = Some(arg.into_iter().map(|i| i.into()).collect());
    self
  }

  /// Adds a [SassLegacyImporter] to the [LegacyOptions]'s [sass_importers] field.
  pub fn sass_importer(mut self, arg: impl Into<BoxLegacyImporter>) -> Self {
    self.options.importers =
      Some(if let Some(mut importers) = self.options.importers {
        importers.push(arg.into());
        importers
      } else {
        vec![arg.into()]
      });
    self
  }

  /// Sets the [LegacyOptions]'s [sass_importers] field with [LegacyImporter]s.
  pub fn importers(
    self,
    arg: impl IntoIterator<Item = impl Into<Box<dyn LegacyImporter>>>,
  ) -> Self {
    self.sass_importers(arg)
  }

  /// Adds a [LegacyImporter] to the [LegacyOptions]'s [sass_importers] field.
  pub fn importer<I: 'static + LegacyImporter>(self, arg: I) -> Self {
    self.sass_importer(arg)
  }

  /// Sets the [LegacyOptions]'s [charset] field.
  pub fn charset(mut self, arg: impl Into<bool>) -> Self {
    self.options.charset = arg.into();
    self
  }

  /// Sets the [LegacyOptions]'s [quiet_deps] field.
  pub fn quiet_deps(mut self, arg: impl Into<bool>) -> Self {
    self.options.quiet_deps = arg.into();
    self
  }

  /// Sets the [LegacyOptions]'s [verbose] field.
  pub fn verbose(mut self, arg: impl Into<bool>) -> Self {
    self.options.verbose = arg.into();
    self
  }

  /// Sets the [LegacyOptions]'s [logger] field.
  pub fn logger<L: 'static + Logger>(mut self, arg: L) -> Self {
    self.options.logger = Some(Box::new(arg));
    self
  }

  /// Sets the [LegacyOptions]'s [file] field.
  pub fn file(mut self, arg: impl AsRef<Path>) -> Self {
    self.options.file = Some(arg.as_ref().to_owned());
    self
  }

  /// Sets the [LegacyOptions]'s [data] field.
  pub fn data(mut self, arg: impl Into<String>) -> Self {
    self.options.data = Some(arg.into());
    self
  }

  /// Sets the [LegacyOptions]'s [indented_syntax] field.
  pub fn indented_syntax(mut self, arg: impl Into<bool>) -> Self {
    self.options.indented_syntax = Some(arg.into());
    self
  }
}

/// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyImporterThis)
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct LegacyOptions {
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacySharedOptions#includePaths)
  pub include_paths: Vec<PathBuf>,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacySharedOptions#indentType)
  pub indent_type: IndentType,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacySharedOptions#indentWidth)
  pub indent_width: usize,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacySharedOptions#linefeed)
  pub linefeed: LineFeed,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacySharedOptions#outputStyle)
  pub output_style: OutputStyle,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacySharedOptions#sourceMap)
  pub source_map: bool,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacySharedOptions#sourceMapContents)
  pub source_map_contents: bool,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacySharedOptions#importer)
  #[cfg_attr(feature = "serde", serde(skip))]
  pub importers: Option<Vec<BoxLegacyImporter>>,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacySharedOptions#charset)
  pub charset: bool,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacySharedOptions#quietDeps)
  pub quiet_deps: bool,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacySharedOptions#verbose)
  pub verbose: bool,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacySharedOptions#logger)
  #[cfg_attr(feature = "serde", serde(skip))]
  pub logger: Option<BoxLogger>,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyFileOptions#file)
  pub file: Option<PathBuf>,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyStringOptions#data)
  pub data: Option<String>,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyStringOptions#indentedSyntax)
  pub indented_syntax: Option<bool>,
}

impl Default for LegacyOptions {
  fn default() -> Self {
    Self {
      indent_width: 2,
      charset: true,
      include_paths: Vec::new(),
      indent_type: IndentType::Space,
      linefeed: LineFeed::LF,
      output_style: OutputStyle::Expanded,
      source_map: false,
      source_map_contents: false,
      importers: None,
      quiet_deps: false,
      verbose: false,
      logger: None,
      file: None,
      data: None,
      indented_syntax: None,
    }
  }
}

impl From<LegacyOptions> for Options {
  fn from(options: LegacyOptions) -> Self {
    Self {
      load_paths: options.include_paths,
      logger: options.logger,
      quiet_deps: options.quiet_deps,
      source_map: options.source_map,
      source_map_include_sources: options.source_map_contents,
      style: options.output_style,
      verbose: options.verbose,
      charset: options.charset,
      // importers: set manually after into modern options
      ..Default::default()
    }
  }
}

impl From<LegacyOptions> for StringOptions {
  fn from(options: LegacyOptions) -> Self {
    let url = options
      .file
      .clone()
      .map(|file| Url::from_file_path(file).unwrap())
      .unwrap_or_else(|| Url::parse(LEGACY_IMPORTER_PROTOCOL).unwrap());
    let syntax = options
      .indented_syntax
      .map(|s| if s { Syntax::Indented } else { Syntax::Scss })
      .unwrap_or_default();
    let options = Options::from(options);
    Self {
      common: options,
      input_importer: None,
      syntax,
      url: Some(url),
    }
  }
}

impl LegacyOptions {
  pub(crate) fn adjust_options(mut self) -> Self {
    if let Some(file) = &self.file {
      if self.indented_syntax.is_some() || self.importers.is_some() {
        self.data = Some(fs::read_to_string(file).unwrap());
        self.indented_syntax = Some(self.indented_syntax.unwrap_or_default());
      }
    }
    self
  }
}

/// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyResult)
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct LegacyResult {
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyResult#css)
  pub css: Vec<u8>,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyResult#map)
  pub map: Option<Vec<u8>>,
  /// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyResult#stats)
  pub stats: LegacyResultStats,
}

/// More information: [Sass documentation](https://sass-lang.com/documentation/js-api/interfaces/LegacyResult#stats)
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct LegacyResultStats {
  /// The absolute path of [LegacyOptions.file], or "data" if [LegacyOptions.file]
  /// wasn't set.
  pub entry: String,
  /// The number of milliseconds between 1 January 1970 at 00:00:00 UTC and the time
  /// at which Sass compilation began.
  pub start: SystemTime,
  /// The number of milliseconds between 1 January 1970 at 00:00:00 UTC and the time
  /// at which Sass compilation ended.
  pub end: SystemTime,
  /// The number of milliseconds it took to compile the Sass file. This is always equal
  /// to start minus end.
  pub duration: Duration,
  /// An array of the absolute paths of all Sass files loaded during compilation. If
  /// a stylesheet was loaded from a LegacyImporter that returned the stylesheet’s
  /// contents, the raw string of the @use or @import that loaded that stylesheet
  /// included in this array.
  pub included_files: Vec<String>,
}

impl LegacyResult {
  /// Creates a new [LegacyResult].
  pub fn new(entry: String, start: SystemTime, result: CompileResult) -> Self {
    let end = SystemTime::now();
    Self {
      css: result.css.into_bytes(),
      map: result.source_map.map(|map| map.into_bytes()),
      stats: LegacyResultStats {
        entry,
        start,
        end,
        duration: end.duration_since(start).unwrap(),
        included_files: result
          .loaded_urls
          .into_iter()
          .filter(|url| format!("{}:", url.scheme()) != END_OF_LOAD_PROTOCOL)
          .map(|url| {
            if url.scheme() == "file" {
              url_to_file_path_cross_platform(&url)
                .to_string_lossy()
                .to_string()
            } else if format!("{}:", url.scheme()) == LEGACY_IMPORTER_PROTOCOL {
              decode(url.path()).unwrap().to_string()
            } else {
              url.to_string()
            }
          })
          .collect(),
      },
    }
  }
}