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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use clap::Parser;
use pisnge::common::parser::{parse_config_and_detect_type, ChartType};
use pisnge::pie_chart::{parse_pie_chart_content, render_pie_chart_svg};
use pisnge::png::svg_to_png;
use pisnge::work_item_movement::{
parse_work_item_movement, render_work_item_movement_svg, validate_work_item_movement,
};
use pisnge::xychart::{parse_xychart_content, render_xychart_svg};
use std::fs;
use std::path::Path;
#[derive(Parser)]
#[command(name = "pisnge")]
#[command(about = "A Rust-based diagram rendering library inspired by Mermaid.js")]
struct Cli {
#[arg(short, long)]
input: String,
#[arg(short, long)]
output: String,
#[arg(short, long, value_parser = ["png", "svg"])]
format: Option<String>,
#[arg(short, long)]
verbose: bool,
#[arg(short, long, default_value = "800")]
width: u32,
#[arg(short = 'H', long, default_value = "600")]
height: u32,
#[arg(long, default_value = "Liberation Sans")]
font: String,
}
fn detect_format_from_extension(output_path: &str) -> Option<String> {
Path::new(output_path)
.extension()
.and_then(|ext| ext.to_str())
.map(|ext| ext.to_lowercase())
.and_then(|ext| match ext.as_str() {
"png" => Some("png".to_string()),
"svg" => Some("svg".to_string()),
_ => None,
})
}
fn main() {
let cli = Cli::parse();
println!("Pisnge - Diagram Renderer");
// Determine output format: use -f flag if provided, otherwise detect from file extension
let output_format = match cli.format {
Some(format) => format,
None => match detect_format_from_extension(&cli.output) {
Some(format) => format,
None => {
eprintln!("Error: Could not detect output format from file extension '{}'. Please specify format using -f flag.",
Path::new(&cli.output).extension().and_then(|ext| ext.to_str()).unwrap_or("(none)"));
eprintln!("Supported formats: png, svg");
std::process::exit(1);
}
},
};
if cli.verbose {
println!("Input file: {}", cli.input);
println!("Output file: {}", cli.output);
println!("Output format: {}", output_format);
}
match fs::read_to_string(&cli.input) {
Ok(content) => {
// Ensure content ends with newline for easier parsing
let normalized_content = if content.ends_with('\n') {
content
} else {
format!("{}\n", content)
};
// First parse config and detect chart type
match parse_config_and_detect_type(&normalized_content) {
Ok((_, (config, chart_type, remaining_content))) => {
if cli.verbose {
println!("\nDetected chart type: {:?}", chart_type);
if let Some(ref config) = config {
println!("Theme: {}", config.theme);
if !config.theme_variables.is_empty() {
println!("Theme variables: {:?}", config.theme_variables);
}
}
}
match chart_type {
ChartType::Pie => {
match parse_pie_chart_content(remaining_content, config) {
Ok((_, pie_chart)) => {
if cli.verbose {
println!("\nParsed pie chart:");
println!(" Show data: {}", pie_chart.show_data);
if let Some(title) = &pie_chart.title {
println!(" Title: {}", title);
}
println!(" Data entries: {}", pie_chart.data.len());
for entry in &pie_chart.data {
println!(" \"{}\": {}", entry.label, entry.value);
}
}
match output_format.as_str() {
"svg" => {
let (svg_document, _, _) = render_pie_chart_svg(
&pie_chart, cli.width, cli.height, &cli.font,
);
match fs::write(&cli.output, svg_document.to_string()) {
Ok(_) => println!("SVG saved to: {}", cli.output),
Err(e) => {
eprintln!("Failed to write SVG file: {}", e);
std::process::exit(1);
}
}
}
"png" => {
let (svg_document, actual_width, actual_height) =
render_pie_chart_svg(
&pie_chart, cli.width, cli.height, &cli.font,
);
match svg_to_png(
&svg_document.to_string(),
actual_width,
actual_height,
&cli.font,
) {
Ok(png_data) => {
match fs::write(&cli.output, png_data) {
Ok(_) => {
println!("PNG saved to: {}", cli.output)
}
Err(e) => {
eprintln!(
"Failed to write PNG file: {}",
e
);
std::process::exit(1);
}
}
}
Err(e) => {
eprintln!(
"Failed to convert SVG to PNG: {}",
e
);
std::process::exit(1);
}
}
}
_ => {
eprintln!(
"Unsupported format: {}. Supported formats: png, svg",
output_format
);
std::process::exit(1);
}
}
}
Err(e) => {
eprintln!("Failed to parse pie chart: {:?}", e);
std::process::exit(1);
}
}
}
ChartType::XY => match parse_xychart_content(remaining_content, config) {
Ok((_, xychart)) => {
if cli.verbose {
println!("\nParsed XY chart:");
if let Some(title) = &xychart.title {
println!(" Title: {}", title);
}
println!(" X-axis labels: {:?}", xychart.x_axis.labels);
println!(
" Y-axis: \"{}\" {} -> {}",
xychart.y_axis.title,
xychart.y_axis.min,
xychart.y_axis.max
);
println!(" Series count: {}", xychart.series.len());
for (i, series) in xychart.series.iter().enumerate() {
println!(
" Series {}: {:?} {:?}",
i, series.series_type, series.data
);
}
}
match output_format.as_str() {
"svg" => {
let (svg_document, _, _) = render_xychart_svg(
&xychart, cli.width, cli.height, &cli.font,
);
match fs::write(&cli.output, svg_document.to_string()) {
Ok(_) => println!("SVG saved to: {}", cli.output),
Err(e) => {
eprintln!("Failed to write SVG file: {}", e);
std::process::exit(1);
}
}
}
"png" => {
let (svg_document, actual_width, actual_height) =
render_xychart_svg(
&xychart, cli.width, cli.height, &cli.font,
);
match svg_to_png(
&svg_document.to_string(),
actual_width,
actual_height,
&cli.font,
) {
Ok(png_data) => {
match fs::write(&cli.output, png_data) {
Ok(_) => {
println!("PNG saved to: {}", cli.output)
}
Err(e) => {
eprintln!(
"Failed to write PNG file: {}",
e
);
std::process::exit(1);
}
}
}
Err(e) => {
eprintln!("Failed to convert SVG to PNG: {}", e);
std::process::exit(1);
}
}
}
_ => {
eprintln!(
"Unsupported format: {}. Supported formats: png, svg",
output_format
);
std::process::exit(1);
}
}
}
Err(e) => {
eprintln!("Failed to parse XY chart: {:?}", e);
std::process::exit(1);
}
},
ChartType::WorkItemMovement => {
match parse_work_item_movement(remaining_content, config) {
Ok((_, work_item_movement)) => {
// Validate the work item movement chart
if let Err(e) = validate_work_item_movement(&work_item_movement)
{
eprintln!("Error: {}", e);
std::process::exit(1);
}
if cli.verbose {
println!("\nParsed work item movement chart:");
if let Some(title) = &work_item_movement.title {
println!(" Title: {}", title);
}
println!(" Columns: {:?}", work_item_movement.columns);
println!(
" Work items: {}",
work_item_movement.items.len()
);
for item in &work_item_movement.items {
println!(
" {}: {} ({}) -> {} ({})",
item.id,
item.from_state,
item.from_points,
item.to_state,
item.to_points
);
}
}
match output_format.as_str() {
"svg" => {
let (svg_document, _, _) =
render_work_item_movement_svg(
&work_item_movement,
cli.width,
&cli.font,
);
match fs::write(&cli.output, svg_document.to_string()) {
Ok(_) => println!("SVG saved to: {}", cli.output),
Err(e) => {
eprintln!("Failed to write SVG file: {}", e);
std::process::exit(1);
}
}
}
"png" => {
let (svg_document, actual_width, actual_height) =
render_work_item_movement_svg(
&work_item_movement,
cli.width,
&cli.font,
);
match svg_to_png(
&svg_document.to_string(),
actual_width,
actual_height,
&cli.font,
) {
Ok(png_data) => {
match fs::write(&cli.output, png_data) {
Ok(_) => {
println!("PNG saved to: {}", cli.output)
}
Err(e) => {
eprintln!(
"Failed to write PNG file: {}",
e
);
std::process::exit(1);
}
}
}
Err(e) => {
eprintln!(
"Failed to convert SVG to PNG: {}",
e
);
std::process::exit(1);
}
}
}
_ => {
eprintln!(
"Unsupported format: {}. Supported formats: png, svg",
output_format
);
std::process::exit(1);
}
}
}
Err(e) => {
eprintln!("Failed to parse work item movement chart: {:?}", e);
std::process::exit(1);
}
}
}
}
}
Err(e) => {
eprintln!(
"Failed to parse chart (unknown type or invalid config): {:?}",
e
);
std::process::exit(1);
}
}
}
Err(e) => {
eprintln!("Failed to read input file: {}", e);
std::process::exit(1);
}
}
}