Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: error handling for dot parser #2

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
bimap = "0.6.2"
thiserror = "1.0.38"

[build-dependencies]
bindgen = "0.59.1"
9 changes: 9 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum DotGraphError {
#[error("`{0}` is not a valid dot graph")]
Invalid(String),
#[error("`{0}` is not a digraph")]
UnDiGraph(String),
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ pub mod graph;
pub mod node;
pub mod edge;
pub mod parser;
pub mod error;

pub use node::node::Node;
pub use edge::edge::Edge;
pub use graph::{
graph::Graph,
subgraph::SubGraph,
};
pub use error::DotGraphError;
20 changes: 14 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::DotGraphError;
use crate::graphviz::{
agfstnode, agfstout, agfstsubg, agget, agnameof, agnxtattr, agnxtnode, agnxtout, agnxtsubg,
agfstnode, agfstout, agfstsubg, agget, agisdirected, agnameof, agnxtattr, agnxtnode, agnxtout, agnxtsubg,
agread, fopen, Agedge_s, Agnode_s, Agraph_s, Agsym_s,
};
use crate::{
Expand All @@ -18,14 +19,21 @@ unsafe fn c_to_rust_string(ptr: *const i8) -> String {
String::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).to_string()
}

pub fn parse(path: &str) -> Graph {
let path = CString::new(path).unwrap();
let option = CString::new("r").unwrap();
pub fn parse(path: &str) -> Result<Graph, DotGraphError> {
let cpath = CString::new(path).unwrap();
let coption = CString::new("r").unwrap();
unsafe {
let fp = fopen(path.as_ptr(), option.as_ptr());
let fp = fopen(cpath.as_ptr(), coption.as_ptr());

let graph = agread(fp as _, 0 as _);
parse_graph(graph)
if graph.is_null() {
return Err(DotGraphError::Invalid(String::from(path)));
}
if agisdirected(graph) == 0 {
return Err(DotGraphError::UnDiGraph(String::from(path)));
}

Ok(parse_graph(graph))
}
}

Expand Down