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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
// (c) 2016 Productize SPRL <joost@productize.be>

// extension: .lib
// format: old-style

use std::fmt;
use std::result;
use std::str::FromStr;
use std::path::PathBuf;

// get from parent
use Result;
use util::read_file;
use parse_split_quote_aware;
use schematic;
use str_error;

/// a Kicad symbolic file
#[derive(Debug,Default)]
pub struct SymbolLib {
    /// the symbols
    pub symbols: Vec<Symbol>,
}

// DEF name reference unused text_offset draw_pinnumber draw_pinname unit_count units_locked option_flag
/// a symbol
#[derive(Debug,Clone)]
pub struct Symbol {
    /// name
    pub name: String,
    /// reference
    pub reference: String,
    /// text offset
    pub text_offset: f64,
    /// draw pinnumber
    pub draw_pinnumber: bool,
    /// draw pinname
    pub draw_pinname: bool,
    /// unit count
    pub unit_count: i64,
    /// is the unit locked
    pub unit_locked: bool,
    /// is it a power symbol
    pub is_power: bool,
    /// fields
    pub fields: Vec<Field>,
    /// aliases
    pub aliases: Vec<String>,
    /// draw
    pub draw: Vec<Draw>,
}
// F n “text” posx posy dimension orientation visibility hjustify vjustify/italic/bold “name”
// F0 "#PWR" 0 0 30 H I C CNN

/// a field
#[derive(Debug,Clone)]
pub struct Field {
    /// field number
    pub i: i64,
    /// value
    pub value: String,
    /// X coordinate
    pub x: f64,
    /// Y coordinate
    pub y: f64,
    /// dimension
    pub dimension: i64,
    /// orientation
    pub orientation: schematic::Orientation,
    /// if the field is visible
    pub visible: bool,
    /// horizontal justification
    pub hjustify: schematic::Justify,
    /// vertical justification
    pub vjustify: schematic::Justify,
    /// italic
    pub italic: bool,
    /// bold
    pub bold: bool,
    /// name of the field
    pub name: String,
}

/// a drawing
#[derive(Debug,Clone)]
pub enum Draw {
    /// a pin
    Pin(Pin),
    /// a non-parsed drawing part
    Other(String),
}

// U (up) D (down) R (right) L (left).
/// pin orientation
#[derive(Debug,Clone)]
pub enum PinOrientation {
    /// up
    Up,    // U
    /// down
    Down,  // D
    /// left
    Left,  // L
    /// right
    Right, // R
}

/// pin type
#[derive(Debug,Clone)]
pub enum PinType {
    /// input
    Input, // I
    /// output
    Output, // O
    /// bidi
    Bidi, // B
    /// tristate
    Tristate, // T
    /// passive
    Passive, // P
    /// unspecified
    Unspecified, // U
    /// power output
    PowerInput, // W
    /// power input
    PowerOutput, // w
    /// open collector
    OpenCollector, // C
    /// open emitter
    OpenEmitter, // E
    /// not connected
    NotConnected, // N
}

/// pin shape
#[derive(Debug,Clone,PartialEq)]
pub enum PinShape {
    /// line
    Line, // None (default)
    /// inverted
    Inverted, // I
    /// clock
    Clock, // C
    /// inverted clock
    InvertedClock, // CI
    /// input low
    InputLow, // L
    /// clock low
    ClockLow, // CL
    /// output low
    OutputLow, // V
    /// falling edge clock
    FallingEdgeClock, // F
    /// non logic
    NonLogic, // X
}

// X name number posx posy length orientation Snum Snom unit convert Etype [shape].
// X P1 1 -200 200 150 R 50 50 1 1 P
// X +3.3V 1 0 0 0 U 30 30 0 0 W N
/// draw a pin
#[derive(Debug,Clone,Default)]
pub struct Pin {
    /// name of the pin
    pub name:String,
    /// number of the pin, which doesn't have to be an actual number
    pub number:String,
    /// x position of the pin
    pub x:i64,
    /// y position of the pin
    pub y:i64,
    /// length of the pin
    pub len:i64,
    /// orientation of the pin
    pub orientation:PinOrientation,
    /// pin number text size
    pub num_size:i64,
    /// pin name text size
    pub name_size:i64,
    /// unit ??
    pub unit: i64,
    /// convert ??
    pub convert: i64,
    /// pin type
    pub pin_type: PinType,
    /// pin visible
    pub pin_visible: bool,
    /// pin shape
    pub pin_shape: PinShape,
    
}

impl SymbolLib {
    /// find a symbol in a symbol lib
    pub fn find<F>(&self, filter: F) -> Option<&Symbol>
        where F: Fn(&Symbol) -> bool
    {
        for symbol in &self.symbols {
            if filter(symbol) {
                return Some(symbol);
            }
        }
        None
    }
}

impl fmt::Display for SymbolLib {
    fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
        writeln!(f, "EESchema-LIBRARY Version 2.3")?;
        writeln!(f, "#encoding utf-8")?;
        writeln!(f, "#")?;
        for i in &self.symbols {
            write!(f, "{}", i)?
        }
        writeln!(f, "#End Library")
    }
}

impl Symbol {
    /// create a new symbol
    pub fn new(name: String, reference: String) -> Symbol {
        Symbol {
            name: name,
            reference: reference,
            text_offset: 0.0,
            draw_pinnumber: false,
            draw_pinname: false,
            unit_count: 1,
            unit_locked: false,
            is_power: false,
            fields: vec![],
            aliases: vec![],
            draw: vec![],
        }
    }

    /// set the name of the symbol
    pub fn set_name(&mut self, name: &str) {
        if char_at(&self.name, 0) == '~' {
            self.name = format!("~{}", name)
        } else {
            self.name = name.to_string()
        }
        let mut field = &mut self.fields[1];
        field.value = name.to_string()
    }
}

impl fmt::Display for Symbol {
    fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
        writeln!(f, "# {}", &&self.fields[1].value)?;
        writeln!(f, "#")?;
        writeln!(f, "DEF {} {} 0 {} {} {} {} {} {}",
                     self.name, self.reference, self.text_offset,
                     if self.draw_pinnumber { "Y" } else { "N" },
                     if self.draw_pinname { "Y" } else { "N" },
                     self.unit_count,
                     if self.unit_locked { "L" } else { "F" },
                     if self.is_power { "P" } else { "N" },
                     )
            ?;
        for field in &self.fields {
            writeln!(f, "{}", field)?
        }
        if !self.aliases.is_empty() {
            write!(f, "ALIAS")?;
            for alias in &self.aliases {
                write!(f, " ")?;
                write!(f, "{}", alias)?;
            }
            writeln!(f, "")?;
        }
        writeln!(f, "DRAW")?;
        for draw in &self.draw {
            writeln!(f, "{}", draw)?
        }
        writeln!(f, "ENDDRAW")?;
        writeln!(f, "ENDDEF")?;
        writeln!(f, "#")
    }
}

impl Default for Field {
    fn default() -> Field {
        Field {
            i: 0,
            value: String::from(""),
            x: 0.0,
            y: 0.0,
            dimension: 0,
            orientation: schematic::Orientation::Horizontal,
            visible: false,
            hjustify: schematic::Justify::Center,
            vjustify: schematic::Justify::Center,
            italic: false,
            bold: false,
            name: String::from(""),
        }
    }
}

impl fmt::Display for Field {
    fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
        write!(f, "F{} \"{}\" {} {} {} {} {} {} {}{}{}",
               self.i, self.value, self.x, self.y, self.dimension,
               self.orientation,
               if self.visible { "V" } else { "I" },
               self.hjustify, self.vjustify,
               if self.italic { "I" } else { "N" },
               if self.bold { "I" } else { "N" },
                    )
            ?;
        if self.i > 3 {
            write!(f, " \"{}\"", self.name)?
        };
        Ok(())
    }
}
impl fmt::Display for Draw {
    fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
        match *self {
            Draw::Other(ref s) => write!(f, "{}", s),
            Draw::Pin(ref p) => write!(f, "{}", p),
        }
    }
}

impl fmt::Display for Pin {
    fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
        write!(f, "X {} {} ", self.name, self.number)?;
        write!(f, "{} {} {} ", self.x, self.y, self.len)?;
        write!(f, "{} ", self.orientation)?;
        write!(f, "{} {} ", self.num_size, self.name_size)?;
        write!(f, "{} {} ", self.unit, self.convert)?;
        write!(f, "{}", self.pin_type)?;
        if self.pin_visible {
            if self.pin_shape != PinShape::Line {
                write!(f, " {}", self.pin_shape)
            } else {
                Ok(())
            }
        } else {
            write!(f, " N{}", self.pin_shape)
        }
    }
}

impl fmt::Display for PinOrientation {
    fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
        match *self {
            PinOrientation::Up => write!(f, "{}", 'U'),
            PinOrientation::Down => write!(f, "{}", 'D'),
            PinOrientation::Left => write!(f, "{}", 'L'),
            PinOrientation::Right => write!(f, "{}", 'R'),
        }
    }
}

impl Default for PinOrientation {
    fn default() -> PinOrientation {
        PinOrientation::Up
    }
}

impl PinOrientation {
    fn from_str(s:&str) -> Result<PinOrientation> {
        match s {
            "U" => Ok(PinOrientation::Up),
            "D" => Ok(PinOrientation::Down),
            "L" => Ok(PinOrientation::Left),
            "R" => Ok(PinOrientation::Right),
            _ => Err(format!("unknown pin orientation {}", s).into())
        }
    }
}

impl fmt::Display for PinType {
    fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
        match *self {
            PinType::Input => write!(f, "{}", 'I'),
            PinType::Output => write!(f, "{}", 'O'),
            PinType::Bidi => write!(f, "{}", 'B'),
            PinType::Tristate => write!(f, "{}", 'T'),
            PinType::Passive => write!(f, "{}", 'P'),
            PinType::Unspecified => write!(f, "{}", 'U'),
            PinType::PowerInput => write!(f, "{}", 'W'),
            PinType::PowerOutput => write!(f, "{}", 'w'),
            PinType::OpenCollector => write!(f, "{}", 'C'),
            PinType::OpenEmitter => write!(f, "{}", 'E'),
            PinType::NotConnected => write!(f, "{}", 'N'),
        }
    }
}

impl Default for PinType {
    fn default() -> PinType {
        PinType::Input
    }
}

impl PinType {
    fn from_str(s:&str) -> Result<PinType> {
        match s {
            "I" => Ok(PinType::Input),
            "O" => Ok(PinType::Output),
            "B" => Ok(PinType::Bidi),
            "T" => Ok(PinType::Tristate),
            "P" => Ok(PinType::Passive),
            "U" => Ok(PinType::Unspecified),
            "W" => Ok(PinType::PowerInput),
            "w" => Ok(PinType::PowerOutput),
            "C" => Ok(PinType::OpenCollector),
            "E" => Ok(PinType::OpenEmitter),
            "N" => Ok(PinType::NotConnected),
            _ => Err(format!("unknown pin type {}", s).into())
        }
    }
}

impl fmt::Display for PinShape {
    fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
        match *self {
            PinShape::Line => Ok(()),
            PinShape::Inverted => write!(f, "{}", 'I'),
            PinShape::Clock => write!(f, "{}", 'C'),
            PinShape::InvertedClock => write!(f, "{}", "CI"),
            PinShape::InputLow => write!(f, "{}", 'L'),
            PinShape::ClockLow => write!(f, "{}", "CL"),
            PinShape::OutputLow => write!(f, "{}", "V"),
            PinShape::FallingEdgeClock => write!(f, "{}", "F"),
            PinShape::NonLogic => write!(f, "{}", "X"),
        }
    }
}

impl Default for PinShape {
    fn default() -> PinShape {
        PinShape::Line
    }
}

impl PinShape {
    fn from_str(s:&str) -> Result<PinShape> {
        if s.is_empty() {
            Ok(PinShape::Line)
        } else {
            let s = if s.starts_with("N") {
                &s[1..]
            } else {
                &s[..]
            };
            match s {
                "I" => Ok(PinShape::Inverted),
                "C" => Ok(PinShape::Clock),
                "CI" => Ok(PinShape::InvertedClock),
                "L" => Ok(PinShape::InputLow),
                "CL" => Ok(PinShape::ClockLow),
                "V" => Ok(PinShape::OutputLow),
                "F" => Ok(PinShape::FallingEdgeClock),
                "X" => Ok(PinShape::NonLogic),
                "" => Ok(PinShape::Line),
                _ => Err(format!("unknown pinshape {}", s).into()),
            }
        }
    }
    
    fn visible_from_str(s:&str) -> bool {
        if s.is_empty() {
            false
        } else {
            !s.starts_with("N")
        }
    }
}

macro_rules! assume_line {
    ($s:expr, $exp:expr) => (
        if $s.eof() {
            return str_error("end of file reached".to_string())
        }
        if $s.here() != $exp {
            return str_error(format!("expected '{}', got '{}'", $exp, $s.here()))
        }
        $s.i += 1;
    )
}

#[derive(Debug)]
struct ParseState {
    i: usize,
    v: Vec<String>,
}

impl ParseState {
    fn new(v2: Vec<&str>) -> ParseState {
        ParseState {
            i: 0,
            v: v2.iter().map(|x| String::from(*x)).collect(),
        }
    }

    fn here(&self) -> String {
        (self.v[self.i]).clone()
    }

    fn next(&mut self) {
        self.i += 1;
    }

    fn eof(&self) -> bool {
        self.i >= self.v.len()
    }
}

fn assume_string(e: &'static str, s: &str) -> Result<()> {
    if *e != *s {
        return str_error(format!("expecting: {}, actually: {}", e, s));
    }
    Ok(())
}

fn i64_from_string(p: &ParseState, s: &str) -> Result<i64> {
    match i64::from_str(s) {
        Ok(i) => Ok(i),
        _ => str_error(format!("int parse error in {}; line: {}", s, p.here())),
    }
}

fn f64_from_string(p: &ParseState, s: &str) -> Result<f64> {
    match f64::from_str(s) {
        Ok(i) => Ok(i),
        _ => str_error(format!("float parse error in {}; line: {}", s, p.here())),
    }
}

fn bool_from_string(s: &str, t: &'static str, f: &'static str) -> Result<bool> {
    if &s[..] == t {
        return Ok(true);
    }
    if &s[..] == f {
        return Ok(false);
    }
    str_error(format!("unknown boolean {}, expected {} or {}", s, t, f))
}

fn char_at(s: &str, p: usize) -> char {
    let v: Vec<char> = s.chars().collect();
    v[..][p]
}

fn parse_symbol(p: &mut ParseState) -> Result<Symbol> {
    p.next(); // skip line like # name
    assume_line!(p, "#");
    let s = p.here();
    let v = &parse_split_quote_aware(&s);
    if v.len() != 10 {
        return str_error(format!("unexpected elements in {}", s));
    }
    assume_string("DEF", &v[0])?;
    let mut s = Symbol::new(v[1].clone(), v[2].clone());
    s.text_offset = f64_from_string(p, &v[4])?;
    s.draw_pinnumber = bool_from_string(&v[5], "Y", "N")?;
    s.draw_pinname = bool_from_string(&v[6], "Y", "N")?;
    s.unit_count = i64_from_string(p, &v[7])?;
    s.unit_locked = bool_from_string(&v[8], "L", "F")?;
    s.is_power = bool_from_string(&v[9], "P", "N")?;
    p.next();
    loop {
        let s2 = p.here();
        if char_at(&s2, 0) == 'F' {
            let f = parse_field(p, &s2)?;
            s.fields.push(f);
            p.next();
        } else {
            break;
        }
    }
    if &p.here() == "$FPLIST" {
        p.next();
        // skip FPLIST for now
        while !p.eof() {
            if &p.here() == "$ENDFPLIST" {
                p.next();
                break;
            }
            p.next()
        }
    }
    if p.here().starts_with("ALIAS") {
        let v = parse_split_quote_aware(&p.here());
        for alias in v.into_iter().skip(1) {
            s.aliases.push(alias)
        }
        p.next();
    }
    // TODO draw
    assume_line!(p, "DRAW");
    while !p.eof() {
        let s2 = p.here();
        if &s2 == "ENDDRAW" {
            p.next();
            break;
        }
        
        if s2.starts_with("X ") {
            let pin = parse_pin(p, &s2)?;
            s.draw.push(Draw::Pin(pin));
        } else {
            s.draw.push(Draw::Other(s2.clone()));
        }
        p.next()
    }
    assume_line!(p, "ENDDEF");
    assume_line!(p, "#");
    Ok(s)
}

fn bool_from<T: PartialEq + fmt::Display>(i: T, t: T, f: T) -> Result<bool> {
    if i == t {
        return Ok(true);
    }
    if i == f {
        return Ok(false);
    }
    str_error(format!("unknown boolean {}, expected {} or {}", i, t, f))
}

// F0 "L" 0 50 40 H V C CNN
fn parse_field(p: &mut ParseState, line: &str) -> Result<Field> {
    let mut f = Field::default();
    let v = &parse_split_quote_aware(line);
    if v.len() != 9 && v.len() != 10 {
        return str_error(format!("unexpected elements in {}", line));
    }
    f.i = i64_from_string(p, &String::from(&v[0][1..]))?;
    let name = if v.len() == 10 {
        v[9].clone()
    } else {
        match f.i {
            0 => String::from("Reference"),
            1 => String::from("Value"),
            2 => String::from("Footprint"),
            3 => String::from("UserDocLink"),
            _ => return str_error("expecting name for componentfield > 3".to_string()),
        }
    };
    f.value = v[1].clone();
    f.x = f64_from_string(p, &v[2])?;
    f.y = f64_from_string(p, &v[3])?;
    f.dimension = i64_from_string(p, &v[4])?;
    f.orientation = schematic::Orientation::new(char_at(&v[5], 0))?;
    f.visible = bool_from_string(&v[6], "V", "I")?;
    f.hjustify = schematic::Justify::new(char_at(&v[7], 0))?;
    f.vjustify = schematic::Justify::new(char_at(&v[8], 0))?;
    f.italic = bool_from(char_at(&v[8], 1), 'I', 'N')?;
    f.bold = bool_from(char_at(&v[8], 2), 'B', 'N')?;
    f.name = name;
    Ok(f)
}

// X +3.3V 1 0 0 0 U 30 30 0 0 W N

fn parse_pin(p:&mut ParseState, line: &str) -> Result<Pin> {
    let mut pin = Pin::default();
    let v = &parse_split_quote_aware(line);
    if v.len() != 12 && v.len() != 13 {
        return str_error(format!("unexpected elements in {}", line));
    }
    pin.name = v[1].clone();
    pin.number = v[2].clone();
    pin.x = i64_from_string(p, &v[3])?;
    pin.y = i64_from_string(p, &v[4])?;
    pin.len = i64_from_string(p, &v[5])?;
    pin.orientation = PinOrientation::from_str(&v[6])?;
    pin.num_size = i64_from_string(p, &v[7])?;
    pin.name_size = i64_from_string(p, &v[8])?;
    pin.unit = i64_from_string(p, &v[9])?;
    pin.convert = i64_from_string(p, &v[10])?;
    pin.pin_type = PinType::from_str(&v[11])?;
    pin.pin_visible = true;
    if v.len() == 13 {
        pin.pin_visible = PinShape::visible_from_str(&v[12]);
        pin.pin_shape = PinShape::from_str(&v[12])?;
    }
    Ok(pin)
}

fn parse(s: &str) -> Result<SymbolLib> {
    let mut lib = SymbolLib::default();
    let v: Vec<&str> = s.lines().collect();
    let p = &mut ParseState::new(v);
    assume_line!(p, "EESchema-LIBRARY Version 2.3");
    assume_line!(p, "#encoding utf-8");
    assume_line!(p, "#");
    while !p.eof() {
        // println!("here: {}", &p.here());
        if &p.here() == "#End Library" {
            break;
        }
        let s = parse_symbol(p)?;
        // println!("new symbol: {}", &s);
        lib.symbols.push(s)
    }
    Ok(lib)
}

/// parse a &str to a symbol lib
pub fn parse_str(s: &str) -> Result<SymbolLib> {
    parse(s)
}

/// parse a file to a symbol lib
pub fn parse_file(filename: &PathBuf) -> Result<SymbolLib> {
    let name = filename.to_str().unwrap();
    let s = read_file(name)?;
    parse(&s[..])
}