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 70 71 72 73 74 75
use webcore::value::Reference; use webcore::try_from::TryInto; use private::TODO; /// 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) // https://html.spec.whatwg.org/#storage-2 #[derive(Clone, Debug, PartialEq, Eq, ReferenceType)] #[reference(instance_of = "Storage")] pub struct Storage( Reference ); 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) // https://html.spec.whatwg.org/#the-storage-interface:dom-storage-length pub fn len( &self ) -> u32 { js!( return @{self}.length; ).try_into().unwrap() } /// Returns a value corresponding to the key. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem) // https://html.spec.whatwg.org/#the-storage-interface:dom-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) // https://html.spec.whatwg.org/#the-storage-interface:dom-storage-setitem pub fn insert( &self, key: &str, value: &str ) -> Result< (), TODO > { js!( @(no_return) @{self}.setItem( @{key}, @{value} ); ); Ok(()) } /// Removes a key from the storage. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Storage/removeItem) // https://html.spec.whatwg.org/#the-storage-interface:dom-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) // https://html.spec.whatwg.org/#the-storage-interface:dom-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) // https://html.spec.whatwg.org/#the-storage-interface:dom-storage-key pub fn key( &self, nth: u32 ) -> Option< String > { js!( return @{self}.key( @{nth} ); ).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() } }