derive serde for Errors and Location, and add error handler

This commit is contained in:
Evan Almloff committed 2022-05-31 12:03:04 -05:00
1 parent c94cf9c8e2
commit 51b87dafcc
7 files changed
+42 -34

No files matched your search

+4 -4
View File
@@ -193,9 +193,9 @@ pub fn rsx(s: TokenStream) -> TokenStream {
quote::quote! {
{
let __line_num = get_line_num();
let __rsx_text_index: RsxTextIndex = cx.consume_context().unwrap();
let __rsx_text_index: RsxContext = cx.consume_context().expect("Hot reload is not avalable on this platform");
// only the insert the rsx text once
if !__rsx_text_index.read().contains_key(&__line_num){
if !__rsx_text_index.read().hm.contains_key(&__line_num){
__rsx_text_index.insert(
__line_num.clone(),
#rsx_text.to_string(),
@@ -205,7 +205,7 @@ pub fn rsx(s: TokenStream) -> TokenStream {
if let Some(__text) = {
let read = __rsx_text_index.read();
// clone prevents deadlock on nested rsx calls
read.get(&__line_num).cloned()
read.hm.get(&__line_num).cloned()
} {
match interpert_rsx(
__cx,
@@ -213,7 +213,7 @@ pub fn rsx(s: TokenStream) -> TokenStream {
#captured
){
Ok(vnode) => vnode,
Err(err) => __cx.text(format_args!("{:?}", err))
Err(_) => __cx.text(format_args!(""))
}
}
else {
-1
View File
@@ -42,7 +42,6 @@ fullscreen = ["wry/fullscreen"]
transparent = ["wry/transparent"]
tray = ["wry/tray"]
[dev-dependencies]
dioxus-core-macro = { path = "../core-macro" }
dioxus-hooks = { path = "../hooks" }
+3
View File
@@ -7,6 +7,9 @@ license = "MIT/Apache-2.0"
[dependencies]
syn = { version = "1.0", features = ["extra-traits"] }
quote = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = { vesion = "1.0" }
dioxus-rsx = { path = "../rsx", default-features = false }
dioxus-ssr = { path = "../ssr" }
dioxus-core = { path = "../core" }
+3 -1
View File
@@ -1,10 +1,12 @@
use serde::{Deserialize, Serialize};
#[derive(Debug)]
pub enum Error {
ParseError(syn::Error),
RecompileRequiredError(RecompileReason),
}
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub enum RecompileReason {
CapturedVariable(String),
CapturedExpression(String),
+25 -22
View File
@@ -2,10 +2,10 @@ use dioxus_core::{Component, Element, LazyNodes, Scope, VNode};
use dioxus_hooks::*;
use error::Error;
use interperter::build;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::panic::Location;
use std::rc::Rc;
use std::sync::{RwLock, RwLockReadGuard};
use std::sync::{Arc, RwLock, RwLockReadGuard};
use syn::parse_str;
mod attributes;
@@ -14,25 +14,13 @@ mod elements;
mod error;
mod interperter;
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CodeLocation {
pub file: String,
pub line: u32,
pub column: u32,
}
pub fn with_hot_reload(cx: Scope<Component>) -> Element {
use_state(&cx, || {
if cx.consume_context::<RsxTextIndex>().is_none() {
let index = RsxTextIndex::default();
cx.provide_context(index);
}
});
cx.render(LazyNodes::new(|node_factory| {
node_factory.component(*cx.props, (), None, "app")
}))
}
pub fn interpert_rsx<'a, 'b>(
factory: dioxus_core::NodeFactory<'a>,
text: &str,
@@ -55,17 +43,32 @@ pub fn get_line_num() -> CodeLocation {
}
}
#[derive(Debug, Default, Clone)]
pub struct RsxTextIndex {
hm: Rc<RwLock<HashMap<CodeLocation, String>>>,
#[derive(Debug, Clone)]
pub struct RsxContext {
data: Arc<RwLock<RsxData>>,
}
impl RsxTextIndex {
pub struct RsxData {
pub hm: HashMap<CodeLocation, String>,
error_handle: Box<dyn FnMut(Error)>,
}
impl std::fmt::Debug for RsxData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RsxData").field("hm", &self.hm).finish()
}
}
impl RsxContext {
pub fn insert(&self, loc: CodeLocation, text: String) {
self.hm.write().unwrap().insert(loc, text);
self.data.write().unwrap().hm.insert(loc, text);
}
pub fn read(&self) -> RwLockReadGuard<HashMap<CodeLocation, String>> {
self.hm.read().unwrap()
pub fn read(&self) -> RwLockReadGuard<RsxData> {
self.data.read().unwrap()
}
pub fn report_error(&self, error: Error) {
(self.data.write().unwrap().error_handle)(error)
}
}