teo_runtime/value/
interface_enum_variant.rs

1use std::fmt::{Display, Formatter};
2use futures_util::StreamExt;
3use serde::Serialize;
4use crate::arguments::Arguments;
5use teo_parser::value::interface_enum_variant::InterfaceEnumVariant as ParserInterfaceEnumVariant;
6
7#[derive(Debug, Clone, Serialize, PartialEq)]
8pub struct InterfaceEnumVariant {
9    pub value: String,
10    pub args: Option<Arguments>,
11}
12
13impl InterfaceEnumVariant {
14
15    pub fn value_only(value: String) -> Self {
16        Self { value, args: None }
17    }
18
19    pub fn new(value: String, args: Arguments) -> Self {
20        Self { value, args: Some(args) }
21    }
22
23    pub fn args(&self) -> Option<&Arguments> {
24        self.args.as_ref()
25    }
26
27    pub fn value(&self) -> &str {
28        self.value.as_str()
29    }
30
31    pub fn normal_not(&self) -> bool {
32        false
33    }
34}
35
36impl Display for InterfaceEnumVariant {
37    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
38        f.write_str(self.value())?;
39        if let Some(args) = self.args() {
40            f.write_str("(")?;
41            Display::fmt(args, f)?;
42            f.write_str(")")?;
43        }
44        Ok(())
45    }
46}
47
48impl From<ParserInterfaceEnumVariant> for InterfaceEnumVariant {
49    fn from(value: ParserInterfaceEnumVariant) -> Self {
50        Self {
51            value: value.value,
52            args: value.args.map(|args| Arguments::new(args.into_iter().map(|(k, v)| (k, v.into())).collect()))
53        }
54    }
55}