mirror of
https://github.com/zaphar/kitchen.git
synced 2025-07-22 19:40:14 -04:00
feat: some additional js_lib stuff for logging
This commit is contained in:
parent
50eecf9a7c
commit
a3aa579fa5
@ -12,13 +12,12 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
use js_sys::Date;
|
use js_sys::Date;
|
||||||
|
use tracing::error;
|
||||||
use wasm_bindgen::JsCast;
|
use wasm_bindgen::JsCast;
|
||||||
use web_sys::{window, Element, Storage};
|
use web_sys::{window, Element, Storage, Window};
|
||||||
|
|
||||||
pub fn get_storage() -> Storage {
|
pub fn get_storage() -> Storage {
|
||||||
window()
|
get_window().local_storage()
|
||||||
.expect("No Window Present")
|
|
||||||
.local_storage()
|
|
||||||
.expect("Failed to get storage")
|
.expect("Failed to get storage")
|
||||||
.expect("No storage available")
|
.expect("No storage available")
|
||||||
}
|
}
|
||||||
@ -27,13 +26,16 @@ pub fn get_ms_timestamp() -> u32 {
|
|||||||
Date::new_0().get_milliseconds()
|
Date::new_0().get_milliseconds()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_window() -> Window {
|
||||||
|
window()
|
||||||
|
.expect("No window present")
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_element_by_id<E>(id: &str) -> Result<Option<E>, Element>
|
pub fn get_element_by_id<E>(id: &str) -> Result<Option<E>, Element>
|
||||||
where
|
where
|
||||||
E: JsCast,
|
E: JsCast,
|
||||||
{
|
{
|
||||||
match window()
|
match get_window().document()
|
||||||
.expect("No window present")
|
|
||||||
.document()
|
|
||||||
.expect("No document in window")
|
.expect("No document in window")
|
||||||
.get_element_by_id(id)
|
.get_element_by_id(id)
|
||||||
{
|
{
|
||||||
@ -41,3 +43,18 @@ where
|
|||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait LogFailures<V, E> {
|
||||||
|
fn swallow_and_log(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E> LogFailures<(), E> for Result<(), E>
|
||||||
|
where
|
||||||
|
E: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
fn swallow_and_log(self) {
|
||||||
|
if let Err(e) = self {
|
||||||
|
error!(err = ?e, "Error: ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user