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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use std::convert::TryFrom;

use wasm_bindgen::JsCast;

use crate::error::QuotaExceededError;

pub struct Storage {
    inner: web_sys::Storage,
}

impl Storage {
    pub fn get(&self, key: &str) -> Option<String> {
        // No indication in the spec that this can fail, unwrap for now.
        self.inner.get_item(key).unwrap()
    }

    pub fn set(&self, key: &str, value: &str) -> Result<(), QuotaExceededError> {
        self.inner.set_item(key, value).map_err(|err| {
            let err: web_sys::DomException = err.unchecked_into();

            QuotaExceededError::new(err)
        })
    }

    pub fn remove(&self, key: &str) {
        // No indication in the spec that this can fail, unwrap for now.
        self.inner.remove_item(key).unwrap();
    }

    pub fn clear(&self) {
        // No indication in the spec that this can fail, unwrap for now.
        self.inner.clear().unwrap();
    }

    pub fn len(&self) -> usize {
        // No indication in the spec that this can fail, unwrap for now.
        self.inner.length().unwrap() as usize
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn is_not_empty(&self) -> bool {
        !self.is_empty()
    }

    pub fn keys(&self) -> StorageKeys {
        StorageKeys { storage: self }
    }

    // TODO: its possible to mimic HashMap's (key, value) iter style and `values` collection, but
    // I'm not sure if this is appropriate
}

impl From<web_sys::Storage> for Storage {
    fn from(inner: web_sys::Storage) -> Self {
        Storage { inner }
    }
}

impl AsRef<web_sys::Storage> for Storage {
    fn as_ref(&self) -> &web_sys::Storage {
        &self.inner
    }
}

#[derive(Clone, Copy)]
pub struct StorageKeys<'a> {
    storage: &'a Storage,
}

impl<'a> StorageKeys<'a> {
    pub fn get(&self, index: usize) -> Option<String> {
        u32::try_from(index).ok().and_then(|index| {
            // No indication in the spec that this can fail, unwrap for now.
            self.storage.inner.key(index).unwrap()
        })
    }

    pub fn len(&self) -> usize {
        self.storage.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn is_not_empty(&self) -> bool {
        !self.is_empty()
    }

    pub fn first(&self) -> Option<String> {
        self.get(0)
    }

    pub fn last(&self) -> Option<String> {
        let len = self.len();

        if len > 0 {
            self.get(len - 1)
        } else {
            None
        }
    }

    pub fn iter(&self) -> StorageKeysIter {
        StorageKeysIter {
            storage_keys: *self,
            current: 0,
        }
    }
}

impl<'a> IntoIterator for StorageKeys<'a> {
    type Item = String;
    type IntoIter = StorageKeysIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        StorageKeysIter {
            storage_keys: self,
            current: 0,
        }
    }
}

pub struct StorageKeysIter<'a> {
    storage_keys: StorageKeys<'a>,
    current: usize,
}

impl<'a> Iterator for StorageKeysIter<'a> {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        let current = self.current;

        self.current += 1;

        self.storage_keys.get(current)
    }
}