@@ -160,27 +160,18 @@ mod web_mod {
160160 pub mod cookies {
161161 use thiserror:: Error ;
162162 use wasm_bindgen:: { JsCast , JsValue } ;
163- use web_sys:: { window , Document , HtmlDocument } ;
163+ use web_sys:: HtmlDocument ;
164164
165165 #[ derive( Error , Debug ) ]
166166 pub enum CookieError {
167- #[ error( "Window Object not valid in this context" ) ]
168- NoWindow ,
169- #[ error( "No `document` available on `window` object" ) ]
170- NoDocument ,
171- #[ error( "`document` is not an HtmlDocument" ) ]
172- NoHtmlDocument ,
173- #[ error( "web_sys error: {0:?}" ) ]
174- WebSys ( JsValue ) ,
175- }
167+ #[ error( "Error reading cookies: {0:?}" ) ]
168+ Get ( JsValue ) ,
176169
177- impl From < JsValue > for CookieError {
178- fn from ( err : JsValue ) -> Self {
179- CookieError :: WebSys ( err)
180- }
170+ #[ error( "Error setting cookie `{key}`: {js_value:?}" ) ]
171+ Set { key : String , js_value : JsValue } ,
181172 }
182173
183- /// A builder for contructing and setting cookies.
174+ /// A builder for constructing and setting cookies.
184175 pub struct Cookie {
185176 name : String ,
186177 value : String ,
@@ -207,8 +198,8 @@ mod web_mod {
207198
208199 /// Gets the value of a cookie by name.
209200 pub fn get ( name : & str ) -> Result < Option < String > , CookieError > {
210- let doc = get_html_document ( ) ? ;
211- let all = doc. cookie ( ) . map_err ( CookieError :: from ) ?;
201+ let doc = get_html_document ( ) ;
202+ let all = doc. cookie ( ) . map_err ( |e| CookieError :: Get ( e ) ) ?;
212203 for cookie in all. split ( ';' ) {
213204 let cookie = cookie. trim ( ) ;
214205 if let Some ( ( k, v) ) = cookie. split_once ( '=' ) {
@@ -240,7 +231,7 @@ mod web_mod {
240231 }
241232
242233 /// Toggles the `Secure` flag.
243- /// The default is `false`.
234+ /// Defaults to `false`.
244235 pub fn secure ( mut self , enabled : bool ) -> Self {
245236 self . secure = enabled;
246237 self
@@ -253,7 +244,7 @@ mod web_mod {
253244 }
254245
255246 pub fn set ( self ) -> Result < ( ) , CookieError > {
256- let doc = get_html_document ( ) ? ;
247+ let doc = get_html_document ( ) ;
257248 let mut parts = vec ! [ format!( "{}={}" , self . name, self . value) ] ;
258249
259250 if let Some ( path) = self . path {
@@ -273,7 +264,10 @@ mod web_mod {
273264 }
274265
275266 let cookie_str = parts. join ( "; " ) ;
276- doc. set_cookie ( & cookie_str) . map_err ( CookieError :: from)
267+ doc. set_cookie ( & cookie_str) . map_err ( |e| CookieError :: Set {
268+ key : self . name . clone ( ) ,
269+ js_value : e,
270+ } )
277271 }
278272
279273 /// Deletes the cookie by setting its value to empty and `Max-Age=0`.
@@ -305,16 +299,8 @@ mod web_mod {
305299 }
306300 }
307301
308- fn get_document ( ) -> Result < Document , CookieError > {
309- window ( )
310- . ok_or ( CookieError :: NoWindow ) ?
311- . document ( )
312- . ok_or ( CookieError :: NoDocument )
313- }
314-
315- fn get_html_document ( ) -> Result < HtmlDocument , CookieError > {
316- let doc = get_document ( ) ?;
317- doc. dyn_into :: < HtmlDocument > ( ) . map_err ( |_| CookieError :: NoHtmlDocument )
302+ fn get_html_document ( ) -> HtmlDocument {
303+ gloo_utils:: document ( ) . unchecked_into :: < HtmlDocument > ( )
318304 }
319305 }
320306}
0 commit comments