Skip to main content

vapoursynth4_rs/plugin/
types.rs

1use std::{borrow::Cow, collections::HashMap};
2
3use crate::map::{Key, KeyStr};
4
5pub enum Type {
6    Int,
7    Float,
8    Data,
9    ANode,
10    VNode,
11    AFrame,
12    VFrame,
13    Func,
14    Array(Box<Type>),
15}
16
17impl Type {
18    #[must_use]
19    pub fn to_args(&self) -> Cow<'static, str> {
20        use Type as t;
21        match self {
22            t::Int => "int".into(),
23            t::Float => "float".into(),
24            t::Data => "data".into(),
25            t::ANode => "anode".into(),
26            t::VNode => "vnode".into(),
27            t::AFrame => "aframe".into(),
28            t::VFrame => "vframe".into(),
29            t::Func => "func".into(),
30            t::Array(t) => t.to_args() + "[]",
31        }
32    }
33}
34
35pub struct TypeBuilder {
36    types: HashMap<Key, (Type, bool)>,
37}
38
39impl TypeBuilder {
40    #[must_use]
41    pub fn new() -> Self {
42        Self {
43            types: HashMap::new(),
44        }
45    }
46
47    pub fn add(&mut self, key: &KeyStr, ty: Type, opt: bool) -> &mut Self {
48        self.types.insert(key.into(), (ty, opt));
49        self
50    }
51
52    #[must_use]
53    pub fn build(self) -> HashMap<Key, (Type, bool)> {
54        self.types
55    }
56}
57
58impl Default for TypeBuilder {
59    fn default() -> Self {
60        Self::new()
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    use crate::key;
68
69    #[test]
70    fn builds_plugin_argument_types() {
71        let mut builder = TypeBuilder::default();
72        for (key, ty, name) in [
73            (key!(c"a"), Type::Int, "int"),
74            (key!(c"b"), Type::Float, "float"),
75            (key!(c"c"), Type::Data, "data"),
76            (key!(c"d"), Type::ANode, "anode"),
77            (key!(c"e"), Type::VNode, "vnode"),
78            (key!(c"f"), Type::AFrame, "aframe"),
79            (key!(c"g"), Type::VFrame, "vframe"),
80            (key!(c"h"), Type::Func, "func"),
81        ] {
82            assert_eq!(ty.to_args(), name);
83            builder.add(key, ty, false);
84        }
85        let values = Key::from(key!(c"values"));
86        builder.add(&values, Type::Array(Box::new(Type::Int)), true);
87        let args = builder.build();
88        assert_eq!(args.len(), 9);
89        assert_eq!(args[&values].0.to_args(), "int[]");
90        assert!(args[&values].1);
91    }
92}