1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use crate::model::*;

pub struct ClassBuilder<'a> {
    lib: &'a mut LibraryBuilder,
    declaration: ClassDeclarationHandle,
    constructor: Option<ClassConstructor<Unvalidated>>,
    destructor: Option<ClassDestructor<Unvalidated>>,
    methods: Vec<Method<Unvalidated>>,
    static_methods: Vec<StaticMethod<Unvalidated>>,
    async_methods: Vec<FutureMethod<Unvalidated>>,
    doc: Option<Doc<Unvalidated>>,
    destruction_mode: DestructionMode,
}

impl<'a> ClassBuilder<'a> {
    pub(crate) fn new(lib: &'a mut LibraryBuilder, declaration: ClassDeclarationHandle) -> Self {
        Self {
            lib,
            declaration,
            constructor: None,
            destructor: None,
            methods: Vec::new(),
            static_methods: Vec::new(),
            async_methods: Vec::new(),
            doc: None,
            destruction_mode: DestructionMode::Automatic,
        }
    }

    pub fn doc<D: Into<Doc<Unvalidated>>>(mut self, doc: D) -> BindResult<Self> {
        match self.doc {
            None => {
                self.doc = Some(doc.into());
                Ok(self)
            }
            Some(_) => Err(BindingErrorVariant::DocAlreadyDefined {
                symbol_name: self.declaration.name.clone(),
            }
            .into()),
        }
    }

    pub fn constructor(mut self, constructor: ClassConstructor<Unvalidated>) -> BindResult<Self> {
        // make sure the method is defined for this class
        self.check_class(&constructor.function.name, constructor.class.clone())?;

        if self.constructor.is_some() {
            return Err(BindingErrorVariant::ConstructorAlreadyDefined {
                handle: self.declaration,
            }
            .into());
        }

        self.constructor = Some(constructor);

        Ok(self)
    }

    pub fn destructor(mut self, destructor: ClassDestructor<Unvalidated>) -> BindResult<Self> {
        if self.destructor.is_some() {
            return Err(BindingErrorVariant::DestructorAlreadyDefined {
                handle: self.declaration,
            }
            .into());
        }

        // make sure the method is defined for this class
        self.check_class(&destructor.function.name, destructor.class.clone())?;

        self.destructor = Some(destructor);

        Ok(self)
    }

    pub fn method(mut self, method: Method<Unvalidated>) -> BindResult<Self> {
        // make sure the method is defined for this class
        self.check_class(&method.name, method.associated_class.clone())?;

        self.methods.push(method);

        Ok(self)
    }

    pub fn static_method(mut self, method: StaticMethod<Unvalidated>) -> BindResult<Self> {
        self.static_methods.push(method);
        Ok(self)
    }

    fn check_class(&self, name: &Name, other: ClassDeclarationHandle) -> BindResult<()> {
        if self.declaration != other {
            return Err(BindingErrorVariant::ClassMemberWrongAssociatedClass {
                name: name.clone(),
                declared: other,
                added_to: self.declaration.clone(),
            }
            .into());
        }
        Ok(())
    }

    pub fn async_method(mut self, method: FutureMethod<Unvalidated>) -> BindResult<Self> {
        self.check_class(&method.name, method.associated_class.clone())?;

        self.async_methods.push(method);

        Ok(self)
    }

    pub fn custom_destroy<T: IntoName>(mut self, name: T) -> BindResult<Self> {
        if self.destructor.is_none() {
            return Err(BindingErrorVariant::NoDestructorForManualDestruction {
                handle: self.declaration,
            }
            .into());
        }

        self.destruction_mode = DestructionMode::Custom(name.into_name()?);
        Ok(self)
    }

    pub fn disposable_destroy(mut self) -> BindResult<Self> {
        if self.destructor.is_none() {
            return Err(BindingErrorVariant::NoDestructorForManualDestruction {
                handle: self.declaration,
            }
            .into());
        }

        self.destruction_mode = DestructionMode::Dispose;
        Ok(self)
    }

    pub fn build(self) -> BindResult<ClassHandle> {
        let doc = match self.doc {
            Some(doc) => doc,
            None => {
                return Err(BindingErrorVariant::DocNotDefined {
                    symbol_name: self.declaration.name.clone(),
                }
                .into())
            }
        };

        let handle = Handle::new(Class {
            declaration: self.declaration.clone(),
            constructor: self.constructor,
            destructor: self.destructor,
            methods: self.methods,
            static_methods: self.static_methods,
            future_methods: self.async_methods,
            doc,
            destruction_mode: self.destruction_mode,
            settings: self.lib.clone_settings(),
        });

        self.lib
            .add_statement(Statement::ClassDefinition(handle.clone()))?;

        Ok(handle)
    }
}

pub struct StaticClassBuilder<'a> {
    lib: &'a mut LibraryBuilder,
    name: Name,
    static_methods: Vec<StaticMethod<Unvalidated>>,
    doc: OptionalDoc,
}

impl<'a> StaticClassBuilder<'a> {
    pub(crate) fn new(lib: &'a mut LibraryBuilder, name: Name) -> Self {
        Self {
            lib,
            name: name.clone(),
            static_methods: Vec::new(),
            doc: OptionalDoc::new(name),
        }
    }

    pub fn doc<D: Into<Doc<Unvalidated>>>(mut self, doc: D) -> BindResult<Self> {
        self.doc.set(doc.into())?;
        Ok(self)
    }

    pub fn static_method(mut self, method: StaticMethod<Unvalidated>) -> BindResult<Self> {
        self.static_methods.push(method);
        Ok(self)
    }

    pub fn build(self) -> BindResult<StaticClassHandle> {
        let handle = Handle::new(StaticClass {
            name: self.name,
            static_methods: self.static_methods,
            doc: self.doc.extract()?,
        });

        self.lib
            .add_statement(Statement::StaticClassDefinition(handle.clone()))?;

        Ok(handle)
    }
}