sushi-rs 0.0.1

🍣 A highly customizable SVG sushi component for WASM frameworks like Yew, Dioxus, and Leptos.
Documentation
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use strum_macros::{AsRefStr, Display, EnumIter, EnumString};

/// The outer shape of the sushi piece.
#[derive(
    EnumString, EnumIter, AsRefStr, Display, Debug, Eq, PartialEq, Hash, Clone, Copy, Default,
)]
pub enum SushiShape {
    /// Classic round maki or gunkan roll.
    #[default]
    Circular,
    /// Square or rectangular tamago / oshi-style roll.
    Square,
    /// Triangular onigiri shape.
    Triangular,
    /// Oval / nigiri hand-pressed shape.
    Oval,
}

/// Whether the sushi is assembled or shown in layers.
#[derive(
    EnumString, EnumIter, AsRefStr, Display, Debug, Eq, PartialEq, Hash, Clone, Copy, Default,
)]
pub enum SushiState {
    /// Fully assembled sushi roll.
    #[default]
    Rolled,
    /// Deconstructed: every component displayed side-by-side.
    Exploded,
}

/// Camera / render perspective.
#[derive(
    EnumString, EnumIter, AsRefStr, Display, Debug, Eq, PartialEq, Hash, Clone, Copy, Default,
)]
pub enum SushiView {
    /// Bird's-eye view looking straight down.
    #[default]
    Top,
    /// Isometric frontal view showing depth.
    Front,
}

/// Decoration applied to the top rim of the sushi.
#[derive(
    EnumString, EnumIter, AsRefStr, Display, Debug, Eq, PartialEq, Hash, Clone, Copy, Default,
)]
pub enum TopEdgeType {
    /// No edge decoration.
    #[default]
    None,
    /// Red salmon roe (ikura) ring.
    RedCaviar,
    /// Black caviar ring.
    BlackCaviar,
    /// Toasted sesame seed ring.
    Sesame,
    /// Fresh herb specks ring.
    Herbs,
    /// Flying-fish roe (tobiko) ring.
    Tobiko,
}

/// Inner filling / topping ingredient.
#[derive(EnumString, EnumIter, AsRefStr, Display, Debug, Eq, PartialEq, Hash, Clone, Copy)]
pub enum Ingredient {
    Salmon,
    Tuna,
    Avocado,
    Egg,
    RedCaviar,
    BlackCaviar,
    Cucumber,
    Crab,
    Shrimp,
    Mango,
    CreamCheese,
    Wasabi,
}

/// Visual metadata for a single ingredient.
#[derive(Debug, Clone)]
pub struct IngredientMeta {
    /// Primary SVG fill colour.
    pub fill: &'static str,
    /// Border / stroke colour.
    pub stroke: &'static str,
    /// Human-readable display label.
    pub label: &'static str,
    /// Emoji representation.
    pub emoji: &'static str,
    /// Lighter highlight shade for texture shading.
    pub light: &'static str,
}

/// Width and height in pixels for the sushi canvas.
#[derive(Debug, Clone, PartialEq)]
pub struct SushiSize {
    /// Canvas width in pixels.
    pub width: f64,
    /// Canvas height in pixels.
    pub height: f64,
}

impl Default for SushiSize {
    fn default() -> Self {
        Self {
            width: 120.0,
            height: 120.0,
        }
    }
}

/// The outer nori / seaweed sheet properties.
#[derive(Debug, Clone, PartialEq)]
pub struct SushiOuterSheet {
    /// Hex colour of the wrapper. Use `"transparent"` for nigiri.
    pub color: String,
    /// Thickness of the wrapper band in pixels.
    pub thickness: f64,
}

impl Default for SushiOuterSheet {
    fn default() -> Self {
        Self {
            color: NORI_COLOR.to_string(),
            thickness: 10.0,
        }
    }
}

/// A decorative ring shown on the top rim of the sushi.
#[derive(Debug, Clone, PartialEq)]
pub struct TopEdgeDecoration {
    /// The kind of decoration.
    pub edge_type: TopEdgeType,
    /// Optional colour override for the decoration element.
    pub color: Option<String>,
}

impl Default for TopEdgeDecoration {
    fn default() -> Self {
        Self {
            edge_type: TopEdgeType::None,
            color: None,
        }
    }
}

/// Complete data model describing one sushi piece.
#[derive(Debug, Clone, PartialEq)]
pub struct SushiData {
    /// Unique identifier used to namespace SVG `id` attributes.
    pub id: String,
    /// Human-readable name displayed in labels and ARIA attributes.
    pub name: String,
    /// Outer silhouette shape.
    pub shape: SushiShape,
    /// Whether the sushi is rolled or deconstructed.
    pub state: SushiState,
    /// Render perspective (defaults to `Top`).
    pub view: SushiView,
    /// Ordered list of inner ingredients.
    pub ingredients: Vec<Ingredient>,
    /// Nori / outer sheet configuration.
    pub outer_sheet: SushiOuterSheet,
    /// Canvas dimensions in pixels.
    pub size: SushiSize,
    /// Optional custom rice colour (defaults to [`RICE_COLOR`]).
    pub rice_color: Option<String>,
    /// Optional top-rim decoration.
    pub top_edge: Option<TopEdgeDecoration>,
    /// Optional human-readable description.
    pub description: Option<String>,
}

impl Default for SushiData {
    fn default() -> Self {
        Self {
            id: "sushi-default".into(),
            name: "Custom Roll".into(),
            shape: SushiShape::Circular,
            state: SushiState::Rolled,
            view: SushiView::Top,
            ingredients: vec![Ingredient::Salmon],
            outer_sheet: SushiOuterSheet::default(),
            size: SushiSize::default(),
            rice_color: None,
            top_edge: None,
            description: None,
        }
    }
}

/// Default nori seaweed colour.
pub const NORI_COLOR: &str = "#0D1F08";

/// Default shari rice colour.
pub const RICE_COLOR: &str = "#F7F3E8";

/// Returns the display metadata for an ingredient.
pub fn ingredient_meta(ingredient: Ingredient) -> IngredientMeta {
    match ingredient {
        Ingredient::Salmon => IngredientMeta {
            fill: "#FA8072",
            stroke: "#E05A4A",
            light: "#FFAAA0",
            label: "Salmon",
            emoji: "🐟",
        },
        Ingredient::Tuna => IngredientMeta {
            fill: "#C0392B",
            stroke: "#992D22",
            light: "#D9574A",
            label: "Tuna",
            emoji: "🐠",
        },
        Ingredient::Avocado => IngredientMeta {
            fill: "#5D9E2F",
            stroke: "#4A7C25",
            light: "#7EC84A",
            label: "Avocado",
            emoji: "🥑",
        },
        Ingredient::Egg => IngredientMeta {
            fill: "#FFD700",
            stroke: "#E5BE00",
            light: "#FFED6A",
            label: "Tamago Egg",
            emoji: "🥚",
        },
        Ingredient::RedCaviar => IngredientMeta {
            fill: "#E74C3C",
            stroke: "#C0392B",
            light: "#FF7F6E",
            label: "Red Caviar",
            emoji: "🔴",
        },
        Ingredient::BlackCaviar => IngredientMeta {
            fill: "#2C2C2C",
            stroke: "#444444",
            light: "#555555",
            label: "Black Caviar",
            emoji: "⚫",
        },
        Ingredient::Cucumber => IngredientMeta {
            fill: "#7AC74C",
            stroke: "#5E9B37",
            light: "#A0E066",
            label: "Cucumber",
            emoji: "🥒",
        },
        Ingredient::Crab => IngredientMeta {
            fill: "#E8733A",
            stroke: "#C5562A",
            light: "#FF9A68",
            label: "Crab",
            emoji: "🦀",
        },
        Ingredient::Shrimp => IngredientMeta {
            fill: "#FFB5A0",
            stroke: "#E89080",
            light: "#FFD5C8",
            label: "Shrimp",
            emoji: "🍤",
        },
        Ingredient::Mango => IngredientMeta {
            fill: "#FF9800",
            stroke: "#E58800",
            light: "#FFB84D",
            label: "Mango",
            emoji: "🥭",
        },
        Ingredient::CreamCheese => IngredientMeta {
            fill: "#FFFCF0",
            stroke: "#E0D8C0",
            light: "#FFFFFF",
            label: "Cream Cheese",
            emoji: "🧀",
        },
        Ingredient::Wasabi => IngredientMeta {
            fill: "#5CB85C",
            stroke: "#4A9A4A",
            light: "#7ACF7A",
            label: "Wasabi",
            emoji: "🌿",
        },
    }
}

/// Returns the SVG pattern `url(#...)` fill reference for an ingredient's texture.
pub fn ingredient_texture_id(ingredient: Ingredient, uid: &str) -> String {
    match ingredient {
        Ingredient::Salmon => format!("url(#salmon-p-{uid})"),
        Ingredient::RedCaviar => format!("url(#rcav-p-{uid})"),
        Ingredient::BlackCaviar => format!("url(#bcav-p-{uid})"),
        _ => format!("url(#sheen-p-{uid})"),
    }
}

/// Converts polar coordinates (degrees, starting at 12 o'clock) to Cartesian.
pub fn polar_to_cart(cx: f64, cy: f64, r: f64, deg: f64) -> (f64, f64) {
    let rad = (deg - 90.0) * std::f64::consts::PI / 180.0;
    (cx + r * rad.cos(), cy + r * rad.sin())
}

/// Builds an SVG arc path from a centre, through a start angle to an end angle.
pub fn describe_arc(cx: f64, cy: f64, r: f64, start_deg: f64, end_deg: f64) -> String {
    let (x1, y1) = polar_to_cart(cx, cy, r, end_deg);
    let (x2, y2) = polar_to_cart(cx, cy, r, start_deg);
    let large = if end_deg - start_deg <= 180.0 { 0 } else { 1 };
    format!(
        "M {cx} {cy} L {x1} {y1} A {r} {r} 0 {large} 0 {x2} {y2} Z",
        cx = cx,
        cy = cy,
        x1 = x1,
        y1 = y1,
        r = r,
        large = large,
        x2 = x2,
        y2 = y2
    )
}

/// Pre-built gallery of sushi pieces used for demo renders.
pub fn default_sushi_gallery() -> Vec<SushiData> {
    vec![
        SushiData {
            id: "salmon-maki".into(),
            name: "Salmon Maki".into(),
            shape: SushiShape::Circular,
            state: SushiState::Rolled,
            view: SushiView::Top,
            ingredients: vec![Ingredient::Salmon],
            outer_sheet: SushiOuterSheet {
                color: NORI_COLOR.into(),
                thickness: 10.0,
            },
            size: SushiSize {
                width: 120.0,
                height: 120.0,
            },
            rice_color: None,
            top_edge: None,
            description: Some("Classic salmon maki roll with fresh Atlantic salmon.".into()),
        },
        SushiData {
            id: "tuna-maki".into(),
            name: "Tuna Maki".into(),
            shape: SushiShape::Circular,
            state: SushiState::Rolled,
            view: SushiView::Top,
            ingredients: vec![Ingredient::Tuna],
            outer_sheet: SushiOuterSheet {
                color: NORI_COLOR.into(),
                thickness: 10.0,
            },
            size: SushiSize {
                width: 120.0,
                height: 120.0,
            },
            rice_color: None,
            top_edge: None,
            description: Some("Bluefin tuna wrapped in nori seaweed.".into()),
        },
        SushiData {
            id: "tamago-square".into(),
            name: "Tamago Square".into(),
            shape: SushiShape::Square,
            state: SushiState::Rolled,
            view: SushiView::Top,
            ingredients: vec![Ingredient::Egg],
            outer_sheet: SushiOuterSheet {
                color: NORI_COLOR.into(),
                thickness: 8.0,
            },
            size: SushiSize {
                width: 120.0,
                height: 120.0,
            },
            rice_color: None,
            top_edge: None,
            description: Some("Sweet Japanese tamago egg in a seaweed square wrap.".into()),
        },
        SushiData {
            id: "tuna-onigiri".into(),
            name: "Tuna Onigiri".into(),
            shape: SushiShape::Triangular,
            state: SushiState::Rolled,
            view: SushiView::Top,
            ingredients: vec![Ingredient::Tuna],
            outer_sheet: SushiOuterSheet {
                color: NORI_COLOR.into(),
                thickness: 6.0,
            },
            size: SushiSize {
                width: 110.0,
                height: 120.0,
            },
            rice_color: None,
            top_edge: None,
            description: Some("Traditional triangular rice ball filled with tuna.".into()),
        },
        SushiData {
            id: "salmon-nigiri".into(),
            name: "Salmon Nigiri".into(),
            shape: SushiShape::Oval,
            state: SushiState::Rolled,
            view: SushiView::Top,
            ingredients: vec![Ingredient::Salmon],
            outer_sheet: SushiOuterSheet {
                color: "transparent".into(),
                thickness: 0.0,
            },
            size: SushiSize {
                width: 130.0,
                height: 100.0,
            },
            rice_color: None,
            top_edge: None,
            description: Some("Hand-pressed salmon draped over seasoned shari rice.".into()),
        },
        SushiData {
            id: "rainbow-roll".into(),
            name: "Rainbow Roll".into(),
            shape: SushiShape::Circular,
            state: SushiState::Rolled,
            view: SushiView::Top,
            ingredients: vec![Ingredient::Salmon, Ingredient::Avocado, Ingredient::Tuna],
            outer_sheet: SushiOuterSheet {
                color: NORI_COLOR.into(),
                thickness: 10.0,
            },
            size: SushiSize {
                width: 120.0,
                height: 120.0,
            },
            rice_color: None,
            top_edge: Some(TopEdgeDecoration {
                edge_type: TopEdgeType::Sesame,
                color: None,
            }),
            description: Some("Colorful rainbow roll with premium multi-topping.".into()),
        },
    ]
}