Skip to main content

gir_parser/
callable.rs

1use xmlserde_derives::XmlDeserialize;
2
3use crate::{
4    function::Function,
5    method::Method,
6    parameter::Parameters,
7    return_value::ReturnValue,
8    traits::{Callable as CallableTrait, FunctionLike},
9};
10
11#[derive(Clone, Debug, PartialEq, Eq, XmlDeserialize)]
12pub enum Callable {
13    #[xmlserde(name = b"constructor")]
14    Constructor(Function),
15    #[xmlserde(name = b"method")]
16    Method(Method),
17    #[xmlserde(name = b"function")]
18    Function(Function),
19}
20
21impl Callable {
22    pub fn is_constructor(&self) -> bool {
23        matches!(self, Self::Constructor(_))
24    }
25
26    pub fn is_method(&self) -> bool {
27        matches!(self, Self::Method(_))
28    }
29
30    pub fn is_function(&self) -> bool {
31        matches!(self, Self::Function(_))
32    }
33
34    pub fn as_function(&self) -> Option<&Function> {
35        match self {
36            Self::Constructor(f) | Self::Function(f) => Some(f),
37            _ => None,
38        }
39    }
40
41    pub fn as_method(&self) -> Option<&Method> {
42        match self {
43            Self::Method(m) => Some(m),
44            _ => None,
45        }
46    }
47
48    pub fn name(&self) -> &str {
49        match self {
50            Self::Constructor(f) | Self::Function(f) => f.name(),
51            Self::Method(m) => m.name(),
52        }
53    }
54
55    pub fn c_identifier(&self) -> Option<&str> {
56        match self {
57            Self::Constructor(f) | Self::Function(f) => f.c_identifier(),
58            Self::Method(m) => m.c_identifier(),
59        }
60    }
61
62    pub fn moved_to(&self) -> Option<&str> {
63        match self {
64            Self::Constructor(f) | Self::Function(f) => f.moved_to(),
65            Self::Method(m) => m.moved_to(),
66        }
67    }
68
69    pub fn throws(&self) -> bool {
70        match self {
71            Self::Constructor(f) | Self::Function(f) => f.throws(),
72            Self::Method(m) => m.throws(),
73        }
74    }
75
76    pub fn return_value(&self) -> &ReturnValue {
77        match self {
78            Self::Constructor(f) | Self::Function(f) => f.return_value(),
79            Self::Method(m) => m.return_value(),
80        }
81    }
82
83    pub fn parameters(&self) -> &Parameters {
84        match self {
85            Self::Constructor(f) | Self::Function(f) => f.parameters(),
86            Self::Method(m) => m.parameters(),
87        }
88    }
89}