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