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