Skip to main content

oak_visualize/
lib.rs

1#![doc = include_str!("readme.md")]
2#![doc(html_logo_url = "https://raw.githubusercontent.com/ygg-lang/oaks/refs/heads/dev/documents/logo.svg")]
3#![doc(html_favicon_url = "https://raw.githubusercontent.com/ygg-lang/oaks/refs/heads/dev/documents/logo.svg")]
4#![feature(new_range_api)]
5#![allow(missing_docs)]
6//! Visualization tools for the Oak language framework.
7//!
8//! This crate provides tools for generating visual representations of
9//! syntax trees and other language structures, primarily in SVG format.
10
11use std::fmt;
12
13pub mod geometry;
14pub mod graph;
15pub mod layout;
16pub mod render;
17pub mod theme;
18pub mod tree;
19
20/// Error type for oak-visualize operations
21#[derive(Debug)]
22pub enum Error {
23    /// Layout computation error
24    LayoutError(String),
25    /// Rendering error
26    RenderError(String),
27    /// Serialization error
28    Serialization(String),
29    /// IO error
30    IoError(std::io::Error),
31    /// Generic error
32    Generic(String),
33}
34
35impl fmt::Display for Error {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Error::LayoutError(msg) => write!(f, "Layout error: {}", msg),
39            Error::RenderError(msg) => write!(f, "Render error: {}", msg),
40            Error::Serialization(msg) => write!(f, "Serialization error: {}", msg),
41            Error::IoError(err) => write!(f, "IO error: {}", err),
42            Error::Generic(msg) => write!(f, "Error: {}", msg),
43        }
44    }
45}
46
47impl std::error::Error for Error {}
48
49impl From<std::io::Error> for Error {
50    fn from(err: std::io::Error) -> Self {
51        Error::IoError(err)
52    }
53}
54
55/// Result type alias for oak-visualize operations
56pub type Result<T> = std::result::Result<T, Error>;
57
58/// Trait for types that can be visualized
59pub trait Visualize {
60    /// Visualize the object as an SVG string
61    fn visualize(&self) -> Result<String>;
62}
63
64/// Helper function to visualize a tree node as an SVG string
65pub fn to_svg<T: Visualize>(item: &T) -> Result<String> {
66    item.visualize()
67}
68
69// Re-export commonly used types
70pub use crate::{
71    geometry::{Point, Rect, Size, Transform},
72    graph::{Graph, GraphEdge, GraphLayout, GraphLayoutAlgorithm, GraphLayoutConfig, GraphNode},
73    layout::{EdgeType, Layout, LayoutConfig, LayoutEdge, LayoutEngine, LayoutNode, NodeType},
74    render::{ElementStyle, ExportFormat, LayoutExporter, RenderConfig, SvgRenderer},
75    theme::{ArrowConfig, EdgeTheme, HighlightTheme, NodeTheme, ShadowConfig, TextTheme, VisualizationTheme},
76    tree::{TreeLayout, TreeLayoutAlgorithm, TreeLayoutConfig, TreeNode},
77};