vizoxide 1.0.5

Vizoxide is a high-level Rust wrapper for Graphviz that provides an idiomatic, builder-pattern interface for creating, styling, laying out, and rendering complex graphs in various output formats.
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! Rendering capabilities for GraphViz.
//!
//! This module provides functions for rendering GraphViz graphs to various formats.

use std::ffi::CString;
use std::path::Path;
use std::io::Write;
use std::slice;
use std::str;

use base64::Engine;
use graphviz_sys as sys;
use crate::error::GraphvizError;
use crate::graph::Graph;
use crate::layout::Context;

/// A GraphViz output format.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
    /// Portable Network Graphics format.
    Png,
    /// Scalable Vector Graphics format.
    Svg,
    /// Portable Document Format.
    Pdf,
    /// PostScript format.
    Ps,
    /// Encapsulated PostScript format.
    Eps,
    /// Graphics Interchange Format.
    Gif,
    /// JPEG format.
    Jpeg,
    /// JSON format.
    Json,
    /// DOT format (GraphViz's native format).
    Dot,
    /// Extended DOT format.
    Xdot,
    /// Plain text format.
    Plain,
    /// Canonical DOT format.
    Canon,
    /// Fig format.
    Fig,
    /// VRML format.
    Vrml,
    /// CMAPx format (client-side image map).
    Cmapx,
    /// IMAP format (server-side image map).
    Imap,
    /// BMP format.
    Bmp,
    /// SVG with embedded XHTML format.
    Svgz,
}

impl Format {
    /// Converts the format to a C string representation.
    ///
    /// # Returns
    ///
    /// A Result containing the C string or an error
    pub(crate) fn as_cstr(&self) -> Result<CString, GraphvizError> {
        let name = match self {
            Format::Png => "png",
            Format::Svg => "svg",
            Format::Pdf => "pdf",
            Format::Ps => "ps",
            Format::Eps => "eps",
            Format::Gif => "gif",
            Format::Jpeg => "jpeg",
            Format::Json => "json",
            Format::Dot => "dot",
            Format::Xdot => "xdot",
            Format::Plain => "plain",
            Format::Canon => "canon",
            Format::Fig => "fig",
            Format::Vrml => "vrml",
            Format::Cmapx => "cmapx",
            Format::Imap => "imap",
            Format::Bmp => "bmp",
            Format::Svgz => "svgz",
        };
        
        CString::new(name).map_err(|_| GraphvizError::InvalidFormat)
    }
    
    /// Checks if the format is binary.
    ///
    /// # Returns
    ///
    /// true if the format is binary, false if it's text-based
    pub fn is_binary(&self) -> bool {
        match self {
            Format::Png | Format::Gif | Format::Jpeg | Format::Pdf |
            Format::Bmp | Format::Svgz => true,
            Format::Svg | Format::Dot | Format::Xdot | Format::Plain |
            Format::Canon | Format::Json | Format::Ps | Format::Eps |
            Format::Fig | Format::Vrml | Format::Cmapx | Format::Imap => false,
        }
    }
    
    /// Returns an iterator over all available output formats.
    ///
    /// # Returns
    ///
    /// An iterator that yields all available output formats
    pub fn all() -> impl Iterator<Item = Format> {
        [
            Format::Png,
            Format::Svg,
            Format::Pdf,
            Format::Ps,
            Format::Eps,
            Format::Gif,
            Format::Jpeg,
            Format::Json,
            Format::Dot,
            Format::Xdot,
            Format::Plain,
            Format::Canon,
            Format::Fig,
            Format::Vrml,
            Format::Cmapx,
            Format::Imap,
            Format::Bmp,
            Format::Svgz,
        ].iter().copied()
    }
    
    /// Gets the MIME type for the format.
    ///
    /// # Returns
    ///
    /// The MIME type as a string
    pub fn mime_type(&self) -> &'static str {
        match self {
            Format::Png => "image/png",
            Format::Svg => "image/svg+xml",
            Format::Pdf => "application/pdf",
            Format::Ps => "application/postscript",
            Format::Eps => "application/postscript",
            Format::Gif => "image/gif",
            Format::Jpeg => "image/jpeg",
            Format::Json => "application/json",
            Format::Dot => "text/vnd.graphviz",
            Format::Xdot => "text/vnd.graphviz",
            Format::Plain => "text/plain",
            Format::Canon => "text/vnd.graphviz",
            Format::Fig => "image/x-xfig",
            Format::Vrml => "model/vrml",
            Format::Cmapx => "text/html",
            Format::Imap => "application/x-httpd-imap",
            Format::Bmp => "image/bmp",
            Format::Svgz => "image/svg+xml",
        }
    }
    
    /// Gets the file extension for the format.
    ///
    /// # Returns
    ///
    /// The file extension as a string
    pub fn extension(&self) -> &'static str {
        match self {
            Format::Png => "png",
            Format::Svg => "svg",
            Format::Pdf => "pdf",
            Format::Ps => "ps",
            Format::Eps => "eps",
            Format::Gif => "gif",
            Format::Jpeg => "jpg",
            Format::Json => "json",
            Format::Dot => "dot",
            Format::Xdot => "xdot",
            Format::Plain => "txt",
            Format::Canon => "dot",
            Format::Fig => "fig",
            Format::Vrml => "wrl",
            Format::Cmapx => "map",
            Format::Imap => "map",
            Format::Bmp => "bmp",
            Format::Svgz => "svgz",
        }
    }
}

/// Renders a graph to a file with the specified format.
///
/// # Arguments
///
/// * `context` - The GraphViz context
/// * `graph` - The graph to render
/// * `format` - The output format
/// * `path` - The output file path
///
/// # Returns
///
/// A Result indicating success or failure
pub fn render_to_file<P: AsRef<Path>>(
    context: &Context,
    graph: &Graph,
    format: Format,
    path: P,
) -> Result<(), GraphvizError> {
    let format_cstr = format.as_cstr()?;
    let path_str = path.as_ref().to_string_lossy();
    let path_cstr = CString::new(path_str.as_bytes())?;
    
    let result = unsafe {
        sys::gvRenderFilename(
            context.inner,
            graph.inner,
            format_cstr.as_ptr(),
            path_cstr.as_ptr(),
        )
    };
    
    if result == 0 {
        Ok(())
    } else {
        Err(GraphvizError::RenderFailed)
    }
}

/// Renders a graph to a string with the specified format.
///
/// For binary formats, the result is base64-encoded.
///
/// # Arguments
///
/// * `context` - The GraphViz context
/// * `graph` - The graph to render
/// * `format` - The output format
///
/// # Returns
///
/// A Result containing the rendered string or an error
pub fn render_to_string(
    context: &Context,
    graph: &Graph,
    format: Format,
) -> Result<String, GraphvizError> {
    // Convert format to C string representation
    let format_cstr = format.as_cstr()?;
    
    // Prepare pointers to receive rendered data and length
    let mut buffer_ptr: *mut std::os::raw::c_char = std::ptr::null_mut();
    let mut length: std::os::raw::c_uint = 0;
    
    // Call GraphViz rendering function to generate in-memory representation
    let result = unsafe {
        sys::gvRenderData(
            context.inner,
            graph.inner,
            format_cstr.as_ptr(),
            &mut buffer_ptr,
            &mut length,
        )
    };
    
    // Validate rendering operation completed successfully
    if result != 0 {
        return Err(GraphvizError::RenderFailed);
    }
    
    // Ensure buffer was allocated properly
    if buffer_ptr.is_null() {
        return Err(GraphvizError::NullPointer("Render buffer is null"));
    }
    
    // Convert data to Rust string, handling different formats appropriately
    let rendered_string = if format.is_binary() {
        // For binary formats, encode as base64
        let data_slice = unsafe { 
            slice::from_raw_parts(buffer_ptr as *const u8, length as usize) 
        };
        base64::engine::general_purpose::STANDARD.encode(data_slice)
    } else {
        // For text formats, convert directly to UTF-8 string
        let data_slice = unsafe { 
            slice::from_raw_parts(buffer_ptr as *const u8, length as usize) 
        };
        match str::from_utf8(data_slice) {
            Ok(s) => s.to_owned(),
            Err(_) => {
                // Clean up memory before returning error
                unsafe { sys::gvFreeRenderData(buffer_ptr) };
                return Err(GraphvizError::InvalidUtf8);
            }
        }
    };
    
    // Release memory allocated by GraphViz
    unsafe { sys::gvFreeRenderData(buffer_ptr) };
    
    Ok(rendered_string)
}

/// Renders a graph to a byte vector with the specified format.
///
/// # Arguments
///
/// * `context` - The GraphViz context
/// * `graph` - The graph to render
/// * `format` - The output format
///
/// # Returns
///
/// A Result containing the rendered bytes or an error
pub fn render_to_bytes(
    context: &Context,
    graph: &Graph,
    format: Format,
) -> Result<Vec<u8>, GraphvizError> {
    // Convert format to C string representation
    let format_cstr = format.as_cstr()?;
    
    // Prepare pointers to receive rendered data and length
    let mut buffer_ptr: *mut std::os::raw::c_char = std::ptr::null_mut();
    let mut length: std::os::raw::c_uint = 0;
    
    // Call GraphViz rendering function to generate in-memory representation
    let result = unsafe {
        sys::gvRenderData(
            context.inner,
            graph.inner,
            format_cstr.as_ptr(),
            &mut buffer_ptr,
            &mut length,
        )
    };
    
    // Validate rendering operation completed successfully
    if result != 0 {
        return Err(GraphvizError::RenderFailed);
    }
    
    // Ensure buffer was allocated properly
    if buffer_ptr.is_null() {
        return Err(GraphvizError::NullPointer("Render buffer is null"));
    }
    
    // Copy data into a Vec<u8>
    let data_slice = unsafe { 
        slice::from_raw_parts(buffer_ptr as *const u8, length as usize) 
    };
    let bytes = data_slice.to_vec();
    
    // Release memory allocated by GraphViz
    unsafe { sys::gvFreeRenderData(buffer_ptr) };
    
    Ok(bytes)
}

/// Renders a graph to a writer with the specified format.
///
/// # Arguments
///
/// * `context` - The GraphViz context
/// * `graph` - The graph to render
/// * `format` - The output format
/// * `writer` - The writer to write to
///
/// # Returns
///
/// A Result indicating success or failure
pub fn render_to_writer<W: Write>(
    context: &Context,
    graph: &Graph,
    format: Format,
    mut writer: W,
) -> Result<(), GraphvizError> {
    let bytes = render_to_bytes(context, graph, format)?;
    writer.write_all(&bytes)?;
    Ok(())
}

/// Options for rendering graphs.
pub struct RenderOptions {
    /// Whether to render with anti-aliasing.
    pub anti_alias: bool,
    /// Whether to render with transparency.
    pub transparent: bool,
    /// Resolution in DPI.
    pub dpi: Option<f64>,
    /// Background color.
    pub background: Option<String>,
    /// Whether to include the graph's bounding box.
    pub show_bb: bool,
    /// Scale factor for rendering.
    pub scale: Option<f64>,
    /// Fit to specific dimensions.
    pub size: Option<(f64, f64)>,
    /// Output quality (0-100) for formats like JPEG.
    pub quality: Option<u32>,
}

impl Default for RenderOptions {
    fn default() -> Self {
        RenderOptions {
            anti_alias: true,
            transparent: false,
            dpi: None,
            background: None,
            show_bb: false,
            scale: None,
            size: None,
            quality: None,
        }
    }
}

impl RenderOptions {
    /// Creates a new RenderOptions with default values.
    ///
    /// # Returns
    ///
    /// A new RenderOptions instance
    pub fn new() -> Self {
        Default::default()
    }
    
    /// Applies the options to a graph.
    ///
    /// # Arguments
    ///
    /// * `graph` - The graph to apply options to
    ///
    /// # Returns
    ///
    /// A Result indicating success or failure
    pub fn apply(&self, graph: &Graph) -> Result<(), GraphvizError> {
        if !self.anti_alias {
            graph.set_attribute("smoothing", "0")?;
        }
        
        if self.transparent {
            graph.set_attribute("bgcolor", "transparent")?;
        }
        
        if let Some(dpi) = self.dpi {
            graph.set_attribute("dpi", &dpi.to_string())?;
        }
        
        if let Some(ref background) = self.background {
            graph.set_attribute("bgcolor", background)?;
        }
        
        if self.show_bb {
            graph.set_attribute("bb", "true")?;
        }
        
        if let Some(scale) = self.scale {
            graph.set_attribute("scale", &scale.to_string())?;
        }
        
        if let Some((width, height)) = self.size {
            graph.set_attribute("size", &format!("{},{}!", width, height))?;
        }
        
        if let Some(quality) = self.quality {
            graph.set_attribute("quality", &quality.to_string())?;
        }
        
        Ok(())
    }
    
    /// Sets whether to render with anti-aliasing.
    ///
    /// # Arguments
    ///
    /// * `anti_alias` - Whether to use anti-aliasing
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_anti_alias(mut self, anti_alias: bool) -> Self {
        self.anti_alias = anti_alias;
        self
    }
    
    /// Sets whether to render with transparency.
    ///
    /// # Arguments
    ///
    /// * `transparent` - Whether to use transparency
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_transparency(mut self, transparent: bool) -> Self {
        self.transparent = transparent;
        self
    }
    
    /// Sets the resolution in DPI.
    ///
    /// # Arguments
    ///
    /// * `dpi` - The resolution value
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_dpi(mut self, dpi: f64) -> Self {
        self.dpi = Some(dpi);
        self
    }
    
    /// Sets the background color.
    ///
    /// # Arguments
    ///
    /// * `color` - The background color
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_background(mut self, color: &str) -> Self {
        self.background = Some(color.to_owned());
        self
    }
    
    /// Sets whether to show the bounding box.
    ///
    /// # Arguments
    ///
    /// * `show_bb` - Whether to show the bounding box
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_show_bb(mut self, show_bb: bool) -> Self {
        self.show_bb = show_bb;
        self
    }
    
    /// Sets the scale factor.
    ///
    /// # Arguments
    ///
    /// * `scale` - The scale factor
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_scale(mut self, scale: f64) -> Self {
        self.scale = Some(scale);
        self
    }
    
    /// Sets the output size.
    ///
    /// # Arguments
    ///
    /// * `width` - The width in inches
    /// * `height` - The height in inches
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_size(mut self, width: f64, height: f64) -> Self {
        self.size = Some((width, height));
        self
    }
    
    /// Sets the output quality.
    ///
    /// # Arguments
    ///
    /// * `quality` - The quality (0-100)
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_quality(mut self, quality: u32) -> Self {
        self.quality = Some(quality);
        self
    }
}