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
//! The library allows to interact with [`graphviz`] format.
//!
//! # Description:
//! Essentially, it starts from 3 base methods:
//! - parse: a source of a dot file in the dot [`notation`]. The output format is a [Graph].
//! - print: [Graph] and [DotPrinter] provides an ability to transform a graph into string
//! following the [`notation`]
//! - exec: an ability to [`execute`] a cmd graphviz engine into different formats and etc.
//! - exec_dot: an ability to [`execute`] a cmd graphviz engine into different formats from a prepared string containing a dot graph.
//!
//! # Examples:
//! ```rust
//! use dot_structures::*;
//! use dot_generator::*;
//! use graphviz_rust::{exec,parse, exec_dot};
//! use graphviz_rust::cmd::{CommandArg, Format};
//! use graphviz_rust::printer::{PrinterContext,DotPrinter};
//! use graphviz_rust::attributes::*;
//!
//! fn parse_test() {
//! let g: Graph = parse(r#"
//! strict digraph t {
//! aa[color=green]
//! subgraph v {
//! aa[shape=square]
//! subgraph vv{a2 -> b2}
//! aaa[color=red]
//! aaa -> bbb
//! }
//! aa -> be -> subgraph v { d -> aaa}
//! aa -> aaa -> v
//! }
//! "#).unwrap();
//!
//! assert_eq!(
//! g,
//! graph!(strict di id!("t");
//! node!("aa";attr!("color","green")),
//! subgraph!("v";
//! node!("aa"; attr!("shape","square")),
//! subgraph!("vv"; edge!(node_id!("a2") => node_id!("b2"))),
//! node!("aaa";attr!("color","red")),
//! edge!(node_id!("aaa") => node_id!("bbb"))
//! ),
//! edge!(node_id!("aa") => node_id!("be") => subgraph!("v"; edge!(node_id!("d") => node_id!("aaa")))),
//! edge!(node_id!("aa") => node_id!("aaa") => node_id!("v"))
//! )
//! )
//! }
//!
//! fn print_test() {
//! let mut g = graph!(strict di id!("id"));
//! assert_eq!("strict digraph id {}".to_string(), g.print(&mut PrinterContext::default()));
//! }
//!
//! fn output_test(){
//! let mut g = graph!(id!("id");
//! node!("nod"),
//! subgraph!("sb";
//! edge!(node_id!("a") => subgraph!(;
//! node!("n";
//! NodeAttributes::color(color_name::black), NodeAttributes::shape(shape::egg))
//! ))
//! ),
//! edge!(node_id!("a1") => node_id!(esc "a2"))
//! );
//! let graph_svg = exec(g, &mut PrinterContext::default(), vec![
//! CommandArg::Format(Format::Svg),
//! ]).unwrap();
//!
//! }
//! fn output_exec_from_test(){
//! let mut g = graph!(id!("id");
//! node!("nod"),
//! subgraph!("sb";
//! edge!(node_id!("a") => subgraph!(;
//! node!("n";
//! NodeAttributes::color(color_name::black), NodeAttributes::shape(shape::egg))
//! ))
//! ),
//! edge!(node_id!("a1") => node_id!(esc "a2"))
//! );
//! let dot = g.print(&mut PrinterContext::default());
//! println!("{}",dot);
//! let format = Format::Svg;
//!
//! let graph_svg = exec_dot(dot.clone(), vec![
//! CommandArg::Format(format),
//! ]).unwrap();
//!
//! let graph_svg = exec_dot(dot, vec![
//! CommandArg::Format(format.clone()),
//! ]).unwrap();
//!
//! }
//! ```
//!
//! [`graphviz`]: https://graphviz.org/
//! [`notation`]: https://graphviz.org/doc/info/lang.html
//! [`execute`]: https://graphviz.org/doc/info/command.html
//!
#![allow(non_camel_case_types)]
#![allow(dead_code)]
pub extern crate dot_generator;
pub extern crate dot_structures;
pub extern crate into_attr;
pub extern crate into_attr_derive;
use std::io;
use dot_structures::*;
use crate::cmd::CommandArg;
use crate::printer::{DotPrinter, PrinterContext};
pub mod attributes;
pub mod printer;
pub mod cmd;
mod parser;
#[macro_use]
extern crate pest_derive;
extern crate pest;
/// Parses a given string into a graph format that can be used afterwards or returning
/// an string with an error description
pub fn parse(dot: &str) -> Result<Graph, String> {
parser::parse(dot)
}
/// Prints a given graph according to a given [`PrinterContext`]
pub fn print(graph: Graph, ctx: &mut PrinterContext) -> String {
graph.print(ctx)
}
/// Executes a given graph using a dot cmd client
pub fn exec(graph: Graph, ctx: &mut PrinterContext, args: Vec<CommandArg>) -> io::Result<String> {
cmd::exec(print(graph, ctx), args)
}
/// Executes a given string representation of the graph using a dot cmd client
pub fn exec_dot(dot_graph: String, args: Vec<CommandArg>) -> io::Result<String> {
cmd::exec(dot_graph, args)
}
#[cfg(test)]
mod tests {
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use dot_structures::*;
use dot_generator::*;
use crate::attributes::*;
use crate::{exec, exec_dot, parse};
use crate::cmd::{CommandArg, Format};
use crate::printer::{DotPrinter, PrinterContext};
#[test]
fn parse_test() {
let g: Graph = parse(r#"
strict digraph t {
aa[color=green]
subgraph v {
aa[shape=square]
subgraph vv{a2 -> b2}
aaa[color=red]
aaa -> bbb
}
aa -> be -> subgraph v { d -> aaa}
aa -> aaa -> v
}
"#).unwrap();
assert_eq!(
g,
graph!(strict di id!("t");
node!("aa";attr!("color","green")),
subgraph!("v";
node!("aa"; attr!("shape","square")),
subgraph!("vv"; edge!(node_id!("a2") => node_id!("b2"))),
node!("aaa";attr!("color","red")),
edge!(node_id!("aaa") => node_id!("bbb"))
),
edge!(node_id!("aa") => node_id!("be") => subgraph!("v"; edge!(node_id!("d") => node_id!("aaa")))),
edge!(node_id!("aa") => node_id!("aaa") => node_id!("v"))
)
)
}
#[test]
fn print_test() {
let mut g = graph!(id!("id"));
for el in (1..10000).into_iter() {
if el % 2 == 0 {
g.add_stmt(stmt!(node!(el)))
} else {
g.add_stmt(stmt!(subgraph!(el)))
}
}
assert_eq!(178896, g.print(&mut PrinterContext::default()).len())
}
#[cfg(windows)]
const LS: &'static str = "\r\n";
#[cfg(not(windows))]
const LS: &'static str = "\n";
#[test]
fn exec_test() {
let mut g = graph!(id!("id");
node!("nod"),
subgraph!("sb";
edge!(node_id!("a") => subgraph!(;
node!("n";
NodeAttributes::color(color_name::black), NodeAttributes::shape(shape::egg))
))
),
edge!(node_id!("a1") => node_id!(esc "a2"))
);
let graph_str = "graph id {\n nod\n subgraph sb {\n a -- subgraph {n[color=black,shape=egg]} \n }\n a1 -- \"a2\" \n}";
let mut ctx = PrinterContext::default();
assert_eq!(graph_str, g.print(&mut ctx));
let child = Command::new("dot")
.arg("-V")
.output()
.expect("dot command failed to start");
let output = String::from_utf8_lossy(&child.stderr);
let version =
output
.strip_prefix("dot - ")
.and_then(|v| v.strip_suffix(LS))
.expect("the version of client is unrecognizable ");
println!("{}", version);
let out_svg = exec(g.clone(), &mut ctx, vec![
CommandArg::Format(Format::Svg),
]).unwrap();
let p = "1.svg";
let out = exec(g.clone(), &mut PrinterContext::default(), vec![
CommandArg::Format(Format::Svg),
CommandArg::Output(p.to_string()),
]).unwrap();
let file = fs::read_to_string(p).unwrap();
fs::remove_file(p).unwrap();
assert_eq!("", out);
assert_eq!(out_svg, file);
}
#[test]
fn output_exec_from_test() {
let mut g = graph!(id!("id");
node!("nod"),
subgraph!("sb";
edge!(node_id!("a") => subgraph!(;
node!("n";
NodeAttributes::color(color_name::black), NodeAttributes::shape(shape::egg))
))
),
edge!(node_id!("a1") => node_id!(esc "a2"))
);
let dot = g.print(&mut PrinterContext::default());
let format = Format::Svg;
let res1 = exec_dot(dot.clone(), vec![
CommandArg::Format(format),
]).unwrap();
let res2 = exec_dot(dot.clone(), vec![
CommandArg::Format(format.clone()),
]).unwrap();
assert_eq!(res1, res2)
}
}