script/dom/
testbindingmaplikewithinterface.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5// check-tidy: no specs after this line
6
7use dom_struct::dom_struct;
8use indexmap::IndexMap;
9use js::rust::HandleObject;
10
11use super::bindings::error::Error;
12use crate::dom::bindings::cell::DomRefCell;
13use crate::dom::bindings::codegen::Bindings::TestBindingMaplikeWithInterfaceBinding::TestBindingMaplikeWithInterfaceMethods;
14use crate::dom::bindings::error::Fallible;
15use crate::dom::bindings::like::Maplike;
16use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::bindings::str::DOMString;
19use crate::dom::globalscope::GlobalScope;
20use crate::dom::testbinding::TestBinding;
21use crate::maplike;
22use crate::script_runtime::CanGc;
23
24/// maplike<DOMString, TestBinding>
25#[dom_struct]
26pub(crate) struct TestBindingMaplikeWithInterface {
27    reflector: Reflector,
28    #[custom_trace]
29    internal: DomRefCell<IndexMap<DOMString, DomRoot<TestBinding>>>,
30}
31
32impl TestBindingMaplikeWithInterface {
33    fn new(
34        global: &GlobalScope,
35        proto: Option<HandleObject>,
36        can_gc: CanGc,
37    ) -> DomRoot<TestBindingMaplikeWithInterface> {
38        reflect_dom_object_with_proto(
39            Box::new(TestBindingMaplikeWithInterface {
40                reflector: Reflector::new(),
41                internal: DomRefCell::new(IndexMap::new()),
42            }),
43            global,
44            proto,
45            can_gc,
46        )
47    }
48}
49
50impl TestBindingMaplikeWithInterfaceMethods<crate::DomTypeHolder>
51    for TestBindingMaplikeWithInterface
52{
53    fn Constructor(
54        global: &GlobalScope,
55        proto: Option<HandleObject>,
56        can_gc: CanGc,
57    ) -> Fallible<DomRoot<TestBindingMaplikeWithInterface>> {
58        Ok(TestBindingMaplikeWithInterface::new(global, proto, can_gc))
59    }
60
61    fn SetInternal(&self, key: DOMString, value: &TestBinding) {
62        let value = DomRoot::from_ref(value);
63        self.internal.set(key, value)
64    }
65
66    fn ClearInternal(&self) {
67        self.internal.clear()
68    }
69
70    fn DeleteInternal(&self, key: DOMString) -> bool {
71        self.internal.delete(key)
72    }
73
74    fn HasInternal(&self, key: DOMString) -> bool {
75        self.internal.has(key)
76    }
77
78    fn GetInternal(&self, key: DOMString) -> Fallible<DomRoot<TestBinding>> {
79        // TODO: error type?
80        self.internal
81            .borrow()
82            .get(&key)
83            .ok_or_else(|| Error::Type(format!("No entry for key {key}")))
84            .cloned()
85    }
86
87    fn Size(&self) -> u32 {
88        self.internal.size()
89    }
90}
91
92impl Maplike for TestBindingMaplikeWithInterface {
93    type Key = DOMString;
94    type Value = DomRoot<TestBinding>;
95
96    maplike!(self, internal);
97}