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#![warn(missing_docs)]
6
7use std::fmt;
8
9/// Geometry primitives for visualization.
10pub mod geometry;
11/// Graph-based visualization structures.
12pub mod graph;
13/// Layout algorithms for nodes and edges.
14pub mod layout;
15/// Rendering logic for generating visual output.
16pub mod render;
17/// Theme configuration for visualization styles.
18pub mod theme;
19/// Tree-specific visualization tools.
20pub mod tree;
21
22/// Error type for oak-visualize operations.
23#[derive(Debug)]
24pub enum Error {
25    /// Layout computation error.
26    LayoutError(String),
27    /// Rendering error.
28    RenderError(String),
29    /// Serialization error.
30    Serialization(String),
31    /// IO error.
32    IoError(std::io::Error),
33    /// Generic error.
34    Generic(String),
35}
36
37impl Error {
38    /// Creates a generic error with a message.
39    pub fn msg<M: Into<String>>(message: M) -> Self {
40        Error::Generic(message.into())
41    }
42}
43
44impl fmt::Display for Error {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            Error::LayoutError(msg) => write!(f, "Layout error: {}", msg),
48            Error::RenderError(msg) => write!(f, "Render error: {}", msg),
49            Error::Serialization(msg) => write!(f, "Serialization error: {}", msg),
50            Error::IoError(err) => write!(f, "IO error: {}", err),
51            Error::Generic(msg) => write!(f, "Error: {}", msg),
52        }
53    }
54}
55
56impl std::error::Error for Error {}
57
58impl From<std::io::Error> for Error {
59    fn from(err: std::io::Error) -> Self {
60        Error::IoError(err)
61    }
62}
63
64/// Result type alias for oak-visualize operations
65pub type Result<T> = std::result::Result<T, Error>;
66
67/// Trait for types that can be visualized
68pub trait Visualize {
69    /// Visualize the object as an SVG string
70    fn visualize(&self) -> Result<String>;
71}
72
73/// Helper function to visualize a tree node as an SVG string
74pub fn to_svg<T: Visualize>(item: &T) -> Result<String> {
75    item.visualize()
76}
77
78// Re-export commonly used types
79pub use crate::{
80    geometry::{Point, Rect, Size, Transform},
81    graph::{Graph, GraphEdge, GraphLayout, GraphLayoutAlgorithm, GraphLayoutConfig, GraphNode},
82    layout::{EdgeType, Layout, LayoutConfig, LayoutEdge, LayoutEngine, LayoutNode, NodeType},
83    render::{ElementStyle, ExportFormat, LayoutExporter, RenderConfig, SvgRenderer},
84    theme::{ArrowConfig, EdgeTheme, HighlightTheme, NodeTheme, ShadowConfig, TextTheme, VisualizationTheme},
85    tree::{TreeLayout, TreeLayoutAlgorithm, TreeLayoutConfig, TreeNode},
86};