1use 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub enum GraphFormat {
45 EdgeList,
47 AdjacencyList,
49 MatrixMarket,
51 GraphML,
53 Gml,
55 Dot,
57 Json,
59}
60
61#[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#[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#[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#[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