1use crate::geom::Point3;
8use crate::model::{
9 Attribute, AttributeDisplay, Block, Color, Entity, Format, HAlign, Layer, LengthUnit, Linetype,
10 Lineweight, Model, ModelKind, Props, Raw, SourceInfo, TextStyle, Units, VAlign, Value, Warning,
11};
12
13macro_rules! setters {
21 ($ty:ty { $($kind:ident $name:ident($field:ident: $fty:ty)),* $(,)? }) => {
22 impl $ty {
23 $(setters!(@one $kind $name $field $fty);)*
24 }
25 };
26 (@one plain $name:ident $field:ident $fty:ty) => {
27 #[doc = concat!("Sets [`Self::", stringify!($field), "`].")]
28 #[must_use]
29 pub fn $name(mut self, value: $fty) -> Self {
30 self.$field = value;
31 self
32 }
33 };
34 (@one into $name:ident $field:ident $fty:ty) => {
35 #[doc = concat!("Sets [`Self::", stringify!($field), "`].")]
36 #[must_use]
37 pub fn $name(mut self, value: impl Into<$fty>) -> Self {
38 self.$field = value.into();
39 self
40 }
41 };
42 (@one some $name:ident $field:ident $fty:ty) => {
43 #[doc = concat!("Sets [`Self::", stringify!($field), "`] to `Some(value)`.")]
44 #[must_use]
45 pub fn $name(mut self, value: $fty) -> Self {
46 self.$field = Some(value);
47 self
48 }
49 };
50 (@one some_into $name:ident $field:ident $fty:ty) => {
51 #[doc = concat!("Sets [`Self::", stringify!($field), "`] to `Some(value)`.")]
52 #[must_use]
53 pub fn $name(mut self, value: impl Into<$fty>) -> Self {
54 self.$field = Some(value.into());
55 self
56 }
57 };
58 (@one prop $name:ident $field:ident $fty:ty) => {
59 #[doc = concat!("Inserts one entry into [`Self::", stringify!($field), "`].")]
60 #[must_use]
61 pub fn $name(mut self, key: impl Into<String>, value: impl Into<$fty>) -> Self {
62 self.$field.insert(key.into(), value.into());
63 self
64 }
65 };
66}
67
68impl SourceInfo {
69 #[must_use]
71 pub fn new(format: Format, version: impl Into<String>) -> Self {
72 Self {
73 format,
74 version: version.into(),
75 ..Self::default()
76 }
77 }
78}
79
80setters!(SourceInfo {
81 some_into with_application(application: String),
82 some_into with_codepage(codepage: String),
83});
84
85impl Units {
86 #[must_use]
88 pub const fn new(unit: LengthUnit) -> Self {
89 Self {
90 unit,
91 meters_per_unit: None,
92 }
93 }
94}
95
96setters!(Units {
97 some with_meters_per_unit(meters_per_unit: f64),
98});
99
100impl Layer {
101 #[must_use]
104 pub fn new(name: impl Into<String>) -> Self {
105 Self {
106 name: name.into(),
107 visible: true,
108 plottable: true,
109 ..Self::default()
110 }
111 }
112}
113
114setters!(Layer {
115 into with_name(name: String),
116 some with_id(id: u64),
117 plain with_color(color: Color),
118 some_into with_linetype(linetype: String),
119 plain with_lineweight(lineweight: Lineweight),
120 plain with_visible(visible: bool),
121 plain with_frozen(frozen: bool),
122 plain with_locked(locked: bool),
123 plain with_plottable(plottable: bool),
124 some_into with_description(description: String),
125 plain with_props(props: Props),
126 prop with_prop(props: Value),
127});
128
129impl Linetype {
130 #[must_use]
133 pub fn new(name: impl Into<String>, pattern: Vec<f64>) -> Self {
134 Self {
135 name: name.into(),
136 pattern,
137 ..Self::default()
138 }
139 }
140}
141
142setters!(Linetype {
143 some_into with_description(description: String),
144 plain with_props(props: Props),
145 prop with_prop(props: Value),
146});
147
148impl TextStyle {
149 #[must_use]
151 pub fn new(name: impl Into<String>) -> Self {
152 Self {
153 name: name.into(),
154 width_factor: 1.0,
155 ..Self::default()
156 }
157 }
158}
159
160setters!(TextStyle {
161 some_into with_font(font: String),
162 plain with_height(height: f64),
163 plain with_width_factor(width_factor: f64),
164 plain with_oblique(oblique: f64),
165 plain with_props(props: Props),
166 prop with_prop(props: Value),
167});
168
169impl Block {
170 #[must_use]
172 pub fn new(name: impl Into<String>) -> Self {
173 Self {
174 name: name.into(),
175 ..Self::default()
176 }
177 }
178}
179
180setters!(Block {
181 some with_id(id: u64),
182 plain with_base_point(base_point: Point3),
183 plain with_entities(entities: Vec<Entity>),
184 plain with_is_xref(is_xref: bool),
185 some_into with_xref_path(xref_path: String),
186 some_into with_description(description: String),
187 plain with_props(props: Props),
188 prop with_prop(props: Value),
189});
190
191impl Model {
192 #[must_use]
194 pub fn new(name: impl Into<String>, kind: ModelKind) -> Self {
195 Self {
196 name: name.into(),
197 kind,
198 ..Self::default()
199 }
200 }
201}
202
203setters!(Model {
204 some with_id(id: u64),
205 plain with_is_3d(is_3d: bool),
206 plain with_entities(entities: Vec<Entity>),
207 plain with_props(props: Props),
208 prop with_prop(props: Value),
209});
210
211setters!(Entity {
212 some with_id(id: u64),
213 plain with_kind(kind: crate::model::EntityKind),
214 plain with_attributes(attributes: Vec<Attribute>),
215 some with_raw(raw: Raw),
216 plain with_props(props: Props),
217});
218
219impl Attribute {
220 #[must_use]
222 pub fn new(tag: impl Into<String>, value: impl Into<Value>) -> Self {
223 Self {
224 tag: tag.into(),
225 value: value.into(),
226 ..Self::default()
227 }
228 }
229}
230
231setters!(Attribute {
232 some_into with_set(set: String),
233 some with_position(position: Point3),
234 plain with_invisible(invisible: bool),
235 some_into with_layer(layer: String),
236 some with_display(display: AttributeDisplay),
237 some with_color(color: Color),
238 some with_lineweight(lineweight: Lineweight),
239 plain with_props(props: Props),
240 prop with_prop(props: Value),
241});
242
243impl AttributeDisplay {
244 #[must_use]
247 pub fn new(height: f64, width: f64) -> Self {
248 Self {
249 height,
250 width,
251 ..Self::default()
252 }
253 }
254}
255
256setters!(AttributeDisplay {
257 some_into with_style(style: String),
258 plain with_halign(halign: HAlign),
259 plain with_valign(valign: VAlign),
260 plain with_rotation(rotation: f64),
261});
262
263impl Warning {
264 #[must_use]
266 pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
267 Self {
268 code: code.into(),
269 message: message.into(),
270 offset: None,
271 object: None,
272 }
273 }
274}
275
276setters!(Warning {
277 some with_offset(offset: u64),
278 some with_object(object: u64),
279});
280
281#[cfg(test)]
282mod tests {
283 use super::*;
284
285 #[test]
286 fn constructors_set_documented_defaults() {
287 let layer = Layer::new("A")
288 .with_color(Color::aci(1))
289 .with_prop("dgn.level_id", 7i64);
290 assert!(layer.visible && layer.plottable && !layer.frozen && !layer.locked);
291 assert_eq!(layer.props.len(), 1);
292 assert_eq!(TextStyle::new("S").width_factor, 1.0);
293 let warning = Warning::new("x.code", "message").with_offset(4);
294 assert_eq!((warning.offset, warning.object), (Some(4), None));
295 let units = Units::new(LengthUnit::Custom).with_meters_per_unit(0.3);
296 assert_eq!(units.meters_per_unit, Some(0.3));
297 }
298}