rust-igraph 0.6.0

Pure-Rust, high-performance graph & network analysis library — 1200+ APIs, zero unsafe, igraph-compatible
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
770
771
772
773
774
775
776
//! LEDA native graph format reader and writer (ALGO-IO-007 / IO-012).
//!
//! The LEDA native graph format is:
//!
//! ```text
//! LEDA.GRAPH
//! string
//! void
//! -2
//! # Vertices
//! 3
//! |{Alice}|
//! |{Bob}|
//! |{Carol}|
//! # Edges
//! 2
//! 1 2 0 |{}|
//! 2 3 0 |{}|
//! ```
//!
//! The header lines specify vertex/edge attribute types (`void`, `string`,
//! `double`). The directedness flag is `-1` for directed, `-2` for
//! undirected. Each edge line has: source target reversal label.
//!
//! Counterpart of `igraph_write_graph_leda` / `igraph_read_graph_leda`.

use std::io::{BufRead, BufReader, Read, Write};

use crate::core::attributes::AttributeValue;
use crate::core::{Graph, IgraphError, IgraphResult};

/// Result of reading a LEDA file.
#[derive(Debug, Clone)]
pub struct LedaGraph {
    /// The parsed graph.
    pub graph: Graph,
    /// Vertex labels (if the vertex attribute type is `string`).
    pub labels: Option<Vec<String>>,
    /// Edge weights (if the edge attribute type is `double`).
    pub weights: Option<Vec<f64>>,
}

/// Read a graph from LEDA native graph format.
///
/// Parses the header (`LEDA.GRAPH`), attribute type declarations,
/// directedness flag, vertex section, and edge section. Vertex IDs in
/// the file are 1-based and converted to 0-based internally.
///
/// # Examples
///
/// ```
/// use rust_igraph::read_leda;
///
/// let input = b"LEDA.GRAPH\nvoid\nvoid\n-2\n# Vertices\n3\n|{}|\n|{}|\n|{}|\n# Edges\n2\n1 2 0 |{}|\n2 3 0 |{}|\n";
/// let result = read_leda(&input[..]).unwrap();
/// assert_eq!(result.graph.vcount(), 3);
/// assert_eq!(result.graph.ecount(), 2);
/// assert!(!result.graph.is_directed());
/// ```
pub fn read_leda<R: Read>(input: R) -> IgraphResult<LedaGraph> {
    let reader = BufReader::new(input);
    let mut content_lines: Vec<String> = Vec::new();
    for line_result in reader.lines() {
        let line = line_result?;
        let trimmed = line.trim().to_string();
        if !trimmed.is_empty() && !trimmed.starts_with('#') {
            content_lines.push(trimmed);
        }
    }

    let get = |i: usize, msg: &str| -> IgraphResult<&str> {
        content_lines
            .get(i)
            .map(String::as_str)
            .ok_or_else(|| leda_parse_err(i, msg))
    };

    if get(0, "empty LEDA file")? != "LEDA.GRAPH" {
        return Err(leda_parse_err(0, "LEDA file must start with 'LEDA.GRAPH'"));
    }
    let has_labels = get(1, "missing vertex attribute type")?.eq_ignore_ascii_case("string");
    let has_weights = get(2, "missing edge attribute type")?.eq_ignore_ascii_case("double");
    let directed = match get(3, "missing directedness flag")? {
        "-1" => true,
        "-2" => false,
        other => {
            return Err(leda_parse_err(
                3,
                &format!("invalid directedness flag '{other}', expected -1 or -2"),
            ));
        }
    };
    let n: u32 = get(4, "missing vertex count")?
        .parse()
        .map_err(|e| leda_parse_err(4, &format!("invalid vertex count: {e}")))?;

    let mut pos = 5;
    let mut labels: Vec<String> = Vec::with_capacity(n as usize);
    for _ in 0..n {
        let vline = get(pos, "unexpected end in vertex section")?;
        let label = extract_leda_label(vline)
            .ok_or_else(|| leda_parse_err(pos, &format!("invalid vertex entry: {vline}")))?;
        labels.push(label);
        pos += 1;
    }

    let parsed = parse_leda_edges(&content_lines, pos, n, has_weights)?;

    let mut graph = Graph::new(n, directed)?;
    graph.add_edges(parsed.edges)?;

    if has_labels {
        graph.set_vertex_attribute_all(
            "name",
            labels
                .iter()
                .map(|l| AttributeValue::String(l.clone()))
                .collect(),
        )?;
    }
    if has_weights {
        graph.set_edge_attribute_all(
            "weight",
            parsed
                .weights
                .iter()
                .map(|&w| AttributeValue::Numeric(w))
                .collect(),
        )?;
    }

    Ok(LedaGraph {
        graph,
        labels: if has_labels { Some(labels) } else { None },
        weights: if has_weights {
            Some(parsed.weights)
        } else {
            None
        },
    })
}

struct LedaEdges {
    edges: Vec<(u32, u32)>,
    weights: Vec<f64>,
}

fn parse_leda_edges(
    lines: &[String],
    start: usize,
    n: u32,
    has_weights: bool,
) -> IgraphResult<LedaEdges> {
    let m_str = lines
        .get(start)
        .ok_or_else(|| leda_parse_err(start, "missing edge count"))?;
    let m: usize = m_str
        .parse()
        .map_err(|e| leda_parse_err(start, &format!("invalid edge count: {e}")))?;

    let mut edges: Vec<(u32, u32)> = Vec::with_capacity(m);
    let mut weights: Vec<f64> = Vec::with_capacity(m);
    for i in 0..m {
        let pos = start + 1 + i;
        let eline = lines
            .get(pos)
            .ok_or_else(|| leda_parse_err(pos, "unexpected end in edge section"))?;
        let tokens: Vec<&str> = eline.splitn(4, ' ').collect();
        if tokens.len() < 3 {
            return Err(leda_parse_err(
                pos,
                &format!("edge line needs at least 3 fields: {eline}"),
            ));
        }
        let from: u32 = tokens[0]
            .parse()
            .map_err(|e| leda_parse_err(pos, &format!("invalid source id: {e}")))?;
        let to: u32 = tokens[1]
            .parse()
            .map_err(|e| leda_parse_err(pos, &format!("invalid target id: {e}")))?;
        if from == 0 || to == 0 || from > n || to > n {
            return Err(leda_parse_err(
                pos,
                &format!("vertex ID out of range: {from}->{to} (n={n})"),
            ));
        }
        edges.push((from - 1, to - 1));

        if has_weights {
            let label = tokens
                .get(3)
                .and_then(|s| extract_leda_label(s))
                .unwrap_or_default();
            let w: f64 = if label.is_empty() {
                0.0
            } else {
                label
                    .parse()
                    .map_err(|e| leda_parse_err(pos, &format!("invalid edge weight: {e}")))?
            };
            weights.push(w);
        }
    }
    Ok(LedaEdges { edges, weights })
}

fn leda_parse_err(line: usize, msg: &str) -> IgraphError {
    IgraphError::Parse {
        line,
        message: msg.to_string(),
    }
}

fn extract_leda_label(s: &str) -> Option<String> {
    let trimmed = s.trim();
    let inner = trimmed.strip_prefix("|{")?.strip_suffix("}|")?;
    Some(inner.to_string())
}

/// Write a graph in LEDA native graph format.
///
/// Vertex labels are written as string attributes if provided.
/// Edge weights are written as double attributes if provided.
/// The `reversal` field for edges is always 0 (no reverse edge pointer).
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, write_leda};
///
/// let mut g = Graph::with_vertices(3);
/// g.add_edge(0, 1).unwrap();
/// g.add_edge(1, 2).unwrap();
///
/// let labels = vec!["A".to_string(), "B".to_string(), "C".to_string()];
/// let mut buf = Vec::new();
/// write_leda(&g, Some(&labels), None, &mut buf).unwrap();
/// let s = String::from_utf8(buf).unwrap();
/// assert!(s.contains("LEDA.GRAPH"));
/// assert!(s.contains("|{A}|"));
/// ```
pub fn write_leda<W: Write>(
    graph: &Graph,
    vertex_labels: Option<&[String]>,
    edge_weights: Option<&[f64]>,
    writer: &mut W,
) -> IgraphResult<()> {
    if let Some(l) = vertex_labels {
        if l.len() != graph.vcount() as usize {
            return Err(IgraphError::InvalidArgument(format!(
                "vertex_labels length {} does not match vcount {}",
                l.len(),
                graph.vcount()
            )));
        }
        for (i, lbl) in l.iter().enumerate() {
            if lbl.contains('\n') {
                return Err(IgraphError::InvalidArgument(format!(
                    "vertex label at index {i} contains a newline character"
                )));
            }
        }
    }
    if let Some(w) = edge_weights {
        if w.len() != graph.ecount() {
            return Err(IgraphError::InvalidArgument(format!(
                "edge_weights length {} does not match ecount {}",
                w.len(),
                graph.ecount()
            )));
        }
    }

    let has_attr_labels =
        vertex_labels.is_none() && graph.vertex_attribute_names().contains(&"name");
    let has_attr_weights =
        edge_weights.is_none() && graph.edge_attribute_names().contains(&"weight");

    // Header
    writeln!(writer, "LEDA.GRAPH")?;

    // Vertex attribute type
    if vertex_labels.is_some() || has_attr_labels {
        writeln!(writer, "string")?;
    } else {
        writeln!(writer, "void")?;
    }

    // Edge attribute type
    if edge_weights.is_some() || has_attr_weights {
        writeln!(writer, "double")?;
    } else {
        writeln!(writer, "void")?;
    }

    // Directedness: -1 = directed, -2 = undirected
    if graph.is_directed() {
        writeln!(writer, "-1")?;
    } else {
        writeln!(writer, "-2")?;
    }

    // Vertices section
    writeln!(writer, "# Vertices")?;
    writeln!(writer, "{}", graph.vcount())?;

    for v in 0..graph.vcount() {
        match vertex_labels {
            Some(labels) => writeln!(writer, "|{{{}}}|", labels[v as usize])?,
            None => {
                if has_attr_labels {
                    let label = graph
                        .vertex_attribute("name", v)
                        .and_then(AttributeValue::as_str)
                        .unwrap_or("");
                    writeln!(writer, "|{{{label}}}|")?;
                } else {
                    writeln!(writer, "|{{}}|")?;
                }
            }
        }
    }

    // Edges section
    writeln!(writer, "# Edges")?;
    writeln!(writer, "{}", graph.ecount())?;

    for eid in 0..graph.ecount() {
        #[allow(clippy::cast_possible_truncation)]
        let (from, to) = graph.edge(eid as u32)?;

        if let Some(w) = edge_weights {
            writeln!(writer, "{} {} 0 |{{{}}}|", from + 1, to + 1, w[eid])?;
        } else {
            #[allow(clippy::cast_possible_truncation)]
            let eid_u32 = eid as u32;
            if let Some(w) = graph
                .edge_attribute("weight", eid_u32)
                .and_then(AttributeValue::as_f64)
            {
                writeln!(writer, "{} {} 0 |{{{w}}}|", from + 1, to + 1)?;
            } else {
                writeln!(writer, "{} {} 0 |{{}}|", from + 1, to + 1)?;
            }
        }
    }

    Ok(())
}

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

    #[test]
    fn test_basic_undirected() {
        let mut g = Graph::with_vertices(3);
        g.add_edge(0, 1).unwrap();
        g.add_edge(1, 2).unwrap();

        let mut buf = Vec::new();
        write_leda(&g, None, None, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();

        assert!(s.starts_with("LEDA.GRAPH\n"));
        assert!(s.contains("void\nvoid\n-2\n"));
        assert!(s.contains("# Vertices\n3\n"));
        assert!(s.contains("|{}|\n|{}|\n|{}|\n"));
        assert!(s.contains("# Edges\n2\n"));
        assert!(s.contains("1 2 0 |{}|\n"));
        assert!(s.contains("2 3 0 |{}|\n"));
    }

    #[test]
    fn test_directed() {
        let mut g = Graph::new(2, true).unwrap();
        g.add_edge(0, 1).unwrap();

        let mut buf = Vec::new();
        write_leda(&g, None, None, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();

        assert!(s.contains("-1\n"));
    }

    #[test]
    fn test_with_labels() {
        let mut g = Graph::with_vertices(3);
        g.add_edge(0, 1).unwrap();

        let labels = vec!["Alice".to_string(), "Bob".to_string(), "Carol".to_string()];
        let mut buf = Vec::new();
        write_leda(&g, Some(&labels), None, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();

        assert!(s.contains("string\nvoid\n"));
        assert!(s.contains("|{Alice}|\n"));
        assert!(s.contains("|{Bob}|\n"));
        assert!(s.contains("|{Carol}|\n"));
    }

    #[test]
    fn test_with_weights() {
        let mut g = Graph::with_vertices(2);
        g.add_edge(0, 1).unwrap();

        let weights = vec![3.5];
        let mut buf = Vec::new();
        write_leda(&g, None, Some(&weights), &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();

        assert!(s.contains("void\ndouble\n"));
        assert!(s.contains("1 2 0 |{3.5}|\n"));
    }

    #[test]
    fn test_with_labels_and_weights() {
        let mut g = Graph::with_vertices(2);
        g.add_edge(0, 1).unwrap();

        let labels = vec!["X".to_string(), "Y".to_string()];
        let weights = vec![1.25];
        let mut buf = Vec::new();
        write_leda(&g, Some(&labels), Some(&weights), &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();

        assert!(s.contains("string\ndouble\n"));
        assert!(s.contains("|{X}|\n"));
        assert!(s.contains("|{Y}|\n"));
        assert!(s.contains("1 2 0 |{1.25}|\n"));
    }

    #[test]
    fn test_empty_graph() {
        let g = Graph::with_vertices(0);

        let mut buf = Vec::new();
        write_leda(&g, None, None, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();

        assert!(s.contains("# Vertices\n0\n"));
        assert!(s.contains("# Edges\n0\n"));
    }

    #[test]
    fn test_no_edges() {
        let g = Graph::with_vertices(3);

        let mut buf = Vec::new();
        write_leda(&g, None, None, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();

        assert!(s.contains("# Vertices\n3\n"));
        assert!(s.contains("# Edges\n0\n"));
    }

    #[test]
    fn test_label_mismatch_error() {
        let g = Graph::with_vertices(3);
        let labels = vec!["A".to_string()];
        let mut buf = Vec::new();
        assert!(write_leda(&g, Some(&labels), None, &mut buf).is_err());
    }

    #[test]
    fn test_weight_mismatch_error() {
        let mut g = Graph::with_vertices(2);
        g.add_edge(0, 1).unwrap();
        let weights = vec![1.0, 2.0];
        let mut buf = Vec::new();
        assert!(write_leda(&g, None, Some(&weights), &mut buf).is_err());
    }

    #[test]
    fn test_newline_in_label_error() {
        let g = Graph::with_vertices(2);
        let labels = vec!["hello\nworld".to_string(), "ok".to_string()];
        let mut buf = Vec::new();
        assert!(write_leda(&g, Some(&labels), None, &mut buf).is_err());
    }

    #[test]
    fn test_self_loop() {
        let mut g = Graph::with_vertices(2);
        g.add_edge(0, 0).unwrap();

        let mut buf = Vec::new();
        write_leda(&g, None, None, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();

        assert!(s.contains("1 1 0 |{}|\n"));
    }

    #[test]
    fn test_one_based_vertex_ids() {
        let mut g = Graph::with_vertices(4);
        g.add_edge(2, 3).unwrap();

        let mut buf = Vec::new();
        write_leda(&g, None, None, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();

        assert!(s.contains("3 4 0 |{}|\n"));
    }

    // --- read_leda tests ---

    #[test]
    fn test_read_basic_undirected() {
        let input = b"LEDA.GRAPH\nvoid\nvoid\n-2\n# Vertices\n3\n|{}|\n|{}|\n|{}|\n# Edges\n2\n1 2 0 |{}|\n2 3 0 |{}|\n";
        let result = read_leda(&input[..]).unwrap();
        assert_eq!(result.graph.vcount(), 3);
        assert_eq!(result.graph.ecount(), 2);
        assert!(!result.graph.is_directed());
        assert!(result.labels.is_none());
        assert!(result.weights.is_none());
    }

    #[test]
    fn test_read_directed() {
        let input =
            b"LEDA.GRAPH\nvoid\nvoid\n-1\n# Vertices\n2\n|{}|\n|{}|\n# Edges\n1\n1 2 0 |{}|\n";
        let result = read_leda(&input[..]).unwrap();
        assert!(result.graph.is_directed());
        assert_eq!(result.graph.ecount(), 1);
    }

    #[test]
    fn test_read_with_labels() {
        let input = b"LEDA.GRAPH\nstring\nvoid\n-2\n# Vertices\n3\n|{Alice}|\n|{Bob}|\n|{Carol}|\n# Edges\n1\n1 2 0 |{}|\n";
        let result = read_leda(&input[..]).unwrap();
        let labels = result.labels.unwrap();
        assert_eq!(labels, vec!["Alice", "Bob", "Carol"]);
    }

    #[test]
    fn test_read_with_weights() {
        let input =
            b"LEDA.GRAPH\nvoid\ndouble\n-2\n# Vertices\n2\n|{}|\n|{}|\n# Edges\n1\n1 2 0 |{3.5}|\n";
        let result = read_leda(&input[..]).unwrap();
        let w = result.weights.unwrap();
        assert!((w[0] - 3.5).abs() < 1e-10);
    }

    #[test]
    fn test_read_with_labels_and_weights() {
        let input = b"LEDA.GRAPH\nstring\ndouble\n-2\n# Vertices\n2\n|{X}|\n|{Y}|\n# Edges\n1\n1 2 0 |{1.25}|\n";
        let result = read_leda(&input[..]).unwrap();
        let labels = result.labels.unwrap();
        assert_eq!(labels, vec!["X", "Y"]);
        let w = result.weights.unwrap();
        assert!((w[0] - 1.25).abs() < 1e-10);
    }

    #[test]
    fn test_read_empty_graph() {
        let input = b"LEDA.GRAPH\nvoid\nvoid\n-2\n# Vertices\n0\n# Edges\n0\n";
        let result = read_leda(&input[..]).unwrap();
        assert_eq!(result.graph.vcount(), 0);
        assert_eq!(result.graph.ecount(), 0);
    }

    #[test]
    fn test_read_no_edges() {
        let input = b"LEDA.GRAPH\nvoid\nvoid\n-2\n# Vertices\n3\n|{}|\n|{}|\n|{}|\n# Edges\n0\n";
        let result = read_leda(&input[..]).unwrap();
        assert_eq!(result.graph.vcount(), 3);
        assert_eq!(result.graph.ecount(), 0);
    }

    #[test]
    fn test_read_self_loop() {
        let input =
            b"LEDA.GRAPH\nvoid\nvoid\n-2\n# Vertices\n2\n|{}|\n|{}|\n# Edges\n1\n1 1 0 |{}|\n";
        let result = read_leda(&input[..]).unwrap();
        assert_eq!(result.graph.ecount(), 1);
        let (from, to) = result.graph.edge(0).unwrap();
        assert_eq!(from, 0);
        assert_eq!(to, 0);
    }

    #[test]
    fn test_read_bad_header() {
        let input = b"NOT_LEDA\nvoid\nvoid\n-2\n";
        assert!(read_leda(&input[..]).is_err());
    }

    #[test]
    fn test_read_bad_directedness() {
        let input = b"LEDA.GRAPH\nvoid\nvoid\n-3\n# Vertices\n0\n# Edges\n0\n";
        assert!(read_leda(&input[..]).is_err());
    }

    #[test]
    fn test_read_vertex_id_out_of_range() {
        let input =
            b"LEDA.GRAPH\nvoid\nvoid\n-2\n# Vertices\n2\n|{}|\n|{}|\n# Edges\n1\n1 5 0 |{}|\n";
        assert!(read_leda(&input[..]).is_err());
    }

    #[test]
    fn test_roundtrip_undirected() {
        let mut g = Graph::with_vertices(4);
        g.add_edge(0, 1).unwrap();
        g.add_edge(1, 2).unwrap();
        g.add_edge(2, 3).unwrap();

        let mut buf = Vec::new();
        write_leda(&g, None, None, &mut buf).unwrap();
        let result = read_leda(&buf[..]).unwrap();

        assert_eq!(result.graph.vcount(), g.vcount());
        assert_eq!(result.graph.ecount(), g.ecount());
        assert_eq!(result.graph.is_directed(), g.is_directed());
    }

    #[test]
    fn test_roundtrip_directed() {
        let mut g = Graph::new(3, true).unwrap();
        g.add_edge(0, 1).unwrap();
        g.add_edge(1, 2).unwrap();

        let mut buf = Vec::new();
        write_leda(&g, None, None, &mut buf).unwrap();
        let result = read_leda(&buf[..]).unwrap();

        assert_eq!(result.graph.vcount(), g.vcount());
        assert_eq!(result.graph.ecount(), g.ecount());
        assert!(result.graph.is_directed());
    }

    #[test]
    fn test_roundtrip_with_labels() {
        let mut g = Graph::with_vertices(3);
        g.add_edge(0, 1).unwrap();
        g.add_edge(1, 2).unwrap();

        let labels = vec!["A".to_string(), "B".to_string(), "C".to_string()];
        let mut buf = Vec::new();
        write_leda(&g, Some(&labels), None, &mut buf).unwrap();
        let result = read_leda(&buf[..]).unwrap();

        assert_eq!(result.labels.unwrap(), labels);
    }

    #[test]
    fn test_roundtrip_with_weights() {
        let mut g = Graph::with_vertices(2);
        g.add_edge(0, 1).unwrap();

        let weights = vec![2.75];
        let mut buf = Vec::new();
        write_leda(&g, None, Some(&weights), &mut buf).unwrap();
        let result = read_leda(&buf[..]).unwrap();

        let w = result.weights.unwrap();
        assert!((w[0] - 2.75).abs() < 1e-10);
    }

    #[test]
    fn test_read_empty_label() {
        let input = b"LEDA.GRAPH\nstring\nvoid\n-2\n# Vertices\n2\n|{}|\n|{hello}|\n# Edges\n0\n";
        let result = read_leda(&input[..]).unwrap();
        let labels = result.labels.unwrap();
        assert_eq!(labels[0], "");
        assert_eq!(labels[1], "hello");
    }

    // --- Attribute integration tests ---

    #[test]
    fn test_read_stores_label_attribute() {
        let input =
            b"LEDA.GRAPH\nstring\nvoid\n-2\n# Vertices\n2\n|{Alice}|\n|{Bob}|\n# Edges\n1\n1 2 0 |{}|\n";
        let result = read_leda(&input[..]).unwrap();
        assert_eq!(
            result
                .graph
                .vertex_attribute("name", 0)
                .and_then(AttributeValue::as_str),
            Some("Alice")
        );
        assert_eq!(
            result
                .graph
                .vertex_attribute("name", 1)
                .and_then(AttributeValue::as_str),
            Some("Bob")
        );
    }

    #[test]
    fn test_read_stores_weight_attribute() {
        let input =
            b"LEDA.GRAPH\nvoid\ndouble\n-2\n# Vertices\n2\n|{}|\n|{}|\n# Edges\n1\n1 2 0 |{4.5}|\n";
        let result = read_leda(&input[..]).unwrap();
        let w = result
            .graph
            .edge_attribute("weight", 0)
            .and_then(AttributeValue::as_f64)
            .unwrap();
        assert!((w - 4.5).abs() < 1e-10);
    }

    #[test]
    fn test_read_no_label_attribute_when_void() {
        let input = b"LEDA.GRAPH\nvoid\nvoid\n-2\n# Vertices\n2\n|{}|\n|{}|\n# Edges\n0\n";
        let result = read_leda(&input[..]).unwrap();
        assert!(result.graph.vertex_attribute("name", 0).is_none());
    }

    #[test]
    fn test_write_fallback_to_label_attribute() {
        let mut g = Graph::with_vertices(2);
        g.add_edge(0, 1).unwrap();
        g.set_vertex_attribute("name", 0, AttributeValue::String("X".into()))
            .unwrap();
        g.set_vertex_attribute("name", 1, AttributeValue::String("Y".into()))
            .unwrap();

        let mut buf = Vec::new();
        write_leda(&g, None, None, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert!(s.contains("string"));
        assert!(s.contains("|{X}|"));
        assert!(s.contains("|{Y}|"));
    }

    #[test]
    fn test_write_fallback_to_weight_attribute() {
        let mut g = Graph::with_vertices(2);
        g.add_edge(0, 1).unwrap();
        g.set_edge_attribute("weight", 0, AttributeValue::Numeric(7.5))
            .unwrap();

        let mut buf = Vec::new();
        write_leda(&g, None, None, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert!(s.contains("double"));
        assert!(s.contains("|{7.5}|"));
    }

    #[test]
    fn test_roundtrip_via_attributes() {
        let input = b"LEDA.GRAPH\nstring\ndouble\n-2\n# Vertices\n2\n|{Alice}|\n|{Bob}|\n# Edges\n1\n1 2 0 |{1.5}|\n";
        let result = read_leda(&input[..]).unwrap();

        let mut buf = Vec::new();
        write_leda(&result.graph, None, None, &mut buf).unwrap();

        let result2 = read_leda(&buf[..]).unwrap();
        assert_eq!(result2.graph.vcount(), 2);
        assert_eq!(result2.graph.ecount(), 1);
        assert!(result2.labels.is_some());
        assert!(result2.weights.is_some());
        let labels = result2.labels.unwrap();
        assert_eq!(labels, vec!["Alice", "Bob"]);
    }

    #[test]
    fn test_explicit_params_override_attributes() {
        let mut g = Graph::with_vertices(2);
        g.add_edge(0, 1).unwrap();
        g.set_vertex_attribute("name", 0, AttributeValue::String("attr_A".into()))
            .unwrap();
        g.set_vertex_attribute("name", 1, AttributeValue::String("attr_B".into()))
            .unwrap();

        let labels = vec!["explicit_A".to_string(), "explicit_B".to_string()];
        let mut buf = Vec::new();
        write_leda(&g, Some(&labels), None, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert!(s.contains("|{explicit_A}|"));
        assert!(!s.contains("attr_A"));
    }
}