Skip to main content

yarnspinner_runtime/markup/parsed_markup/
markup_value.rs

1//! Adapted from <https://github.com/YarnSpinnerTool/YarnSpinner/blob/da39c7195107d8211f21c263e4084f773b84eaff/YarnSpinner/YarnSpinner.Markup/MarkupParseResult.cs>
2//! which was split into multiple files.
3
4use crate::prelude::*;
5use core::fmt::Display;
6
7/// A value associated with a markup name.
8///
9/// You do not create instances of this struct yourself. It is created
10/// by objects that can parse markup, such as [`Dialogue`]
11#[derive(Debug, Clone, PartialEq)]
12#[cfg_attr(feature = "bevy", derive(Reflect))]
13#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
14#[cfg_attr(feature = "bevy", reflect(Debug, PartialEq))]
15#[cfg_attr(
16    all(feature = "bevy", feature = "serde"),
17    reflect(Serialize, Deserialize)
18)]
19pub enum MarkupValue {
20    /// An integer value. Note that while Yarn variables make no distinction between integers and floats, markup values do.
21    Integer(u32),
22    /// A floating-point value. Note that while Yarn variables make no distinction between integers and floats, markup values do.
23    Float(f32),
24    /// A string value.
25    String(String),
26    /// A boolean value.
27    Bool(bool),
28}
29
30impl MarkupValue {
31    /// Returns the name of the enum variant.
32    pub fn type_name(&self) -> &'static str {
33        match self {
34            MarkupValue::Integer(_) => "Integer",
35            MarkupValue::Float(_) => "Float",
36            MarkupValue::String(_) => "String",
37            MarkupValue::Bool(_) => "Bool",
38        }
39    }
40}
41
42impl Display for MarkupValue {
43    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
44        match self {
45            MarkupValue::Integer(i) => write!(f, "{i}"),
46            MarkupValue::Float(fl) => write!(f, "{fl}"),
47            MarkupValue::String(s) => write!(f, "{s}"),
48            MarkupValue::Bool(b) => write!(f, "{b}"),
49        }
50    }
51}
52
53impl From<String> for MarkupValue {
54    fn from(s: String) -> Self {
55        MarkupValue::String(s)
56    }
57}
58
59impl From<&str> for MarkupValue {
60    fn from(s: &str) -> Self {
61        MarkupValue::String(s.to_string())
62    }
63}
64
65impl From<u32> for MarkupValue {
66    fn from(i: u32) -> Self {
67        MarkupValue::Integer(i)
68    }
69}
70
71impl From<f32> for MarkupValue {
72    fn from(f: f32) -> Self {
73        MarkupValue::Float(f)
74    }
75}
76
77impl From<bool> for MarkupValue {
78    fn from(b: bool) -> Self {
79        MarkupValue::Bool(b)
80    }
81}