pub use crate::error::IOError;
use crate::graph::{Graph, GraphStorage};
pub mod graphml;
pub mod graphson;
pub use graphml::{GraphMLExport, GraphMLImport};
pub use graphson::{GraphSONExport, GraphSONImport, ImportNote, ImportNoteKind, ImportReport};
pub trait GraphImport {
fn import_graphml(&mut self, path: &str) -> Result<(), IOError>;
fn import_graphml_reader<R: std::io::BufRead>(&mut self, reader: R) -> Result<(), IOError>;
fn import_graphson(&mut self, path: &str) -> Result<ImportReport, IOError>;
fn import_graphson_reader<R: std::io::BufRead>(
&mut self,
reader: R,
) -> Result<ImportReport, IOError>;
}
pub trait GraphExport {
fn export_graphml(&self, path: &str) -> Result<(), IOError>;
fn export_graphml_writer<W: std::io::Write>(&self, writer: W) -> Result<(), IOError>;
fn export_graphml_writer_with_options<W: std::io::Write>(
&self,
writer: W,
beautify: bool,
) -> Result<(), IOError> {
let _ = beautify;
self.export_graphml_writer(writer)
}
fn export_graphson(&self, path: &str) -> Result<(), IOError>;
fn export_graphson_writer<W: std::io::Write>(&self, writer: W) -> Result<(), IOError>;
}
pub(crate) fn import_graphml_file_with<S: GraphStorage>(
g: &mut S,
path: &str,
presets: Option<&std::collections::HashMap<compact_str::CompactString, crate::ElementProperty>>,
) -> Result<(), IOError> {
let f = std::fs::File::open(path).map_err(|err| IOError::FileRead {
path: path.to_string(),
source: err,
})?;
let reader = quick_xml::Reader::from_reader(std::io::BufReader::with_capacity(128 * 1024, f));
GraphMLImport {}.import_reader_with(g, reader, presets)
}
pub(crate) fn import_graphson_file_with<S: GraphStorage>(
g: &mut S,
path: &str,
presets: Option<&std::collections::HashMap<compact_str::CompactString, crate::ElementProperty>>,
) -> Result<ImportReport, IOError> {
let f = std::fs::File::open(path).map_err(|err| IOError::FileRead {
path: path.to_string(),
source: err,
})?;
GraphSONImport::new().import_reader_with(
g,
std::io::BufReader::with_capacity(128 * 1024, f),
presets,
)
}
impl<S: GraphStorage> GraphImport for S {
fn import_graphml(&mut self, path: &str) -> Result<(), IOError> {
import_graphml_file_with(self, path, None)
}
fn import_graphml_reader<R: std::io::BufRead>(&mut self, reader: R) -> Result<(), IOError> {
let importer = GraphMLImport {};
let xml_reader = quick_xml::Reader::from_reader(reader);
importer.import_reader(self, xml_reader)
}
fn import_graphson(&mut self, path: &str) -> Result<ImportReport, IOError> {
import_graphson_file_with(self, path, None)
}
fn import_graphson_reader<R: std::io::BufRead>(
&mut self,
reader: R,
) -> Result<ImportReport, IOError> {
GraphSONImport::new().import_reader(self, reader)
}
}
impl GraphImport for Graph {
fn import_graphml(&mut self, path: &str) -> Result<(), IOError> {
self.write().import_graphml(path)
}
fn import_graphml_reader<R: std::io::BufRead>(&mut self, reader: R) -> Result<(), IOError> {
self.write().import_graphml_reader(reader)
}
fn import_graphson(&mut self, path: &str) -> Result<ImportReport, IOError> {
self.write().import_graphson(path)
}
fn import_graphson_reader<R: std::io::BufRead>(
&mut self,
reader: R,
) -> Result<ImportReport, IOError> {
self.write().import_graphson_reader(reader)
}
}
impl<S: GraphStorage> GraphExport for S {
fn export_graphml(&self, path: &str) -> Result<(), IOError> {
let exporter = GraphMLExport::new();
let f = std::fs::File::create(path).map_err(|err| IOError::FileRead {
path: path.to_string(),
source: err,
})?;
let writer = std::io::BufWriter::with_capacity(128 * 1024, f);
exporter.export_writer(self, writer)
}
fn export_graphml_writer<W: std::io::Write>(&self, writer: W) -> Result<(), IOError> {
let exporter = GraphMLExport::new();
exporter.export_writer(self, writer)
}
fn export_graphml_writer_with_options<W: std::io::Write>(
&self,
writer: W,
beautify: bool,
) -> Result<(), IOError> {
let exporter = GraphMLExport::new().with_beautify(beautify);
exporter.export_writer(self, writer)
}
fn export_graphson(&self, path: &str) -> Result<(), IOError> {
GraphSONExport::new().export_file(self, path)
}
fn export_graphson_writer<W: std::io::Write>(&self, writer: W) -> Result<(), IOError> {
GraphSONExport::new().export_writer(self, writer)
}
}
impl GraphExport for Graph {
fn export_graphml(&self, path: &str) -> Result<(), IOError> {
self.read().export_graphml(path)
}
fn export_graphml_writer<W: std::io::Write>(&self, writer: W) -> Result<(), IOError> {
self.read().export_graphml_writer(writer)
}
fn export_graphml_writer_with_options<W: std::io::Write>(
&self,
writer: W,
beautify: bool,
) -> Result<(), IOError> {
self.read()
.export_graphml_writer_with_options(writer, beautify)
}
fn export_graphson(&self, path: &str) -> Result<(), IOError> {
self.read().export_graphson(path)
}
fn export_graphson_writer<W: std::io::Write>(&self, writer: W) -> Result<(), IOError> {
self.read().export_graphson_writer(writer)
}
}