1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use webcore::value::Reference;
use webcore::try_from::TryInto;
/// The `Storage` interface of the Web Storage API provides access to
/// the session storage or local storage for a particular domain.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Storage)
pub struct Storage( Reference );
reference_boilerplate! {
Storage,
instanceof Storage
}
impl Storage {
/// Gets the number of data items stored in the `Storage` object.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Storage/length)
pub fn len( &self ) -> usize {
let length: i32 = js!( return @{self}.length; ).try_into().unwrap();
length as usize
}
/// Returns a value corresponding to the key.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem)
pub fn get( &self, key: &str ) -> Option< String > {
js!( return @{self}.getItem( @{key} ); ).try_into().ok()
}
/// Inserts a key-value pair into the storage.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem)
pub fn insert( &self, key: &str, value: &str ) {
js!( @(no_return)
@{self}.setItem( @{key}, @{value} );
);
}
/// Removes a key from the storage.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Storage/removeItem)
pub fn remove( &self, key: &str ) {
js!( @(no_return)
@{self}.removeItem( @{key} );
);
}
/// When invoked, will empty all keys out of the storage.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Storage/clear)
pub fn clear( &self ) {
js!( @(no_return)
@{self}.clear();
);
}
/// Return the name of the nth key in the storage.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Storage/key)
pub fn key( &self, nth: usize ) -> Option< String > {
js!( return @{self}.key( @{nth as u32} ); ).try_into().ok()
}
/// Returns true if the storage contains a value for the specified key.
pub fn contains_key( &self, key: &str ) -> bool {
js!( return !!@{self}.getItem( @{key} ); ).try_into().unwrap()
}
}