script/dom/testing/
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 crate::dom::bindings::cell::DomRefCell;
12use crate::dom::bindings::codegen::Bindings::TestBindingMaplikeWithInterfaceBinding::TestBindingMaplikeWithInterfaceMethods;
13use crate::dom::bindings::error::{Error, Fallible};
14use crate::dom::bindings::like::Maplike;
15use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::globalscope::GlobalScope;
19use crate::dom::testbinding::TestBinding;
20use crate::maplike;
21use crate::script_runtime::CanGc;
22
23/// maplike<DOMString, TestBinding>
24#[dom_struct]
25pub(crate) struct TestBindingMaplikeWithInterface {
26    reflector: Reflector,
27    #[custom_trace]
28    internal: DomRefCell<IndexMap<DOMString, DomRoot<TestBinding>>>,
29}
30
31impl TestBindingMaplikeWithInterface {
32    fn new(
33        global: &GlobalScope,
34        proto: Option<HandleObject>,
35        can_gc: CanGc,
36    ) -> DomRoot<TestBindingMaplikeWithInterface> {
37        reflect_dom_object_with_proto(
38            Box::new(TestBindingMaplikeWithInterface {
39                reflector: Reflector::new(),
40                internal: DomRefCell::new(IndexMap::new()),
41            }),
42            global,
43            proto,
44            can_gc,
45        )
46    }
47}
48
49impl TestBindingMaplikeWithInterfaceMethods<crate::DomTypeHolder>
50    for TestBindingMaplikeWithInterface
51{
52    fn Constructor(
53        global: &GlobalScope,
54        proto: Option<HandleObject>,
55        can_gc: CanGc,
56    ) -> Fallible<DomRoot<TestBindingMaplikeWithInterface>> {
57        Ok(TestBindingMaplikeWithInterface::new(global, proto, can_gc))
58    }
59
60    fn SetInternal(&self, key: DOMString, value: &TestBinding) {
61        let value = DomRoot::from_ref(value);
62        self.internal.set(key, value)
63    }
64
65    fn ClearInternal(&self) {
66        self.internal.clear()
67    }
68
69    fn DeleteInternal(&self, key: DOMString) -> bool {
70        self.internal.delete(key)
71    }
72
73    fn HasInternal(&self, key: DOMString) -> bool {
74        self.internal.has(key)
75    }
76
77    fn GetInternal(&self, key: DOMString) -> Fallible<DomRoot<TestBinding>> {
78        // TODO: error type?
79        self.internal
80            .borrow()
81            .get(&key)
82            .ok_or_else(|| Error::Type(format!("No entry for key {key}")))
83            .cloned()
84    }
85
86    fn Size(&self) -> u32 {
87        self.internal.size()
88    }
89}
90
91impl Maplike for TestBindingMaplikeWithInterface {
92    type Key = DOMString;
93    type Value = DomRoot<TestBinding>;
94
95    maplike!(self, internal);
96}