Skip to main content

reda_gds/models/primitive/
mod.rs

1mod presentation;
2mod transform;
3
4pub use presentation::*;
5use reda_geometry::shape::Point;
6pub use transform::*;
7
8use std::fmt;
9use chrono::{Datelike, Timelike, Local};
10
11/// This record contains a value that describes the type of path endpoints. The value is
12/// - 0 for square-ended paths that endflush with their endpoints
13/// - 1 for round-ended paths
14/// - 2 for square-ended paths that extend a half-width beyond their endpoints
15#[derive(Debug, Clone, Copy)]
16pub enum GdsPathType {
17    SquareEnd = 0,
18    RoundEnd = 1,
19    SquareEndExtend = 2,
20}
21
22impl Default for GdsPathType {
23    fn default() -> Self {
24        Self::SquareEnd
25    }
26}
27
28impl GdsPathType {
29    pub fn from_u16(value: u16) -> Option<Self> {
30        match value {
31            0 => Some(Self::SquareEnd),
32            1 => Some(Self::RoundEnd),
33            2 => Some(Self::SquareEndExtend),
34            _ => None,
35        }
36    }
37
38    pub fn to_u16(&self) -> u16 {
39        match self {
40            Self::SquareEnd => 0,
41            Self::RoundEnd => 1,
42            Self::SquareEndExtend => 2,
43        }
44    }
45}
46
47impl std::fmt::Display for GdsPathType {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        let description = match self {
50            GdsPathType::SquareEnd => "SquareEnd (0)",
51            GdsPathType::RoundEnd => "RoundEnd (1)",
52            GdsPathType::SquareEndExtend => "SquareEndExtend (2)",
53        };
54        write!(f, "{}", description)
55    }
56}
57
58/// Defines the format of a Stream tape in two bytes. The possible values are:
59/// 1. for GDSII Archive format
60/// 2. for GDSII Filtered format
61/// 3. for EDSM Archive format
62/// 4. for EDSHI Filtered forrnat
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub enum GdsFormat {
65    /// GDSII Archive format
66    GdsiiArchive = 0,
67    /// GDSII Filtered format
68    GdsiiFiltered = 1,
69    /// EDSM Archive format
70    EdsmArchive = 2,
71    /// EDSHI Filtered format
72    EdshiFiltered = 3,
73}
74
75impl GdsFormat {
76    /// Converts a `u16` value into a `GdsFormat`, if valid.
77    pub fn from_u16(value: u16) -> Option<Self> {
78        match value {
79            0 => Some(GdsFormat::GdsiiArchive),
80            1 => Some(GdsFormat::GdsiiFiltered),
81            2 => Some(GdsFormat::EdsmArchive),
82            3 => Some(GdsFormat::EdshiFiltered),
83            _ => None,
84        }
85    }
86
87    /// Converts a `GdsFormat` into a `u16` value.
88    pub fn to_u16(self) -> u16 {
89        self as u16
90    }
91}
92
93#[derive(Debug, Clone)]
94pub struct GdsDateTime {
95    pub year: i16,
96    pub month: i16,
97    pub day: i16,
98    pub hour: i16,
99    pub minute: i16,
100    pub second: i16,
101}
102
103impl Default for GdsDateTime {
104    fn default() -> Self {
105        Self::now()
106    }
107}
108
109impl GdsDateTime {
110    pub fn now() -> Self {
111        let now = Local::now();
112        GdsDateTime {
113            year: now.year() as i16,
114            month: now.month() as i16,
115            day: now.day() as i16,
116            hour: now.hour() as i16,
117            minute: now.minute() as i16,
118            second: now.second() as i16,
119        }
120    }
121}
122
123impl fmt::Display for GdsDateTime {
124    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125        write!(f, "{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
126            self.year, self.month, self.day, self.hour, self.minute, self.second)
127    }
128}
129
130pub type GdsCoord = Point<i32>;
131