panlabel 0.6.0

The universal annotation converter
Documentation
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
//! CVAT XML reader and writer.
//!
//! This adapter supports CVAT "for images" task-export XML:
//! - single `annotations.xml` file
//! - root `<annotations>` containing `<image>` entries
//! - `<box>` elements only (object-detection bboxes)

use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::fmt::Write as _;
use std::fs;
use std::path::{Path, PathBuf};

use roxmltree::{Document, Node};

use super::model::{Annotation, Category, Dataset, DatasetInfo, Image};
use super::{AnnotationId, BBoxXYXY, CategoryId, ImageId, Pixel};
use crate::error::PanlabelError;

const CVAT_XML_FILE_NAME: &str = "annotations.xml";

/// Read a CVAT XML file or directory containing `annotations.xml` into IR.
pub fn read_cvat_xml(path: &Path) -> Result<Dataset, PanlabelError> {
    let resolved = resolve_cvat_xml_path(path)?;
    let xml = fs::read_to_string(&resolved).map_err(PanlabelError::Io)?;
    parse_cvat_xml_str(&xml, &resolved)
}

/// Write an IR dataset as CVAT XML.
///
/// - If `path` ends with `.xml`, writes directly to that file.
/// - Otherwise, treats `path` as a directory and writes `annotations.xml` inside it.
pub fn write_cvat_xml(path: &Path, dataset: &Dataset) -> Result<(), PanlabelError> {
    let (_out_dir, out_file) = resolve_cvat_output_path(path);
    if let Some(parent) = out_file.parent() {
        fs::create_dir_all(parent).map_err(PanlabelError::Io)?;
    }

    let xml = build_cvat_xml(dataset, &out_file)?;
    fs::write(&out_file, xml).map_err(PanlabelError::Io)
}

/// Parse CVAT XML from a string.
pub fn from_cvat_xml_str(xml: &str) -> Result<Dataset, PanlabelError> {
    parse_cvat_xml_str(xml, Path::new("<string>"))
}

/// Parse CVAT XML from bytes (must be valid UTF-8).
pub fn from_cvat_xml_slice(bytes: &[u8]) -> Result<Dataset, PanlabelError> {
    let xml = std::str::from_utf8(bytes).map_err(|source| PanlabelError::CvatXmlParse {
        path: PathBuf::from("<bytes>"),
        message: format!("input is not valid UTF-8: {source}"),
    })?;
    parse_cvat_xml_str(xml, Path::new("<bytes>"))
}

/// Serialize an IR dataset to a CVAT XML string.
pub fn to_cvat_xml_string(dataset: &Dataset) -> Result<String, PanlabelError> {
    build_cvat_xml(dataset, Path::new("<string>"))
}

#[derive(Debug)]
struct ParsedBox {
    label: String,
    bbox: BBoxXYXY<Pixel>,
    occluded: bool,
    z_order: Option<i32>,
    source: Option<String>,
    attributes: BTreeMap<String, String>,
}

#[derive(Debug)]
struct ParsedImage {
    name: String,
    width: u32,
    height: u32,
    cvat_id: Option<u64>,
    boxes: Vec<ParsedBox>,
}

#[derive(Clone, Debug)]
struct MetaLabels {
    all: BTreeSet<String>,
    bbox_or_unknown: BTreeSet<String>,
}

fn parse_cvat_xml_str(xml: &str, path: &Path) -> Result<Dataset, PanlabelError> {
    let document = Document::parse(xml).map_err(|source| PanlabelError::CvatXmlParse {
        path: path.to_path_buf(),
        message: source.to_string(),
    })?;

    let root = document.root_element();
    if root.tag_name().name() != "annotations" {
        return Err(PanlabelError::CvatXmlParse {
            path: path.to_path_buf(),
            message: "missing <annotations> root element".to_string(),
        });
    }

    let meta_labels = extract_meta_labels(root, path)?;

    let mut seen_image_names = BTreeSet::new();
    let mut parsed_images = Vec::new();
    let mut referenced_labels: BTreeSet<String> = BTreeSet::new();

    for image_node in root
        .children()
        .filter(|n| n.is_element() && n.tag_name().name() == "image")
    {
        let parsed = parse_image_element(image_node, path, meta_labels.as_ref())?;
        if !seen_image_names.insert(parsed.name.clone()) {
            return Err(PanlabelError::CvatXmlParse {
                path: path.to_path_buf(),
                message: format!(
                    "duplicate image name: '{}' appears in multiple <image> elements",
                    parsed.name
                ),
            });
        }

        for b in &parsed.boxes {
            referenced_labels.insert(b.label.clone());
        }

        parsed_images.push(parsed);
    }

    let category_names: BTreeSet<String> = match &meta_labels {
        Some(meta) => {
            let mut out = meta.bbox_or_unknown.clone();
            out.extend(referenced_labels);
            out
        }
        None => referenced_labels,
    };

    let categories: Vec<Category> = category_names
        .into_iter()
        .enumerate()
        .map(|(idx, name)| Category::new((idx + 1) as u64, name))
        .collect();

    let category_id_by_name: BTreeMap<String, CategoryId> =
        categories.iter().map(|c| (c.name.clone(), c.id)).collect();

    parsed_images.sort_by(|a, b| a.name.cmp(&b.name));

    let mut images = Vec::with_capacity(parsed_images.len());
    let mut image_id_by_name: BTreeMap<String, ImageId> = BTreeMap::new();

    for (idx, parsed) in parsed_images.iter().enumerate() {
        let mut image = Image::new(
            (idx + 1) as u64,
            parsed.name.clone(),
            parsed.width,
            parsed.height,
        );
        if let Some(cvat_id) = parsed.cvat_id {
            image
                .attributes
                .insert("cvat_image_id".to_string(), cvat_id.to_string());
        }
        image_id_by_name.insert(parsed.name.clone(), image.id);
        images.push(image);
    }

    let mut annotations = Vec::new();
    let mut next_ann_id: u64 = 1;

    for parsed_img in parsed_images {
        let image_id = image_id_by_name
            .get(&parsed_img.name)
            .copied()
            .ok_or_else(|| PanlabelError::CvatXmlParse {
                path: path.to_path_buf(),
                message: format!(
                    "internal error: missing image mapping for '{}'",
                    parsed_img.name
                ),
            })?;

        for parsed_box in parsed_img.boxes {
            let category_id = category_id_by_name
                .get(&parsed_box.label)
                .copied()
                .ok_or_else(|| PanlabelError::CvatXmlParse {
                    path: path.to_path_buf(),
                    message: format!(
                        "internal error: missing category mapping for '{}'",
                        parsed_box.label
                    ),
                })?;

            let mut ann = Annotation::new(
                AnnotationId::new(next_ann_id),
                image_id,
                category_id,
                parsed_box.bbox,
            );

            let mut attrs = parsed_box.attributes;
            if parsed_box.occluded {
                attrs.insert("occluded".to_string(), "1".to_string());
            }
            if let Some(z) = parsed_box.z_order.filter(|z| *z != 0) {
                attrs.insert("z_order".to_string(), z.to_string());
            }
            if let Some(source) = parsed_box.source.as_ref().filter(|s| !s.trim().is_empty()) {
                attrs.insert("source".to_string(), source.trim().to_string());
            }

            ann.attributes = attrs;
            annotations.push(ann);
            next_ann_id += 1;
        }
    }

    Ok(Dataset {
        info: DatasetInfo::default(),
        licenses: vec![],
        images,
        categories,
        annotations,
    })
}

fn parse_image_element(
    node: Node<'_, '_>,
    path: &Path,
    meta: Option<&MetaLabels>,
) -> Result<ParsedImage, PanlabelError> {
    let name = required_attr(node, "name", path, "<image>")?.to_string();
    let width = parse_required_u32_attr(node, "width", path, "<image>")?;
    let height = parse_required_u32_attr(node, "height", path, "<image>")?;
    let cvat_id = node
        .attribute("id")
        .map(str::trim)
        .filter(|v| !v.is_empty())
        .map(|raw| {
            raw.parse::<u64>().map_err(|_| PanlabelError::CvatXmlParse {
                path: path.to_path_buf(),
                message: format!("invalid <image id> value '{raw}'; expected u64"),
            })
        })
        .transpose()?;

    let mut boxes = Vec::new();
    for child in node.children().filter(|n| n.is_element()) {
        let tag = child.tag_name().name();
        if tag != "box" {
            return Err(PanlabelError::CvatXmlParse {
                path: path.to_path_buf(),
                message: format!(
                    "image '{}' contains unsupported annotation type <{tag}>; only <box> is supported",
                    name
                ),
            });
        }

        boxes.push(parse_box_element(child, path, &name, meta)?);
    }

    Ok(ParsedImage {
        name,
        width,
        height,
        cvat_id,
        boxes,
    })
}

fn parse_box_element(
    node: Node<'_, '_>,
    path: &Path,
    image_name: &str,
    meta: Option<&MetaLabels>,
) -> Result<ParsedBox, PanlabelError> {
    let label = required_attr(node, "label", path, "<box>")?.to_string();

    if let Some(meta) = meta {
        if !meta.all.contains(&label) {
            return Err(PanlabelError::CvatXmlParse {
                path: path.to_path_buf(),
                message: format!(
                    "<box> in image '{}' references unknown label '{}' not in <meta><task><labels>",
                    image_name, label
                ),
            });
        }
    }

    let xtl = parse_required_f64_attr(node, "xtl", path, "<box>", image_name)?;
    let ytl = parse_required_f64_attr(node, "ytl", path, "<box>", image_name)?;
    let xbr = parse_required_f64_attr(node, "xbr", path, "<box>", image_name)?;
    let ybr = parse_required_f64_attr(node, "ybr", path, "<box>", image_name)?;

    let occluded = node
        .attribute("occluded")
        .map(str::trim)
        .filter(|v| !v.is_empty())
        .map(|raw| match raw {
            "0" => Ok(false),
            "1" => Ok(true),
            _ => Err(PanlabelError::CvatXmlParse {
                path: path.to_path_buf(),
                message: format!(
                    "<box> in image '{}' has invalid occluded='{raw}'; expected '0' or '1'",
                    image_name
                ),
            }),
        })
        .transpose()?
        .unwrap_or(false);

    let z_order = node
        .attribute("z_order")
        .map(str::trim)
        .filter(|v| !v.is_empty())
        .map(|raw| {
            raw.parse::<i32>().map_err(|_| PanlabelError::CvatXmlParse {
                path: path.to_path_buf(),
                message: format!(
                    "<box> in image '{}' has invalid z_order='{raw}'; expected i32",
                    image_name
                ),
            })
        })
        .transpose()?;

    let source = node
        .attribute("source")
        .map(str::trim)
        .filter(|v| !v.is_empty())
        .map(ToOwned::to_owned);

    let mut attributes = BTreeMap::new();
    for attr_node in node
        .children()
        .filter(|n| n.is_element() && n.tag_name().name() == "attribute")
    {
        let name = required_attr(attr_node, "name", path, "<attribute>")?.trim();
        if name.is_empty() {
            return Err(PanlabelError::CvatXmlParse {
                path: path.to_path_buf(),
                message: format!("<attribute> in image '{}' has empty name", image_name),
            });
        }

        let value = attr_node.text().map(str::trim).unwrap_or("").to_string();

        attributes.insert(format!("cvat_attr_{name}"), value);
    }

    Ok(ParsedBox {
        label,
        bbox: BBoxXYXY::<Pixel>::from_xyxy(xtl, ytl, xbr, ybr),
        occluded,
        z_order,
        source,
        attributes,
    })
}

fn extract_meta_labels(
    root: Node<'_, '_>,
    path: &Path,
) -> Result<Option<MetaLabels>, PanlabelError> {
    let Some(meta) = child_element(root, "meta") else {
        return Ok(None);
    };

    if child_element(meta, "project").is_some() {
        return Err(PanlabelError::CvatXmlParse {
            path: path.to_path_buf(),
            message: "project export not supported; export a task instead".to_string(),
        });
    }

    let Some(task) = child_element(meta, "task") else {
        return Ok(None);
    };
    let Some(labels) = child_element(task, "labels") else {
        return Ok(None);
    };

    let mut all = BTreeSet::new();
    let mut bbox_or_unknown = BTreeSet::new();

    for label_node in labels
        .children()
        .filter(|n| n.is_element() && n.tag_name().name() == "label")
    {
        let name = required_child_text(label_node, "name", path, "<label>")?;
        all.insert(name.clone());

        let typ = optional_child_text(label_node, "type").unwrap_or_else(|| "bbox".to_string());
        if typ.trim().eq_ignore_ascii_case("bbox") {
            bbox_or_unknown.insert(name);
        }
    }

    Ok(Some(MetaLabels {
        all,
        bbox_or_unknown,
    }))
}

fn build_cvat_xml(dataset: &Dataset, output_path: &Path) -> Result<String, PanlabelError> {
    let image_by_id: BTreeMap<ImageId, &Image> =
        dataset.images.iter().map(|img| (img.id, img)).collect();
    let category_by_id: BTreeMap<CategoryId, &Category> =
        dataset.categories.iter().map(|cat| (cat.id, cat)).collect();

    let mut annotations_by_image: BTreeMap<ImageId, Vec<&Annotation>> = BTreeMap::new();
    for ann in &dataset.annotations {
        if !image_by_id.contains_key(&ann.image_id) {
            return Err(PanlabelError::CvatWriteError {
                path: output_path.to_path_buf(),
                message: format!(
                    "annotation {} references missing image {}",
                    ann.id.as_u64(),
                    ann.image_id.as_u64()
                ),
            });
        }

        if !category_by_id.contains_key(&ann.category_id) {
            return Err(PanlabelError::CvatWriteError {
                path: output_path.to_path_buf(),
                message: format!(
                    "annotation {} references missing category {}",
                    ann.id.as_u64(),
                    ann.category_id.as_u64()
                ),
            });
        }

        annotations_by_image
            .entry(ann.image_id)
            .or_default()
            .push(ann);
    }

    for anns in annotations_by_image.values_mut() {
        anns.sort_by_key(|ann| ann.id);
    }

    let used_category_ids: HashSet<CategoryId> =
        dataset.annotations.iter().map(|a| a.category_id).collect();
    let mut categories: Vec<&Category> = dataset
        .categories
        .iter()
        .filter(|cat| used_category_ids.contains(&cat.id))
        .collect();
    categories.sort_by(|a, b| a.name.cmp(&b.name));

    let category_name_by_id: BTreeMap<CategoryId, String> = dataset
        .categories
        .iter()
        .map(|cat| (cat.id, cat.name.clone()))
        .collect();

    let mut images_sorted: Vec<&Image> = dataset.images.iter().collect();
    images_sorted.sort_by(|a, b| a.file_name.cmp(&b.file_name));

    let mut xml = String::new();
    writeln!(xml, "<?xml version=\"1.0\" encoding=\"utf-8\"?>").expect("write to string");
    writeln!(xml, "<annotations>").expect("write to string");
    writeln!(xml, "  <version>1.1</version>").expect("write to string");
    writeln!(xml, "  <meta>").expect("write to string");
    writeln!(xml, "    <task>").expect("write to string");
    writeln!(xml, "      <name>panlabel export</name>").expect("write to string");
    writeln!(xml, "      <size>{}</size>", images_sorted.len()).expect("write to string");
    writeln!(xml, "      <mode>annotation</mode>").expect("write to string");
    writeln!(xml, "      <labels>").expect("write to string");
    for cat in categories {
        writeln!(xml, "        <label>").expect("write to string");
        writeln!(xml, "          <name>{}</name>", xml_escape(&cat.name)).expect("write to string");
        writeln!(xml, "        </label>").expect("write to string");
    }
    writeln!(xml, "      </labels>").expect("write to string");
    writeln!(xml, "    </task>").expect("write to string");
    writeln!(xml, "  </meta>").expect("write to string");

    for (idx, image) in images_sorted.into_iter().enumerate() {
        let image_idx = idx as u64;
        writeln!(
            xml,
            "  <image id=\"{}\" name=\"{}\" width=\"{}\" height=\"{}\">",
            image_idx,
            xml_escape(&image.file_name),
            image.width,
            image.height
        )
        .expect("write to string");

        let anns = annotations_by_image.remove(&image.id).unwrap_or_default();
        for ann in anns {
            let label = category_name_by_id.get(&ann.category_id).ok_or_else(|| {
                PanlabelError::CvatWriteError {
                    path: output_path.to_path_buf(),
                    message: format!(
                        "internal error: missing category {} while writing",
                        ann.category_id.as_u64()
                    ),
                }
            })?;

            let occluded = ann
                .attributes
                .get("occluded")
                .and_then(|value| normalize_bool_attr(value))
                .unwrap_or("0");

            let z_order = ann
                .attributes
                .get("z_order")
                .and_then(|v| v.trim().parse::<i32>().ok())
                .unwrap_or(0);

            let source = ann
                .attributes
                .get("source")
                .map(|v| v.trim())
                .filter(|v| !v.is_empty())
                .unwrap_or("manual");

            writeln!(
                xml,
                "    <box label=\"{}\" occluded=\"{}\" xtl=\"{}\" ytl=\"{}\" xbr=\"{}\" ybr=\"{}\" z_order=\"{}\" source=\"{}\">",
                xml_escape(label),
                occluded,
                ann.bbox.xmin(),
                ann.bbox.ymin(),
                ann.bbox.xmax(),
                ann.bbox.ymax(),
                z_order,
                xml_escape(source),
            )
            .expect("write to string");

            for (key, value) in &ann.attributes {
                let Some(raw_name) = key.strip_prefix("cvat_attr_") else {
                    continue;
                };
                let raw_name = raw_name.trim();
                if raw_name.is_empty() {
                    continue;
                }

                writeln!(
                    xml,
                    "      <attribute name=\"{}\">{}</attribute>",
                    xml_escape(raw_name),
                    xml_escape(value)
                )
                .expect("write to string");
            }

            writeln!(xml, "    </box>").expect("write to string");
        }

        writeln!(xml, "  </image>").expect("write to string");
    }

    writeln!(xml, "</annotations>").expect("write to string");
    Ok(xml)
}

fn resolve_cvat_xml_path(path: &Path) -> Result<PathBuf, PanlabelError> {
    if path.is_file() {
        return Ok(path.to_path_buf());
    }

    if !path.is_dir() {
        return Err(PanlabelError::CvatLayoutInvalid {
            path: path.to_path_buf(),
            message: "input must be a file or directory".to_string(),
        });
    }

    let candidate = path.join(CVAT_XML_FILE_NAME);
    if candidate.is_file() {
        return Ok(candidate);
    }

    Err(PanlabelError::CvatLayoutInvalid {
        path: path.to_path_buf(),
        message: format!("expected '{CVAT_XML_FILE_NAME}' at directory root"),
    })
}

fn resolve_cvat_output_path(path: &Path) -> (PathBuf, PathBuf) {
    let is_xml_file = path
        .extension()
        .and_then(|ext| ext.to_str())
        .map(|ext| ext.eq_ignore_ascii_case("xml"))
        .unwrap_or(false);

    if is_xml_file {
        let parent = path.parent().unwrap_or(Path::new(".")).to_path_buf();
        return (parent, path.to_path_buf());
    }

    (path.to_path_buf(), path.join(CVAT_XML_FILE_NAME))
}

fn child_element<'a, 'input>(node: Node<'a, 'input>, tag: &str) -> Option<Node<'a, 'input>> {
    node.children()
        .find(|child| child.is_element() && child.tag_name().name() == tag)
}

fn optional_child_text(node: Node<'_, '_>, tag: &str) -> Option<String> {
    child_element(node, tag)
        .and_then(|child| child.text())
        .map(str::trim)
        .filter(|text| !text.is_empty())
        .map(ToOwned::to_owned)
}

fn required_child_text(
    node: Node<'_, '_>,
    tag: &str,
    path: &Path,
    context: &str,
) -> Result<String, PanlabelError> {
    optional_child_text(node, tag).ok_or_else(|| PanlabelError::CvatXmlParse {
        path: path.to_path_buf(),
        message: format!("missing <{tag}> in {context}"),
    })
}

fn required_attr<'a>(
    node: Node<'a, '_>,
    attr: &str,
    path: &Path,
    context: &str,
) -> Result<&'a str, PanlabelError> {
    node.attribute(attr)
        .ok_or_else(|| PanlabelError::CvatXmlParse {
            path: path.to_path_buf(),
            message: format!("missing '{attr}' attribute in {context}"),
        })
}

fn parse_required_u32_attr(
    node: Node<'_, '_>,
    attr: &str,
    path: &Path,
    context: &str,
) -> Result<u32, PanlabelError> {
    let raw = required_attr(node, attr, path, context)?;
    raw.trim()
        .parse::<u32>()
        .map_err(|_| PanlabelError::CvatXmlParse {
            path: path.to_path_buf(),
            message: format!("invalid '{attr}' value '{raw}' in {context}; expected u32"),
        })
}

fn parse_required_f64_attr(
    node: Node<'_, '_>,
    attr: &str,
    path: &Path,
    context: &str,
    image_name: &str,
) -> Result<f64, PanlabelError> {
    let raw = required_attr(node, attr, path, context)?;
    raw.trim()
        .parse::<f64>()
        .map_err(|_| PanlabelError::CvatXmlParse {
            path: path.to_path_buf(),
            message: format!(
                "<box> in image '{}' has invalid {attr}='{raw}'; expected floating-point number",
                image_name
            ),
        })
}

fn xml_escape(raw: &str) -> String {
    raw.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&apos;")
}

fn normalize_bool_attr(value: &str) -> Option<&'static str> {
    match value.trim().to_ascii_lowercase().as_str() {
        "true" | "yes" | "1" => Some("1"),
        "false" | "no" | "0" => Some("0"),
        _ => None,
    }
}

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

    #[test]
    fn parse_rejects_invalid_root() {
        let xml = r#"<?xml version="1.0" encoding="utf-8"?><annotation></annotation>"#;
        let err = from_cvat_xml_str(xml).unwrap_err();
        match err {
            PanlabelError::CvatXmlParse { message, .. } => {
                assert!(message.contains("<annotations>"))
            }
            other => panic!("expected CvatXmlParse, got {other:?}"),
        }
    }

    #[test]
    fn parse_rejects_polygon() {
        let xml = r#"<?xml version="1.0" encoding="utf-8"?>
<annotations>
  <image id="0" name="img.jpg" width="10" height="10">
    <polygon label="cat" points="1,1;2,2"/>
  </image>
</annotations>"#;
        let err = from_cvat_xml_str(xml).unwrap_err();
        match err {
            PanlabelError::CvatXmlParse { message, .. } => {
                assert!(message.contains("unsupported annotation type"))
            }
            other => panic!("expected CvatXmlParse, got {other:?}"),
        }
    }

    #[test]
    fn write_then_read_roundtrip_semantic() {
        let xml = r#"<?xml version="1.0" encoding="utf-8"?>
<annotations>
  <meta>
    <task>
      <labels>
        <label><name>cat</name><type>bbox</type></label>
        <label><name>dog</name><type>bbox</type></label>
        <label><name>unused</name><type>bbox</type></label>
      </labels>
    </task>
  </meta>
  <image id="5" name="b.jpg" width="20" height="10">
    <box label="cat" occluded="1" xtl="1.0" ytl="2.0" xbr="3.0" ybr="4.0" z_order="2" source="manual">
      <attribute name="truncated">no</attribute>
    </box>
  </image>
  <image id="2" name="a.jpg" width="20" height="10"></image>
</annotations>"#;

        let dataset = from_cvat_xml_str(xml).expect("parse");
        assert!(dataset.categories.iter().any(|c| c.name == "unused"));

        let out = to_cvat_xml_string(&dataset).expect("write");
        let restored = from_cvat_xml_str(&out).expect("parse restored");

        assert_eq!(restored.images.len(), 2);
        assert_eq!(restored.annotations.len(), 1);
        assert!(!restored.categories.iter().any(|c| c.name == "unused"));

        let ann = &restored.annotations[0];
        assert_eq!(ann.attributes.get("occluded"), Some(&"1".to_string()));
        assert_eq!(ann.attributes.get("z_order"), Some(&"2".to_string()));
        assert_eq!(ann.attributes.get("source"), Some(&"manual".to_string()));
        assert_eq!(
            ann.attributes.get("cvat_attr_truncated"),
            Some(&"no".to_string())
        );
    }
}