Skip to main content

primase_core/
material_usage.rs

1//! What a material's surface is, as the source states it (`core.material_usage`, on material entities).
2
3use serde::Serialize;
4use serde_json::Value;
5
6use crate::{Component, JsonError};
7
8/// `core.material_usage`: what the surfaces drawn with a material are, as the source states it (its shader family),
9/// for consumers that treat them differently whatever their shading: a target's material class, a viewer's wind or
10/// two-sided lighting. A reader's lowering sets it from the source through a named table and counts what it does not
11/// map; it is never guessed from names. A material without it is ordinary. Further kinds are added with a source
12/// that states them, as a new VERSION.
13#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
14#[serde(rename_all = "snake_case")]
15pub enum MaterialUsage {
16    /// Vegetation: leaves, needles, grass and the cards drawn for them.
17    Foliage,
18    /// A water surface.
19    Water,
20}
21
22impl Component for MaterialUsage {
23    const NAME: &'static str = "core.material_usage";
24    const GEOMETRY: crate::Geometry = crate::Geometry::None;
25    fn to_json(&self) -> Result<Option<Value>, JsonError> {
26        crate::json::to_value(self).map(Some)
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use crate::World;
34
35    #[test]
36    fn a_usage_shows_by_its_name() {
37        let mut w = World::new();
38        let leaves = w.spawn();
39        w.insert(leaves, MaterialUsage::Foliage);
40        let sea = w.spawn();
41        w.insert(sea, MaterialUsage::Water);
42        assert_eq!(
43            w.get::<MaterialUsage>(leaves).unwrap().to_json().unwrap(),
44            Some(serde_json::json!("foliage"))
45        );
46        assert_eq!(
47            w.get::<MaterialUsage>(sea).unwrap().to_json().unwrap(),
48            Some(serde_json::json!("water"))
49        );
50    }
51}