Skip to main content

ezu_graph/
value.rs

1//! `PortValue` — the runtime values flowing along DAG edges.
2
3use std::sync::Arc;
4
5use crate::buf::{OpaqueValue, RasterBuf, ScalarField};
6use crate::port::PortKind;
7
8/// One value flowing along an edge. Cloning is cheap (Arc / Copy).
9#[derive(Debug, Clone)]
10pub enum PortValue {
11    Features(OpaqueValue),
12    Raster(Arc<RasterBuf>),
13    Sprite(Arc<RasterBuf>),
14    Brush(OpaqueValue),
15    Scalar(ScalarValue),
16    ScalarField(Arc<ScalarField>),
17}
18
19impl PortValue {
20    pub fn kind(&self) -> PortKind {
21        match self {
22            PortValue::Features(_) => PortKind::Features,
23            PortValue::Raster(_) => PortKind::Raster,
24            PortValue::Sprite(_) => PortKind::Sprite,
25            PortValue::Brush(_) => PortKind::Brush,
26            PortValue::Scalar(_) => PortKind::Scalar,
27            PortValue::ScalarField(_) => PortKind::ScalarField,
28        }
29    }
30
31    pub fn as_scalar_field(&self) -> Option<&Arc<ScalarField>> {
32        if let PortValue::ScalarField(f) = self {
33            Some(f)
34        } else {
35            None
36        }
37    }
38
39    pub fn as_raster(&self) -> Option<&Arc<RasterBuf>> {
40        if let PortValue::Raster(r) = self {
41            Some(r)
42        } else {
43            None
44        }
45    }
46
47    pub fn as_sprite(&self) -> Option<&Arc<RasterBuf>> {
48        if let PortValue::Sprite(s) = self {
49            Some(s)
50        } else {
51            None
52        }
53    }
54
55    pub fn as_scalar(&self) -> Option<&ScalarValue> {
56        if let PortValue::Scalar(s) = self {
57            Some(s)
58        } else {
59            None
60        }
61    }
62}
63
64/// A constant value carried on a `Scalar` port.
65#[derive(Debug, Clone, Copy, PartialEq)]
66pub enum ScalarValue {
67    /// Linear sRGB RGBA, components in `[0, 1]`.
68    Color([f32; 4]),
69    Number(f64),
70    Bool(bool),
71}
72
73impl ScalarValue {
74    pub fn as_color(&self) -> Option<[f32; 4]> {
75        if let ScalarValue::Color(c) = self {
76            Some(*c)
77        } else {
78            None
79        }
80    }
81
82    pub fn as_number(&self) -> Option<f64> {
83        if let ScalarValue::Number(n) = self {
84            Some(*n)
85        } else {
86            None
87        }
88    }
89}