vmf_forge/vmf/
world.rs

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
717
718
719
720
721
//! This module provides structures for representing the world block in a VMF file, which contains world geometry, hidden entities, and groups.

use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

use super::common::Editor;
use crate::utils::{get_key, parse_hs_key, To01String};
use crate::{
    errors::{VmfError, VmfResult},
    VmfBlock, VmfSerializable,
};

/// Represents the world block in a VMF file.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct World {
    /// The key-value pairs associated with the world.
    pub key_values: IndexMap<String, String>,
    /// The list of solids that make up the world geometry.
    pub solids: Vec<Solid>,
    /// The list of hidden solids in the world.
    pub hidden: Vec<Solid>,
    /// The groups present in the world, if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub group: Option<Group>,
}

impl TryFrom<VmfBlock> for World {
    type Error = VmfError;

    fn try_from(block: VmfBlock) -> VmfResult<Self> {
        let mut world = World {
            key_values: block.key_values,
            ..Default::default()
        };

        for inner_block in block.blocks {
            match inner_block.name.as_str() {
                "solid" => world.solids.push(Solid::try_from(inner_block)?),
                "group" => world.group = Group::try_from(inner_block).ok(),
                "hidden" => {
                    if let Some(hidden_block) = inner_block.blocks.first() {
                        world.hidden.push(Solid::try_from(hidden_block.to_owned())?);
                    }
                }
                _ => {
                    // The `world` block does not support other types of blocks (except `hidden`, `group` and `solid`)
                    debug_assert!(false, "Unexpected block name: {}", inner_block.name);
                }
            };
        }

        Ok(world)
    }
}

impl Into<VmfBlock> for World {
    fn into(self) -> VmfBlock {
        let mut blocks = Vec::new();

        // Add solids
        for solid in self.solids {
            blocks.push(solid.into());
        }

        // Add hidden solids
        for hidden_solid in self.hidden {
            blocks.push(VmfBlock {
                name: "hidden".to_string(),
                key_values: IndexMap::new(),
                blocks: vec![hidden_solid.into()],
            });
        }

        // Add groups
        if let Some(group) = self.group {
            blocks.push(group.into());
        }

        VmfBlock {
            name: "world".to_string(),
            key_values: self.key_values,
            blocks,
        }
    }
}

impl VmfSerializable for World {
    fn to_vmf_string(&self, indent_level: usize) -> String {
        let indent = "\t".repeat(indent_level);
        let mut output = String::with_capacity(2048);

        output.push_str(&format!("{0}world\n{0}{{\n", indent));

        // Adds key_values of the main block
        for (key, value) in &self.key_values {
            output.push_str(&format!("{}\t\"{}\" \"{}\"\n", indent, key, value));
        }

        // Solids Block
        if self.solids.len() > 0 {
            for solid in &self.solids {
                output.push_str(&solid.to_vmf_string(indent_level + 1));
            }
        }

        // Hidden Solids Block
        if self.hidden.len() > 0 {
            output.push_str(&format!("{0}\tHidden\n{0}\t{{\n", indent));
            for solid in &self.hidden {
                output.push_str(&solid.to_vmf_string(indent_level + 2));
            }
            output.push_str(&format!("{}\t}}\n", indent));
        }

        // Group Block
        if let Some(group) = &self.group {
            output.push_str(&group.to_vmf_string(indent_level + 1));
        }

        output.push_str(&format!("{}}}\n", indent));
        output
    }
}

/// Represents a solid object in the VMF world.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Solid {
    /// The unique ID of the solid.
    pub id: u64,
    /// The sides of the solid.
    pub sides: Vec<Side>,
    /// The editor data for the solid.
    pub editor: Editor,
}

impl TryFrom<VmfBlock> for Solid {
    type Error = VmfError;

    fn try_from(block: VmfBlock) -> VmfResult<Self> {
        let mut solid = Solid {
            id: parse_hs_key!(&block.key_values, "id", u64)?,
            sides: Vec::with_capacity(4),
            ..Default::default()
        };

        for inner_block in block.blocks {
            match inner_block.name.as_str() {
                "side" => solid.sides.push(Side::try_from(inner_block)?),
                "editor" => solid.editor = Editor::try_from(inner_block)?,
                _ => {
                    debug_assert!(false, "Unexpected block name: {}", inner_block.name);
                }
            }
        }

        Ok(solid)
    }
}

impl Into<VmfBlock> for Solid {
    fn into(self) -> VmfBlock {
        let mut blocks = Vec::new();

        // Adds sides
        for side in self.sides {
            blocks.push(side.into());
        }

        // Adds editor
        blocks.push(self.editor.into());

        VmfBlock {
            name: "solid".to_string(),
            key_values: {
                let mut key_values = IndexMap::new();
                key_values.insert("id".to_string(), self.id.to_string());
                key_values
            },
            blocks,
        }
    }
}

impl VmfSerializable for Solid {
    fn to_vmf_string(&self, indent_level: usize) -> String {
        let indent = "\t".repeat(indent_level);
        let mut output = String::with_capacity(256);

        // Start of solid block
        output.push_str(&format!("{0}solid\n{0}{{\n", indent));
        output.push_str(&format!("{}\t\"id\" \"{}\"\n", indent, self.id));

        // Sides
        for side in &self.sides {
            output.push_str(&side.to_vmf_string(indent_level + 1));
        }

        // Editor block
        output.push_str(&self.editor.to_vmf_string(indent_level + 1));

        output.push_str(&format!("{}}}\n", indent));

        output
    }
}

/// Represents a side of a solid object in the VMF world.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Side {
    /// The unique ID of the side.
    pub id: u32,
    /// The plane equation of the side.
    pub plane: String,
    /// The material used on the side.
    pub material: String,
    /// The U axis of the texture coordinates.
    pub u_axis: String,
    /// The V axis of the texture coordinates.
    pub v_axis: String,
    /// The rotation of the texture.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rotation: Option<f32>,
    /// The scale of the lightmap.
    pub lightmap_scale: u16,
    /// The smoothing groups that this side belongs to.
    pub smoothing_groups: i32,
    /// flags
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags: Option<u32>,
    /// The displacement info of the side, if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dispinfo: Option<DispInfo>,
}

impl TryFrom<VmfBlock> for Side {
    type Error = VmfError;

    fn try_from(block: VmfBlock) -> VmfResult<Self> {
        let kv = &block.key_values;
        let dispinfo_block = block.blocks.iter().find(|b| b.name == "dispinfo");

        Ok(Side {
            id: parse_hs_key!(kv, "id", u32)?,
            plane: get_key!(kv, "plane")?.to_owned(),
            material: get_key!(kv, "material")?.to_owned(),
            u_axis: get_key!(kv, "uaxis")?.to_owned(),
            v_axis: get_key!(kv, "vaxis")?.to_owned(),
            rotation: get_key!(kv, "rotation", "_".into()).parse().ok(),
            lightmap_scale: parse_hs_key!(kv, "lightmapscale", u16)?,
            smoothing_groups: parse_hs_key!(kv, "smoothing_groups", i32)?,
            flags: get_key!(kv, "flags", "_".into()).parse().ok(),
            dispinfo: match dispinfo_block {
                Some(block) => Some(DispInfo::try_from(block.clone())?), // todo: clone :<
                None => None,
            },
        })
    }
}

impl Into<VmfBlock> for Side {
    fn into(self) -> VmfBlock {
        let mut key_values = IndexMap::new();
        key_values.insert("id".to_string(), self.id.to_string());
        key_values.insert("plane".to_string(), self.plane);
        key_values.insert("material".to_string(), self.material);
        key_values.insert("uaxis".to_string(), self.u_axis);
        key_values.insert("vaxis".to_string(), self.v_axis);
        if let Some(rotation) = self.rotation {
            key_values.insert("rotation".to_string(), rotation.to_string());
        }
        key_values.insert("lightmapscale".to_string(), self.lightmap_scale.to_string());
        key_values.insert(
            "smoothing_groups".to_string(),
            self.smoothing_groups.to_string(),
        );
        if let Some(flags) = self.flags {
            key_values.insert("flags".to_string(), flags.to_string());
        }

        VmfBlock {
            name: "side".to_string(),
            key_values,
            blocks: Vec::new(),
        }
    }
}

impl VmfSerializable for Side {
    fn to_vmf_string(&self, indent_level: usize) -> String {
        let indent = "\t".repeat(indent_level);
        let mut output = String::with_capacity(256);

        // Start of Side block
        output.push_str(&format!("{0}side\n{0}{{\n", indent));

        // Writes all key-value pairs with appropriate indentation
        output.push_str(&format!("{}\t\"id\" \"{}\"\n", indent, self.id));
        output.push_str(&format!("{}\t\"plane\" \"{}\"\n", indent, self.plane));
        output.push_str(&format!("{}\t\"material\" \"{}\"\n", indent, self.material));
        output.push_str(&format!("{}\t\"uaxis\" \"{}\"\n", indent, self.u_axis));
        output.push_str(&format!("{}\t\"vaxis\" \"{}\"\n", indent, self.v_axis));

        if let Some(rotation) = self.rotation {
            output.push_str(&format!("{}\t\"rotation\" \"{}\"\n", indent, rotation));
        }

        output.push_str(&format!(
            "{}\t\"lightmapscale\" \"{}\"\n",
            indent, self.lightmap_scale
        ));
        output.push_str(&format!(
            "{}\t\"smoothing_groups\" \"{}\"\n",
            indent, self.smoothing_groups
        ));

        // Adds the flag if it exists
        if let Some(flags) = self.flags {
            output.push_str(&format!("{}\t\"flags\" \"{}\"\n", indent, flags));
        }

        if let Some(dispinfo) = &self.dispinfo {
            output.push_str(&dispinfo.to_vmf_string(indent_level + 1));
        }

        // End of Side block
        output.push_str(&format!("{0}}}\n", indent));

        output
    }
}

/// Looks for a block with the specified name in a vector of `VmfBlock`s.
///
/// # Arguments
///
/// * `blocks` - A slice of `VmfBlock`s to search in.
/// * `name` - The name of the block to search for.
///
/// # Returns
///
/// A `Result` containing a reference to the first `VmfBlock` with the specified name, or a `VmfError` if no such block is found.
macro_rules! find_block {
    ($blocks:expr, $name:expr) => {
        $blocks.iter().find(|b| b.name == $name).ok_or_else(|| {
            VmfError::InvalidFormat(format!("Missing {} block in dispinfo", $name))
        })?
    };
}

/// Represents the displacement information for a side.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct DispInfo {
    /// The power of the displacement map (2, 3, or 4).
    pub power: u8,
    /// The starting position of the displacement.
    pub start_position: String,
    /// Flags for the displacement.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags: Option<u32>,
    /// The elevation of the displacement.
    pub elevation: f32,
    /// Whether the displacement is subdivided.
    pub subdiv: bool,
    /// The normals for each vertex in the displacement.
    pub normals: DispRows,
    /// The distances for each vertex in the displacement.
    pub distances: DispRows,
    /// The offsets for each vertex in the displacement.
    pub offsets: DispRows,
    /// The offset normals for each vertex in the displacement.
    pub offset_normals: DispRows,
    /// The alpha values for each vertex in the displacement.
    pub alphas: DispRows,
    /// The triangle tags for the displacement.
    pub triangle_tags: DispRows,
    /// The allowed vertices for the displacement.
    pub allowed_verts: IndexMap<String, Vec<i32>>,
}

impl TryFrom<VmfBlock> for DispInfo {
    type Error = VmfError;

    fn try_from(block: VmfBlock) -> VmfResult<Self> {
        let normals_block = find_block!(block.blocks, "normals");
        let distances_block = find_block!(block.blocks, "distances");
        let offsets_block = find_block!(block.blocks, "offsets");
        let offset_normals_block = find_block!(block.blocks, "offset_normals");
        let alphas_block = find_block!(block.blocks, "alphas");
        let triangle_tags_block = find_block!(block.blocks, "triangle_tags");
        let allowed_verts_block = find_block!(block.blocks, "allowed_verts");

        let kv = &block.key_values;
        Ok(DispInfo {
            power: parse_hs_key!(kv, "power", u8)?,
            start_position: get_key!(kv, "startposition")?.to_string(),
            flags: get_key!(kv, "flags", "_".into()).parse().ok(),
            elevation: get_key!(kv, "elevation")?.parse()?,
            subdiv: get_key!(kv, "subdiv")? == "1",
            normals: DispRows::try_from(normals_block.clone())?,
            distances: DispRows::try_from(distances_block.clone())?,
            offsets: DispRows::try_from(offsets_block.clone())?,
            offset_normals: DispRows::try_from(offset_normals_block.clone())?,
            alphas: DispRows::try_from(alphas_block.clone())?,
            triangle_tags: DispRows::try_from(triangle_tags_block.clone())?,
            allowed_verts: DispInfo::parse_allowed_verts(allowed_verts_block)?,
        })
    }
}

impl Into<VmfBlock> for DispInfo {
    fn into(self) -> VmfBlock {
        let blocks = vec![
            self.normals.into_vmf_block("normals"),
            self.distances.into_vmf_block("distances"),
            self.offsets.into_vmf_block("offsets"),
            self.offset_normals.into_vmf_block("offset_normals"),
            self.alphas.into_vmf_block("alphas"),
            self.triangle_tags.into_vmf_block("triangle_tags"),
            Self::allowed_verts_into_vmf_block(self.allowed_verts),
        ];

        let mut key_values = IndexMap::new();
        key_values.insert("power".to_string(), self.power.to_string());
        key_values.insert("startposition".to_string(), self.start_position);
        key_values.insert("elevation".to_string(), self.elevation.to_string());
        key_values.insert("subdiv".to_string(), self.subdiv.to_01_string());

        if let Some(flags) = self.flags {
            key_values.insert("flags".to_string(), flags.to_string());
        }

        VmfBlock {
            name: "dispinfo".to_string(),
            key_values,
            blocks,
        }
    }
}

impl VmfSerializable for DispInfo {
    fn to_vmf_string(&self, indent_level: usize) -> String {
        let indent = "\t".repeat(indent_level);
        let mut output = String::with_capacity(256);

        output.push_str(&format!("{}dispinfo\n", indent));
        output.push_str(&format!("{}{{\n", indent));
        output.push_str(&format!("{}\t\"power\" \"{}\"\n", indent, self.power));
        output.push_str(&format!(
            "{}\t\"startposition\" \"{}\"\n",
            indent, self.start_position
        ));

        // Adds the flag if it exists
        if let Some(flags) = self.flags {
            output.push_str(&format!("{}\t\"flags\" \"{}\"\n", indent, flags));
        }

        output.push_str(&format!(
            "{}\t\"elevation\" \"{}\"\n",
            indent, self.elevation
        ));
        output.push_str(&format!(
            "{}\t\"subdiv\" \"{}\"\n",
            indent,
            self.subdiv.to_01_string()
        ));
        output.push_str(&self.normals.to_vmf_string(indent_level + 1, "normals"));
        output.push_str(&self.distances.to_vmf_string(indent_level + 1, "distances"));
        output.push_str(&self.offsets.to_vmf_string(indent_level + 1, "offsets"));
        output.push_str(
            &self
                .offset_normals
                .to_vmf_string(indent_level + 1, "offset_normals"),
        );
        output.push_str(&self.alphas.to_vmf_string(indent_level + 1, "alphas"));
        output.push_str(
            &self
                .triangle_tags
                .to_vmf_string(indent_level + 1, "triangle_tags"),
        );
        output.push_str(&Self::allowed_verts_to_vmf_string(
            &self.allowed_verts,
            indent_level + 1,
        ));
        output.push_str(&format!("{}}}\n", indent));

        output
    }
}

impl DispInfo {
    /// Parses the allowed vertices from a `VmfBlock`.
    ///
    /// # Arguments
    ///
    /// * `block` - The `VmfBlock` containing the allowed vertices data.
    ///
    /// # Returns
    ///
    /// A `Result` containing an `IndexMap` of allowed vertices, or a `VmfError` if parsing fails.
    fn parse_allowed_verts(block: &VmfBlock) -> VmfResult<IndexMap<String, Vec<i32>>> {
        let mut allowed_verts = IndexMap::new();
        for (key, value) in &block.key_values {
            let verts: VmfResult<Vec<i32>> = value
                .split_whitespace()
                .map(|s| {
                    s.parse::<i32>()
                        .map_err(|e| VmfError::ParseInt(e, s.to_string()))
                })
                .collect();
            allowed_verts.insert(key.clone(), verts?);
        }
        Ok(allowed_verts)
    }

    /// Converts the allowed vertices data into a `VmfBlock`.
    ///
    /// # Arguments
    ///
    /// * `allowed_verts` - The `IndexMap` containing the allowed vertices data.
    ///
    /// # Returns
    ///
    /// A `VmfBlock` representing the allowed vertices data.
    fn allowed_verts_into_vmf_block(allowed_verts: IndexMap<String, Vec<i32>>) -> VmfBlock {
        let mut key_values = IndexMap::new();
        for (key, values) in allowed_verts {
            key_values.insert(
                key,
                values
                    .iter()
                    .map(|v| v.to_string())
                    .collect::<Vec<String>>()
                    .join(" "),
            );
        }

        VmfBlock {
            name: "allowed_verts".to_string(),
            key_values,
            blocks: Vec::new(),
        }
    }

    /// Converts the allowed vertices data into a string representation.
    ///
    /// # Arguments
    ///
    /// * `allowed_verts` - A reference to an `IndexMap` containing the allowed vertices data.
    /// * `indent_level` - The indentation level for formatting.
    ///
    /// # Returns
    ///
    /// A string representation of the allowed vertices data.
    fn allowed_verts_to_vmf_string(
        allowed_verts: &IndexMap<String, Vec<i32>>,
        indent_level: usize,
    ) -> String {
        let indent = "\t".repeat(indent_level);
        let mut output = String::new();

        output.push_str(&format!("{}allowed_verts\n", indent));
        output.push_str(&format!("{}{{\n", indent));
        for (key, values) in allowed_verts {
            output.push_str(&format!(
                "{}\t\"{}\" \"{}\"\n",
                indent,
                key,
                values
                    .iter()
                    .map(|v| v.to_string())
                    .collect::<Vec<String>>()
                    .join(" ")
            ));
        }
        output.push_str(&format!("{}}}\n", indent));

        output
    }
}

/// Represents rows of data for displacement information, such as normals, distances, offsets, etc.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct DispRows {
    /// The rows of data, each represented as a string.
    pub rows: Vec<String>,
}

impl TryFrom<VmfBlock> for DispRows {
    type Error = VmfError;

    fn try_from(block: VmfBlock) -> VmfResult<Self> {
        let mut rows = Vec::with_capacity(block.key_values.len());
        for (key, value) in block.key_values {
            if key.starts_with("row") {
                let index = key[3..]
                    .parse::<usize>()
                    .map_err(|e| VmfError::ParseInt(e, key.to_string()))?;
                if index >= rows.len() {
                    rows.resize(index + 1, String::new());
                }
                rows[index] = value;
            }
        }
        Ok(DispRows { rows })
    }
}

impl DispRows {
    /// Converts the `DispRows` data into a `VmfBlock` with the specified name.
    ///
    /// # Arguments
    ///
    /// * `self` - The `DispRows` instance to convert.
    /// * `name` - The name of the block.
    ///
    /// # Returns
    ///
    /// A `VmfBlock` representing the `DispRows` data.
    fn into_vmf_block(self, name: &str) -> VmfBlock {
        let mut key_values = IndexMap::new();
        for (i, row) in self.rows.into_iter().enumerate() {
            key_values.insert(format!("row{}", i), row);
        }

        VmfBlock {
            name: name.to_string(),
            key_values,
            blocks: Vec::new(),
        }
    }

    /// Converts the `DispRows` data into a string representation with the specified name and indentation level.
    ///
    /// # Arguments
    ///
    /// * `self` - A reference to the `DispRows` instance.
    /// * `indent_level` - The indentation level for formatting.
    /// * `name` - The name of the block.
    ///
    /// # Returns
    ///
    /// A string representation of the `DispRows` data.
    fn to_vmf_string(&self, indent_level: usize, name: &str) -> String {
        let indent = "\t".repeat(indent_level);
        let mut output = String::with_capacity(32);

        output.push_str(&format!("{}{}\n", indent, name));
        output.push_str(&format!("{}{{\n", indent));
        for (i, row) in self.rows.iter().enumerate() {
            output.push_str(&format!("{}\t\"row{}\" \"{}\"\n", indent, i, row));
        }
        output.push_str(&format!("{}}}\n", indent));

        output
    }
}

/// Represents a group in the VMF world.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Group {
    /// The unique ID of the group.
    pub id: u32,
    /// The editor data for the group.
    pub editor: Editor,
}

impl TryFrom<VmfBlock> for Group {
    type Error = VmfError;

    fn try_from(block: VmfBlock) -> VmfResult<Self> {
        let mut editor = None;
        for inner_block in block.blocks {
            if inner_block.name.eq_ignore_ascii_case("editor") {
                editor = Some(Editor::try_from(inner_block)?);
            }
        }

        Ok(Self {
            id: parse_hs_key!(&block.key_values, "id", u32)?,
            editor: editor.unwrap_or_default(),
        })
    }
}

impl Into<VmfBlock> for Group {
    fn into(self) -> VmfBlock {
        let mut blocks = Vec::with_capacity(2);

        // Adds Editor block
        blocks.push(self.editor.into());

        VmfBlock {
            name: "group".to_string(),
            key_values: {
                let mut key_values = IndexMap::new();
                key_values.insert("id".to_string(), self.id.to_string());
                key_values
            },
            blocks,
        }
    }
}

impl VmfSerializable for Group {
    fn to_vmf_string(&self, indent_level: usize) -> String {
        let indent = "\t".repeat(indent_level);
        let mut output = String::with_capacity(64);

        // Writes the main entity block
        output.push_str(&format!("{0}group\n{0}{{\n", indent));
        output.push_str(&format!("{}\t\"id\" \"{}\"\n", indent, self.id));

        // Editor block
        output.push_str(&self.editor.to_vmf_string(indent_level + 1));

        output.push_str(&format!("{}}}\n", indent));

        output
    }
}