stdweb/webapi/string_map.rs
1use webcore::value::Reference;
2use webcore::try_from::TryInto;
3use private::TODO;
4
5/// Used by the `dataset` HTML attribute to represent data for custom attributes added to elements.
6///
7/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/DOMStringMap)
8// https://html.spec.whatwg.org/#domstringmap
9#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
10#[reference(instance_of = "DOMStringMap")]
11pub struct StringMap( Reference );
12
13// The methods here are deliberately named exactly as those from Rust's HashMap.
14impl StringMap {
15 /// Returns a value corresponding to the key.
16 // https://html.spec.whatwg.org/#dom-domstringmap-nameditem
17 pub fn get( &self, key: &str ) -> Option< String > {
18 js!( return @{self}[ @{key} ]; ).try_into().ok()
19 }
20
21 /// Inserts a key-value pair into the map.
22 // https://html.spec.whatwg.org/#dom-domstringmap-setitem
23 pub fn insert( &self, key: &str, value: &str ) -> Result< (), TODO > {
24 js!( @(no_return)
25 @{self}[ @{key} ] = @{value};
26 );
27
28 Ok(())
29 }
30
31 /// Removes a key from the map.
32 // https://html.spec.whatwg.org/#dom-domstringmap-removeitem
33 pub fn remove( &self, key: &str ) {
34 js!( @(no_return)
35 delete @{self}[ @{key} ];
36 );
37 }
38
39 /// Returns true if the map contains a value for the specified key.
40 pub fn contains_key( &self, key: &str ) -> bool {
41 js!( return @{key} in @{self}; ).try_into().unwrap()
42 }
43}