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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
extern crate zip;

use self::zip::ZipArchive;
use std::io::Read;
use super::*;

const INVALID_VALUE_MESSAGE: &str = "Expected a color in format of #rrggbb or #rrggbbaa, or a variable's name";
const UNEXPECTED_COLON_MESSAGE: &str = "Unexpected colon (`:`)";
const UNEXPECTED_SEMICOLON_MESSAGE: &str = "Unexpected semicolon (`;`)";
const EXPECTED_COLON_MESSAGE: &str = "Expected a colon (`:`)";
const EXPECTED_SEMICOLON_MESSAGE: &str = "Expected a semicolon (`;`)";
const INVALID_NAME_MESSAGE: &str = "A variable's name may only consist of latin letters, digits and the symbol `_`";
const NO_COLORS_DECLARATION_MESSAGE: &str = "A `.tdesktop-theme` archive must have a `colors.tdesktop-theme` file";
const BACKGROUND_PRECEDENCE: [(&str, WallpaperType, WallpaperExtension); 4] = [
  ("background.jpg", WallpaperType::Background, WallpaperExtension::Jpg),
  ("background.png", WallpaperType::Background, WallpaperExtension::Png),
  ("tiled.jpg", WallpaperType::Tiled, WallpaperExtension::Jpg),
  ("tiled.png", WallpaperType::Tiled, WallpaperExtension::Png),
];

const SHORT_HEX_LENGTH: usize = 1 + 2 * 3;
const LONG_HEX_LENGTH: usize = 1 + 2 * 4;

#[derive(PartialEq)]
struct Token<'a> {
  token: &'a [u8],
  line: usize,
  column: usize,
}

impl<'a> std::fmt::Debug for Token<'a> {
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    write!(f,
      "Token \"{token}\" at {line}:{column}",
      token = String::from_utf8(self.token.to_vec()).unwrap(),
      line = self.line,
      column = self.column,
    )
  }
}

enum InComment {
  None,
  SingleLine,
  MultiLine,
}

/// Represents an error occured during parsing.
///
/// # Reasons parsing may fail
///
/// - Absence of the file `colors.tdesktop-theme` if the parser was given an
///   archive;
/// - Bad formatting of the palette file (it's `<variable>:<color>;`, with
///   any number of whitespaces between tokens, and also C-style comments);
/// - Wrong format of colors (they may only be `#rrggbb` or `#rrggbbaa`);
/// - Wrong format of variable names (they may only contain latin letters,
///   digits and the underscore symbol (`_`)).
#[derive(Debug)]
pub struct ParseError {
  /// Contains a message explaining why parsing failed.
  pub message: &'static str,
  /// If possible, the parser will report the line of the contents where parsing
  /// failed.
  pub line: Option<usize>,
  /// If possible, the parser will report the column of the contents where
  /// parsing failed.
  pub column: Option<usize>,
}

impl ParseError {
  fn new(message: &'static str, line: usize, column: usize) -> ParseError {
    ParseError {
      message,
      line: Some(line),
      column: Some(column),
    }
  }

  fn invalid_value(line: usize, column: usize) -> ParseError {
    ParseError::new(INVALID_VALUE_MESSAGE, line, column)
  }

  fn unexpected_colon(line: usize, column: usize) -> ParseError {
    ParseError::new(UNEXPECTED_COLON_MESSAGE, line, column)
  }

  fn unexpected_semicolon(line: usize, column: usize) -> ParseError {
    ParseError::new(UNEXPECTED_SEMICOLON_MESSAGE, line, column)
  }

  fn expected_colon(line: usize, column: usize) -> ParseError {
    ParseError::new(EXPECTED_COLON_MESSAGE, line, column)
  }

  fn expected_semicolon(line: usize, column: usize) -> ParseError {
    ParseError::new(EXPECTED_SEMICOLON_MESSAGE, line, column)
  }

  fn invalid_name(line: usize, column: usize) -> ParseError {
    ParseError::new(INVALID_NAME_MESSAGE, line, column)
  }

  fn no_colors_declaration() -> ParseError {
    ParseError {
      message: NO_COLORS_DECLARATION_MESSAGE,
      line: None,
      column: None,
    }
  }
}

impl std::fmt::Display for ParseError {
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    if let Some(line) = self.line {
      write!(f,
        "{message} at {line}:{column}",
        message = self.message,
        line = line,
        // If `line` exists, then `columns` must exist too.
        column = self.column.unwrap(),
      )
    } else {
      write!(f, "{}", self.message)
    }
  }
}

impl std::error::Error for ParseError {}

fn parse_hex_number(
  number: &[u8],
  line: usize,
  column: usize,
) -> Result<u8, ParseError> {
  let mut result = 0;

  for (index, digit) in number.iter().enumerate() {
    let shift = (number.len() - index - 1) as u8 * 4;

    if digit >= &b'0' && digit <= &b'9' {
      result += (digit - b'0') << shift;
    } else if digit >= &b'a' && digit <= &b'f' {
      result += (digit - b'a' + 10) << shift;
    } else if digit >= &b'A' && digit <= &b'F' {
      result += (digit - b'A' + 10) << shift;
    } else {
      return Err(ParseError::invalid_value(line, column));
    }
  }

  Ok(result)
}

fn parse_color(
  value: &[u8],
  line: usize,
  column: usize,
) -> Result<Color, ParseError> {
  if value == b":" {
    return Err(ParseError::unexpected_colon(line, column));
  }

  if value == b";" {
    return Err(ParseError::unexpected_semicolon(line, column));
  }

  if
    (value.len() != SHORT_HEX_LENGTH && value.len() != LONG_HEX_LENGTH)
    || value[0] != b'#'
  {
    return Err(ParseError::invalid_value(line, column));
  }

  let red = parse_hex_number(&value[1..3], line, column)?;
  let green = parse_hex_number(&value[3..5], line, column)?;
  let blue = parse_hex_number(&value[5..7], line, column)?;
  let alpha = if value.len() == LONG_HEX_LENGTH {
    parse_hex_number(&value[7..9], line, column)?
  } else {
    0xff
  };

  Ok([red, green, blue, alpha])
}

fn tokenize(contents: &[u8]) -> Vec<Token> {
  let mut tokens = Vec::new();
  let mut line = 1;
  let mut column = 1;
  let mut is_in_comment = InComment::None;
  let mut token_start_index = None;
  let mut token_start_line = 0;
  let mut token_start_column = 0;

  let mut index = 0;

  macro_rules! skip {
    ($offset:expr) => {{
      index += $offset;
      column += $offset;
    }};
  }

  while let Some(symbol) = contents.get(index) {
    if symbol == &b'\n' {
      if let InComment::SingleLine = is_in_comment {
        is_in_comment = InComment::None;
      }

      line += 1;
      column = 1;
      index += 1;

      continue;
    }

    if let InComment::SingleLine = is_in_comment {
      skip!(1);
      continue;
    }

    if index + 2 < contents.len() {
      match &contents[index..index + 2] {
        b"//" => {
          is_in_comment = InComment::SingleLine;
          skip!(2);
          continue;
        },
        b"/*" => {
          is_in_comment = InComment::MultiLine;
          skip!(2);
          continue;
        },
        _ => (),
      }
    }

    if let InComment::MultiLine = is_in_comment {
      if index + 2 < contents.len() && &contents[index..index + 2] == b"*/" {
        is_in_comment = InComment::None;
        skip!(2);
      } else {
        skip!(1);
      }

      continue;
    }

    if (*symbol as char).is_whitespace() {
      if let Some(start) = token_start_index {
        tokens.push(Token {
          token: &contents[start..index],
          line: token_start_line,
          column: token_start_column,
        });
        token_start_index = None;
      }

      skip!(1);
      continue;
    }

    match symbol {
      b':' | b';' => {
        if let Some(start) = token_start_index {
          tokens.push(Token {
            token: &contents[start..index],
            line: token_start_line,
            column: token_start_column,
          });
        }

        tokens.push(Token {
          token: &contents[index..index + 1],
          line,
          column,
        });
        token_start_index = None;
      },
      _ => if let None = token_start_index {
        token_start_index = Some(index);
        token_start_line = line;
        token_start_column = column;
      },
    }

    skip!(1);
  }

  if let Some(start) = token_start_index {
    tokens.push(Token {
      token: &contents[start..index],
      line: token_start_line,
      column: token_start_column,
    });
  }

  tokens
}

fn get_name(
  bytes: &[u8],
  line: usize,
  column: usize,
) -> Result<String, ParseError> {
  if bytes == b":" {
    return Err(ParseError::unexpected_colon(line, column));
  }

  if bytes == b";" {
    return Err(ParseError::unexpected_semicolon(line, column));
  }

  if bytes.iter().all(|symbol| {
    (symbol >= &b'a' && symbol <= &b'z')
    || (symbol >= &b'A' && symbol <= &b'Z')
    || (symbol >= &b'0' && symbol <= &b'9')
    || symbol == &b'_'
  }) {
    Ok(String::from_utf8(bytes.to_vec()).unwrap())
  } else {
    Err(ParseError::invalid_name(line, column))
  }
}

fn parse_palette(contents: &[u8]) -> Result<Variables, ParseError> {
  let tokens = tokenize(contents);
  let mut variables = IndexMap::new();

  for tokens_group in tokens.chunks(4) {
    let Token { token: variable_name, line, column } = tokens_group[0];
    let mut line = line;
    let mut column = column;

    if let Some(colon) = tokens_group.get(1) {
      if &colon.token != &b":" {
        return Err(ParseError::expected_colon(colon.line, colon.column));
      }

      line = colon.line;
      column = colon.column;
    } else {
      return Err(ParseError::expected_colon(line, column + 1));
    };

    let value = if let Some(color) = tokens_group.get(2) {
      color
    } else {
      return Err(ParseError::invalid_value(line, column));
    };

    line = value.line;
    column = value.column;

    if let Some(semicolon) = tokens_group.get(3) {
      if &semicolon.token != &b";" {
        return Err(ParseError::expected_semicolon(
          semicolon.line,
          semicolon.column,
        ));
      }
    } else {
      return Err(ParseError::expected_semicolon(line, column + 1));
    }

    let variable_name = get_name(&variable_name, line, column)?;
    let value = if let Ok(color) = parse_color(value.token, line, column) {
      Value::Color(color)
    } else if let Ok(name) = get_name(value.token, line, column) {
      Value::Link(name)
    } else {
      return Err(ParseError::invalid_value(line, column));
    };

    variables.insert(variable_name, value);
  }

  Ok(variables)
}

fn get_bytes(file: self::zip::read::ZipFile<'_>) -> Vec<u8> {
  file
    .bytes()
    // Any hint why reading a byte may fail in the middle?
    .map(|x| x.unwrap())
    .collect()
}

pub fn parse(
  contents: &[u8],
) -> Result<(Option<Wallpaper>, Variables), ParseError> {
  let cursor = std::io::Cursor::new(contents);

  if let Ok(mut archive) = ZipArchive::new(cursor) {
    let variables = {
      if let Ok(palette) = archive.by_name("colors.tdesktop-theme") {
        let bytes = get_bytes(palette);

        parse_palette(&bytes[..])?
      } else {
        return Err(ParseError::no_colors_declaration());
      }
    };

    let mut wallpaper = None;

    for (filename, wallpaper_type, extension) in BACKGROUND_PRECEDENCE.iter() {
      if let Ok(content) = archive.by_name(filename) {
        wallpaper = Some(Wallpaper {
          wallpaper_type: *wallpaper_type,
          extension: *extension,
          bytes: get_bytes(content),
        });

        break;
      }
    }

    Ok((wallpaper, variables))
  } else {
    Ok((None, parse_palette(contents)?))
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use std::fs::read;

  #[test]
  fn correctly_parses_hex_numbers() {
    assert_eq!(parse_hex_number(b"00", 0, 0).unwrap(), 0x00);
    assert_eq!(parse_hex_number(b"05", 0, 0).unwrap(), 0x05);
    assert_eq!(parse_hex_number(b"0f", 0, 0).unwrap(), 0x0f);
    assert_eq!(parse_hex_number(b"50", 0, 0).unwrap(), 0x50);
    assert_eq!(parse_hex_number(b"55", 0, 0).unwrap(), 0x55);
    assert_eq!(parse_hex_number(b"5f", 0, 0).unwrap(), 0x5f);
    assert_eq!(parse_hex_number(b"f0", 0, 0).unwrap(), 0xf0);
    assert_eq!(parse_hex_number(b"f5", 0, 0).unwrap(), 0xf5);
    assert_eq!(parse_hex_number(b"ff", 0, 0).unwrap(), 0xff);
    assert_eq!(parse_hex_number(b"FF", 0, 0).unwrap(), 0xff);

    if let Ok(_) = parse_hex_number(b"gg", 0, 0) {
      panic!("Should've returned an error");
    }

    if let Ok(_) = parse_hex_number(b"GG", 0, 0) {
      panic!("Should've returned an error");
    }

    if let Ok(_) = parse_hex_number(b"::", 0, 0) {
      panic!("Should've returned an error");
    }

    if let Ok(_) = parse_hex_number(b"!!", 0, 0) {
      panic!("Should've returned an error");
    }
  }

  #[test]
  fn correctly_parses_colors() {
    assert_eq!(
      parse_color(b"#102030", 0, 0).unwrap(),
      [0x10, 0x20, 0x30, 0xff],
    );

    assert_eq!(
      parse_color(b"#10203040", 0, 0).unwrap(),
      [0x10, 0x20, 0x30, 0x40],
    );

    if let Ok(_) = parse_color(b"#fff", 0, 0) {
      panic!("Should've returned an error");
    }

    if let Ok(_) = parse_color(b"1020304", 0, 0) {
      panic!("Should've returned an error");
    }
  }

  #[test]
  fn tokenizes_correctly() {
    assert_eq!(
      tokenize(
        b"windowBg: #123456; // this is a comment
        windowFg:#12345678; /*
          this is a multiline comment
          :;
        */ windowActiveBg : #112233;"
      ),
      vec![
        Token { token: b"windowBg", line: 1, column: 1 },
        Token { token: b":", line: 1, column: 9 },
        Token { token: b"#123456", line: 1, column: 11 },
        Token { token: b";", line: 1, column: 18 },
        Token { token: b"windowFg", line: 2, column: 9 },
        Token { token: b":", line: 2, column: 17 },
        Token { token: b"#12345678", line: 2, column: 18 },
        Token { token: b";", line: 2, column: 27 },
        Token { token: b"windowActiveBg", line: 5, column: 12 },
        Token { token: b":", line: 5, column: 27 },
        Token { token: b"#112233", line: 5, column: 29 },
        Token { token: b";", line: 5, column: 36 },
      ],
    );

    assert_eq!(
      tokenize(b"w"),
      vec![Token { token: b"w", line: 1, column: 1 }],
    );
  }

  #[test]
  fn parses_palettes_correctly() {
    assert_eq!(
      parse_palette(
        b"windowBg: #ffffff;
        windowFg: #00000000;
        windowBoldFg: windowFg;"
      ).unwrap(),
      indexmap!{
        "windowBg".to_string() => Value::Color([0xff; 4]),
        "windowFg".to_string() => Value::Color([0x00; 4]),
        "windowBoldFg".to_string() => Value::Link("windowFg".to_string()),
      },
    );

    if let Ok(_) = parse_palette(b";") {
      panic!("parse_palette should've returned an error for b\";\"");
    }

    if let Ok(_) = parse_palette(b":") {
      panic!("parse_palette should've returned an error for b\":\"");
    }

    if let Ok(_) = parse_palette(b"w") {
      panic!("parse_palette should've returned an error for b\"w\"");
    }

    if let Ok(_) = parse_palette(b"w: #;") {
      panic!("parse_palette should've returned an error for b\"w: #;\"");
    }

    if let Ok(_) = parse_palette(b"w: v") {
      panic!("parse_palette should've returned an error for b\"w: v\"");
    }
  }

  #[test]
  fn general_parser_works_correctly() {
    let contents = read("./tests/assets/palette.tdesktop-palette").unwrap();
    let theme = parse(&contents[..]).unwrap();

    assert_eq!(theme, (None, indexmap!{
      "windowBg".to_string() => Value::Color([0x10, 0x20, 0x30, 0xff]),
    }));

    let contents = read("./tests/assets/all-wallpapers.tdesktop-theme")
      .unwrap();
    let theme = parse(&contents[..]).unwrap();

    assert_eq!(theme, (
      Some(Wallpaper {
        wallpaper_type: WallpaperType::Background,
        extension: WallpaperExtension::Jpg,
        bytes: Vec::new(),
      }),
      IndexMap::new(),
    ));

    let contents = read("./tests/assets/no-background-jpg.tdesktop-theme")
      .unwrap();
    let theme = parse(&contents[..]).unwrap();

    assert_eq!(theme, (
      Some(Wallpaper {
        wallpaper_type: WallpaperType::Background,
        extension: WallpaperExtension::Png,
        bytes: Vec::new(),
      }),
      IndexMap::new(),
    ));

    let contents = read("./tests/assets/no-all-background.tdesktop-theme")
      .unwrap();
    let theme = parse(&contents[..]).unwrap();

    assert_eq!(theme, (
      Some(Wallpaper {
        wallpaper_type: WallpaperType::Tiled,
        extension: WallpaperExtension::Jpg,
        bytes: Vec::new(),
      }),
      IndexMap::new(),
    ));

    let contents = read("./tests/assets/only-tiled-png.tdesktop-theme")
      .unwrap();
    let theme = parse(&contents[..]).unwrap();

    assert_eq!(theme, (
      Some(Wallpaper {
        wallpaper_type: WallpaperType::Tiled,
        extension: WallpaperExtension::Png,
        bytes: Vec::new(),
      }),
      IndexMap::new(),
    ));

    let contents = read("./tests/assets/only-colors.tdesktop-theme").unwrap();
    let theme = parse(&contents[..]).unwrap();

    assert_eq!(theme, (None, indexmap!{
      "windowBg".to_string() => Value::Color([0x10, 0x20, 0x30, 0xff]),
    }));

    let contents = read("./tests/assets/no-colors.tdesktop-theme").unwrap();

    if let Ok(_) = parse(&contents[..]) {
      panic!("`parse` should've returned an error for a `.tdesktop-theme` file with no `colors.tdesktop-theme`");
    }
  }
}