xdot/xdot_parse/
shapes.rs

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
//! Drawable shapes included in [Shape].

/// A drawable shape including closed shapes, lines, and text.
#[derive(Debug, Clone, PartialEq)]
pub enum Shape {
    Ellipse(Ellipse),
    Points(Points),
    Text(Text),
}

/// A horizontal ellipse shape.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "pyo3",
    pyo3::pyclass(eq, get_all, set_all, module = "xdot_rs.shapes")
)]
pub struct Ellipse {
    pub filled: bool,
    pub x: f32,
    pub y: f32,
    pub w: f32,
    pub h: f32,
}
#[cfg(feature = "pyo3")]
#[pyo3::pymethods]
impl Ellipse {
    #[new]
    #[pyo3(signature = (x, y, w, h, filled=true))]
    fn new(x: f32, y: f32, w: f32, h: f32, filled: bool) -> Self {
        Ellipse { x, y, w, h, filled }
    }
}
impl From<Ellipse> for Shape {
    fn from(val: Ellipse) -> Self {
        Shape::Ellipse(val)
    }
}

/// Type of shape defined by a sequence of points.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
    feature = "pyo3",
    pyo3::pyclass(eq, eq_int, get_all, set_all, module = "xdot_rs.shapes")
)]
pub enum PointsType {
    Polygon,
    Polyline,
    BSpline,
}
/// Shape defined by a sequence of points (line or closed shape).
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "pyo3",
    pyo3::pyclass(eq, get_all, set_all, module = "xdot_rs.shapes")
)]
pub struct Points {
    pub filled: bool,
    pub r#type: PointsType,
    pub points: Vec<(f32, f32)>,
}
#[cfg(feature = "pyo3")]
#[pyo3::pymethods]
impl Points {
    #[new]
    #[pyo3(signature = (r#type, points, filled=true))]
    fn new(r#type: PointsType, points: Vec<(f32, f32)>, filled: bool) -> Self {
        Points {
            points,
            r#type,
            filled,
        }
    }
}
impl From<Points> for Shape {
    fn from(val: Points) -> Self {
        Shape::Points(val)
    }
}

/// Horizontal text alignment: left, center, or right.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
    feature = "pyo3",
    pyo3::pyclass(eq, eq_int, get_all, set_all, module = "xdot_rs.shapes")
)]
pub enum TextAlign {
    Left,
    Center,
    Right,
}
/// Multiline text for node or edge labels.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "pyo3",
    pyo3::pyclass(eq, get_all, set_all, module = "xdot_rs.shapes")
)]
pub struct Text {
    pub x: f32,
    pub y: f32,
    pub align: TextAlign,
    pub width: f32,
    pub text: String,
}
#[cfg(feature = "pyo3")]
#[pyo3::pymethods]
impl Text {
    #[new]
    fn new(x: f32, y: f32, align: TextAlign, width: f32, text: String) -> Self {
        Text {
            x,
            y,
            align,
            width,
            text,
        }
    }
}
impl From<Text> for Shape {
    fn from(val: Text) -> Self {
        Shape::Text(val)
    }
}

/// External image, currently unimplemented.
#[doc(hidden)]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "pyo3", pyo3::pyclass(module = "xdot_rs.shapes"))]
pub struct ExternalImage;

#[cfg(feature = "pyo3")]
#[pyo3::pymodule]
#[pyo3(name = "shapes")]
pub fn pymodule(m: &pyo3::Bound<'_, pyo3::types::PyModule>) -> pyo3::PyResult<()> {
    use pyo3::prelude::*;

    m.add_class::<Ellipse>()?;
    m.add_class::<PointsType>()?;
    m.add_class::<Points>()?;
    m.add_class::<TextAlign>()?;
    m.add_class::<Text>()?;
    m.add_class::<ExternalImage>()?;
    Ok(())
}