derive serde for Errors and Location, and add error handler
This commit is contained in:
1 parent
c94cf9c8e2
commit
51b87dafcc
7 files changed
+42
-34
No files matched your search
@@ -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" }
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user