1use std::collections::HashMap;
5use std::sync::Arc;
6
7use crate::buf::{OpaqueValue, RasterBuf, ScalarField};
8use crate::value::ScalarValue;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub struct TileId {
13 pub z: u8,
14 pub x: u32,
15 pub y: u32,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub struct CanvasInfo {
23 pub tile_size: u32,
24 pub pad: u32,
25}
26
27impl CanvasInfo {
28 pub fn padded_size(&self) -> u32 {
29 self.tile_size + 2 * self.pad
30 }
31}
32
33#[derive(Debug, Clone)]
41pub enum Asset {
42 Image(Arc<RasterBuf>),
43 Brush(OpaqueValue),
44 Features(OpaqueValue),
45 ScalarField(Arc<ScalarField>),
46}
47
48#[derive(Debug, thiserror::Error)]
49pub enum AssetError {
50 #[error("asset not found: `{0}`")]
51 NotFound(String),
52 #[error("asset decode failed for `{src}`: {msg}")]
53 Decode { src: String, msg: String },
54 #[error("asset error: {0}")]
55 Other(String),
56}
57
58pub trait AssetLoader: Send + Sync {
67 fn load(&self, name: &str) -> Result<Asset, AssetError>;
68
69 fn hash(&self, _name: &str) -> u128 {
73 0
74 }
75}
76
77pub struct NoAssets;
80impl AssetLoader for NoAssets {
81 fn load(&self, name: &str) -> Result<Asset, AssetError> {
82 Err(AssetError::NotFound(name.to_string()))
83 }
84}
85
86#[derive(Debug, Default, Clone)]
89pub struct ParamValues {
90 pub values: HashMap<String, ScalarValue>,
91}
92
93impl ParamValues {
94 pub fn new() -> Self {
95 Self::default()
96 }
97
98 pub fn set(&mut self, name: impl Into<String>, value: ScalarValue) {
99 self.values.insert(name.into(), value);
100 }
101
102 pub fn get(&self, name: &str) -> Option<ScalarValue> {
103 self.values.get(name).copied()
104 }
105}
106
107pub struct EvalCtx<'a> {
109 pub tile: TileId,
110 pub canvas: CanvasInfo,
111 pub assets: &'a dyn AssetLoader,
112 pub params: &'a ParamValues,
113 pub rng_seed: u64,
116}
117
118#[derive(Debug, thiserror::Error)]
119pub enum EvalError {
120 #[error("input port `{0}` was not supplied")]
121 MissingInput(String),
122 #[error("input `{port}` has wrong kind: expected {expected:?}, got {got:?}")]
123 InputKindMismatch {
124 port: String,
125 expected: crate::port::PortKind,
126 got: crate::port::PortKind,
127 },
128 #[error(transparent)]
129 Asset(#[from] AssetError),
130 #[error("{0}")]
131 Other(String),
132}