scirs2_graph/io/
mod.rs

1//! Input/output operations for graphs
2//!
3//! This module provides functions for reading and writing graph data
4//! in various formats.
5
6use std::path::Path;
7use std::str::FromStr;
8
9use crate::base::{DiGraph, EdgeWeight, Graph, Node};
10use crate::error::Result;
11
12pub mod adjacency_list;
13pub mod dot;
14pub mod edge_list;
15pub mod gml;
16pub mod graphml;
17pub mod json;
18pub mod matrix_market;
19
20use adjacency_list::{
21    read_adjacency_list_format, read_adjacency_list_format_digraph, write_adjacency_list_format,
22    write_adjacency_list_format_digraph,
23};
24use dot::{read_dot_format, read_dot_format_digraph, write_dot_format, write_dot_format_digraph};
25use edge_list::{
26    read_edge_list_format, read_edge_list_format_digraph, write_edge_list_format,
27    write_edge_list_format_digraph,
28};
29use gml::{read_gml_format, read_gml_format_digraph, write_gml_format, write_gml_format_digraph};
30use graphml::{
31    read_graphml_format, read_graphml_format_digraph, write_graphml_format,
32    write_graphml_format_digraph,
33};
34use json::{
35    read_json_format, read_json_format_digraph, write_json_format, write_json_format_digraph,
36};
37use matrix_market::{
38    read_matrix_market_format, read_matrix_market_format_digraph, write_matrix_market_format,
39    write_matrix_market_format_digraph,
40};
41
42/// Supported file formats for graph I/O
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub enum GraphFormat {
45    /// Edge list format (one edge per line: source target \[weight\])
46    EdgeList,
47    /// Adjacency list format (source: target1 target2 ...)
48    AdjacencyList,
49    /// Matrix Market format (sparse matrix format)
50    MatrixMarket,
51    /// GraphML format (XML-based format for graphs)
52    GraphML,
53    /// GML format (Graph Modeling Language)
54    Gml,
55    /// DOT format (Graphviz format)
56    Dot,
57    /// JSON graph format
58    Json,
59}
60
61/// Reads a graph from a file
62///
63/// # Arguments
64/// * `path` - Path to the file
65/// * `format` - Format of the file
66/// * `weighted` - Whether the graph has edge weights
67/// * `directed` - Whether the graph is directed
68///
69/// # Returns
70/// * `Ok(Graph)` - The graph read from the file
71/// * `Err(GraphError)` - If there was an error reading the file
72#[allow(dead_code)]
73pub fn read_graph<N, E, P>(path: P, format: GraphFormat, weighted: bool) -> Result<Graph<N, E>>
74where
75    N: Node + std::fmt::Debug + FromStr + Clone,
76    E: EdgeWeight + std::marker::Copy + std::fmt::Debug + std::default::Default + FromStr,
77    P: AsRef<Path>,
78{
79    match format {
80        GraphFormat::EdgeList => read_edge_list_format(path, weighted),
81        GraphFormat::AdjacencyList => read_adjacency_list_format(path, weighted),
82        GraphFormat::Dot => read_dot_format(path, weighted),
83        GraphFormat::Json => read_json_format(path, weighted),
84        GraphFormat::GraphML => read_graphml_format(path, weighted),
85        GraphFormat::Gml => read_gml_format(path, weighted),
86        GraphFormat::MatrixMarket => read_matrix_market_format(path, weighted),
87    }
88}
89
90/// Reads a directed graph from a file
91///
92/// # Arguments
93/// * `path` - Path to the file
94/// * `format` - Format of the file
95/// * `weighted` - Whether the graph has edge weights
96///
97/// # Returns
98/// * `Ok(DiGraph)` - The directed graph read from the file
99/// * `Err(GraphError)` - If there was an error reading the file
100#[allow(dead_code)]
101pub fn read_digraph<N, E, P>(path: P, format: GraphFormat, weighted: bool) -> Result<DiGraph<N, E>>
102where
103    N: Node + std::fmt::Debug + FromStr + Clone,
104    E: EdgeWeight + std::marker::Copy + std::fmt::Debug + std::default::Default + FromStr,
105    P: AsRef<Path>,
106{
107    match format {
108        GraphFormat::EdgeList => read_edge_list_format_digraph(path, weighted),
109        GraphFormat::AdjacencyList => read_adjacency_list_format_digraph(path, weighted),
110        GraphFormat::Dot => read_dot_format_digraph(path, weighted),
111        GraphFormat::Json => read_json_format_digraph(path, weighted),
112        GraphFormat::GraphML => read_graphml_format_digraph(path, weighted),
113        GraphFormat::Gml => read_gml_format_digraph(path, weighted),
114        GraphFormat::MatrixMarket => read_matrix_market_format_digraph(path, weighted),
115    }
116}
117
118/// Writes a graph to a file
119///
120/// # Arguments
121/// * `graph` - The graph to write
122/// * `path` - Path to the output file
123/// * `format` - Format to write the file in
124/// * `weighted` - Whether to include edge weights
125///
126/// # Returns
127/// * `Ok(())` - If the graph was written successfully
128/// * `Err(GraphError)` - If there was an error writing the file
129#[allow(dead_code)]
130pub fn write_graph<N, E, Ix, P>(
131    graph: &Graph<N, E, Ix>,
132    path: P,
133    format: GraphFormat,
134    weighted: bool,
135) -> Result<()>
136where
137    N: Node + std::fmt::Debug + std::fmt::Display + Clone,
138    E: EdgeWeight
139        + std::marker::Copy
140        + std::fmt::Debug
141        + std::default::Default
142        + std::fmt::Display
143        + Clone,
144    Ix: petgraph::graph::IndexType,
145    P: AsRef<Path>,
146{
147    match format {
148        GraphFormat::EdgeList => write_edge_list_format(graph, path, weighted),
149        GraphFormat::AdjacencyList => write_adjacency_list_format(graph, path, weighted),
150        GraphFormat::Dot => write_dot_format(graph, path, weighted),
151        GraphFormat::Json => write_json_format(graph, path, weighted),
152        GraphFormat::GraphML => write_graphml_format(graph, path, weighted),
153        GraphFormat::Gml => write_gml_format(graph, path, weighted),
154        GraphFormat::MatrixMarket => write_matrix_market_format(graph, path, weighted),
155    }
156}
157
158/// Writes a directed graph to a file
159///
160/// # Arguments
161/// * `graph` - The directed graph to write
162/// * `path` - Path to the output file
163/// * `format` - Format to write the file in
164/// * `weighted` - Whether to include edge weights
165///
166/// # Returns
167/// * `Ok(())` - If the graph was written successfully
168/// * `Err(GraphError)` - If there was an error writing the file
169#[allow(dead_code)]
170pub fn write_digraph<N, E, Ix, P>(
171    graph: &DiGraph<N, E, Ix>,
172    path: P,
173    format: GraphFormat,
174    weighted: bool,
175) -> Result<()>
176where
177    N: Node + std::fmt::Debug + std::fmt::Display + Clone,
178    E: EdgeWeight
179        + std::marker::Copy
180        + std::fmt::Debug
181        + std::default::Default
182        + std::fmt::Display
183        + Clone,
184    Ix: petgraph::graph::IndexType,
185    P: AsRef<Path>,
186{
187    match format {
188        GraphFormat::EdgeList => write_edge_list_format_digraph(graph, path, weighted),
189        GraphFormat::AdjacencyList => write_adjacency_list_format_digraph(graph, path, weighted),
190        GraphFormat::Dot => write_dot_format_digraph(graph, path, weighted),
191        GraphFormat::Json => write_json_format_digraph(graph, path, weighted),
192        GraphFormat::GraphML => write_graphml_format_digraph(graph, path, weighted),
193        GraphFormat::Gml => write_gml_format_digraph(graph, path, weighted),
194        GraphFormat::MatrixMarket => write_matrix_market_format_digraph(graph, path, weighted),
195    }
196}
197
198// All I/O formats are now implemented in their respective modules:
199// - edge_list: Simple edge list format
200// - adjacency_list: Adjacency list format
201// - matrix_market: Matrix Market sparse format
202// - dot: Graphviz DOT format
203// - json: JSON graph format
204// - graphml: GraphML XML format
205// - gml: Graph Modeling Language format