Skip to content

Commit

Permalink
feat: add more data conversion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Apr 19, 2024
1 parent 06ebc00 commit 0e3df5d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/core/src/dom/component/stateful_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ where
let mount_event = on_component_mount(move |me| {
program.mount(
&me.target_node.as_node(),
MountProcedure::append_to_shadow(),
//MountProcedure::append_to_shadow(),
MountProcedure::append(),
);
let stylesheet = <COMP as Component>::stylesheet().join("\n");
log::info!("stylesheet: {}", stylesheet);
Expand Down
24 changes: 24 additions & 0 deletions crates/core/src/dom/dom_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,28 @@ impl DomAttrValue {
let simple = self.as_simple()?;
Some(simple.to_string())
}

/// return i32 if it can be converted as such
pub fn as_i32(&self) -> Option<i32> {
let simple = self.as_simple()?;
simple.as_i32()
}

/// return i64 if it can be converted as such
pub fn as_i64(&self) -> Option<i64> {
let simple = self.as_simple()?;
simple.as_i64()
}

/// return i32 if it can be converted as such
pub fn as_f32(&self) -> Option<f32> {
let simple = self.as_simple()?;
simple.as_f32()
}

/// return i64 if it can be converted as such
pub fn as_f64(&self) -> Option<f64> {
let simple = self.as_simple()?;
simple.as_f64()
}
}
46 changes: 46 additions & 0 deletions crates/core/src/vdom/attribute/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ impl Value {
}
}

/// converts to f32 if the variants are numerical representation
pub fn as_f32(&self) -> Option<f32> {
match self {
Self::Bool(_) => None,
Self::Cow(_v) => None,
Self::Vec(_v) => None,
Self::U8(v) => Some(f32::from(*v)),
Self::U16(v) => Some(f32::from(*v)),
Self::U32(v) => Some(*v as f32),
Self::U64(v) => Some(*v as f32),
Self::U128(v) => Some(*v as f32),
Self::Usize(v) => Some(*v as f32),
Self::I8(v) => Some(f32::from(*v)),
Self::I16(v) => Some(f32::from(*v)),
Self::I32(v) => Some(*v as f32),
Self::I64(v) => Some(*v as f32),
Self::I128(v) => Some(*v as f32),
Self::Isize(v) => Some(*v as f32),
Self::F32(v) => Some(*v),
Self::F64(v) => Some(*v as f32),
}
}

/// converts to f64 if the variants are numerical representation
pub fn as_f64(&self) -> Option<f64> {
match self {
Expand Down Expand Up @@ -108,6 +131,29 @@ impl Value {
}
}

/// converts to i64 if the variants are numerical representation
pub fn as_i64(&self) -> Option<i64> {
match self {
Self::Bool(_) => None,
Self::Cow(_v) => None,
Self::Vec(_v) => None,
Self::U8(v) => Some(i64::from(*v)),
Self::U16(v) => Some(i64::from(*v)),
Self::U32(v) => Some(*v as i64),
Self::U64(v) => Some(*v as i64),
Self::U128(v) => Some(*v as i64),
Self::Usize(v) => Some(*v as i64),
Self::I8(v) => Some(i64::from(*v)),
Self::I16(v) => Some(i64::from(*v)),
Self::I32(v) => Some(*v as i64),
Self::I64(v) => Some(*v),
Self::I128(v) => Some(*v as i64),
Self::Isize(v) => Some(*v as i64),
Self::F32(v) => Some(*v as i64),
Self::F64(v) => Some(*v as i64),
}
}

/// If this is Value::Vec variant, append the new value
/// otherwise, turn this value into Value::Vec(Vec<Value>) variant
/// and append the new value.
Expand Down

0 comments on commit 0e3df5d

Please sign in to comment.