Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Mar 5, 2024
1 parent dd70989 commit 09faeb6
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 57 deletions.
50 changes: 38 additions & 12 deletions crates/core/src/dom/component.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::html::attributes::{class, classes, Attribute};
use crate::{dom::Effects, vdom::Node};
use crate::vdom::AttributeName;
use crate::vdom::AttributeValue;
use crate::{dom::Effects, vdom::Node};

/// A component has a view and can update itself.
///
Expand All @@ -26,7 +26,10 @@ where
fn view(&self) -> Node<MSG>;

/// component can have static styles
fn stylesheet() -> Vec<String> where Self: Sized{
fn stylesheet() -> Vec<String>
where
Self: Sized,
{
vec![]
}

Expand All @@ -38,12 +41,18 @@ where

/// return the component name
/// defaults to the struct simplified name
fn component_name() -> String where Self: Sized {
fn component_name() -> String
where
Self: Sized,
{
extract_simple_struct_name::<Self>()
}

/// prefix the class bane
fn prefix_class(class_name: &str) -> String where Self: Sized{
fn prefix_class(class_name: &str) -> String
where
Self: Sized,
{
let component_name = Self::component_name();
if class_name.is_empty() {
component_name
Expand All @@ -53,12 +62,18 @@ where
}

/// create a classname prepended with this component name
fn class_ns(class_name: &str) -> Attribute<MSG> where Self: Sized {
fn class_ns(class_name: &str) -> Attribute<MSG>
where
Self: Sized,
{
class(Self::prefix_class(class_name))
}

/// create namespaced class names to pair that evaluates to true
fn classes_ns_flag(pair: impl IntoIterator<Item = (impl ToString, bool)>) -> Attribute<MSG> where Self: Sized {
fn classes_ns_flag(pair: impl IntoIterator<Item = (impl ToString, bool)>) -> Attribute<MSG>
where
Self: Sized,
{
let class_list = pair.into_iter().filter_map(|(class, flag)| {
if flag {
Some(Self::prefix_class(&class.to_string()))
Expand All @@ -71,7 +86,10 @@ where
}

/// create a selector class prepended with this component name
fn selector_ns(class_name: &str) -> String where Self: Sized{
fn selector_ns(class_name: &str) -> String
where
Self: Sized,
{
let component_name = Self::component_name();
if class_name.is_empty() {
format!(".{component_name}")
Expand All @@ -81,7 +99,10 @@ where
}

/// create namesspaced selector from multiple classnames
fn selectors_ns(class_names: impl IntoIterator<Item = impl ToString>) -> String where Self: Sized {
fn selectors_ns(class_names: impl IntoIterator<Item = impl ToString>) -> String
where
Self: Sized,
{
let selectors: Vec<String> = class_names
.into_iter()
.map(|class_name| Self::selector_ns(&class_name.to_string()))
Expand Down Expand Up @@ -195,17 +216,22 @@ pub(crate) fn extract_simple_struct_name<T: ?Sized>() -> String {
}

/// A component that can be used directly in the view without mapping
pub trait StatefulComponent<MSG>: Component<MSG, MSG> where MSG: 'static{

pub trait StatefulComponent<MSG>: Component<MSG, MSG>
where
MSG: 'static,
{
/// returns the attributes that is observed by this component
/// These are the names of the attributes the component is interested in
fn observed_attributes() -> Vec<AttributeName> where Self: Sized;
fn observed_attributes() -> Vec<AttributeName>
where
Self: Sized;

/// This will be invoked when a component is used as a custom element
/// and the attributes of the custom-element has been modified
///
/// if the listed attributes in the observed attributes are modified
fn attribute_changed(&mut self,
fn attribute_changed(
&mut self,
attr_name: AttributeName,
old_value: AttributeValue<MSG>,
new_value: AttributeValue<MSG>,
Expand Down
5 changes: 4 additions & 1 deletion crates/core/src/dom/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ where
F: Fn(Event) -> MSG + 'static,
MSG: 'static,
{
vdom::attr(event_name, AttributeValue::EventListener(EventCallback::from(f)))
vdom::attr(
event_name,
AttributeValue::EventListener(EventCallback::from(f)),
)
}

/// on click event
Expand Down
2 changes: 0 additions & 2 deletions crates/core/src/dom/web_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use wasm_bindgen::JsCast;

/// a trait for implementing WebComponent in the DOM with custom tag
pub trait WebComponent<MSG> {

/// returns the attributes that is observed by this component
/// These are the names of the attributes the component is interested in
fn observed_attributes() -> Vec<&'static str>;
Expand All @@ -31,7 +30,6 @@ pub trait WebComponent<MSG> {
fn adopted_callback(&mut self);
}


thread_local!(static REGISTER_CUSTOM_ELEMENT_FUNCTION: js_sys::Function = declare_custom_element_function());

/// register using custom element define
Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/html/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use std::borrow::Cow;

pub use crate::vdom::EventCallback;
pub use crate::vdom::Style;
pub use crate::vdom::{key, replace, skip, skip_criteria};
pub use crate::{dom::Event, vdom::Attribute};
pub use attribute_macros::commons::*;
pub use attribute_macros::*;
pub use crate::vdom::{key, replace, skip, skip_criteria};

#[macro_use]
mod attribute_macros;
Expand Down Expand Up @@ -96,9 +96,9 @@ pub fn styles_flag<MSG>(
pub fn classes_flag<MSG>(
pair: impl IntoIterator<Item = (impl Into<Value>, bool)>,
) -> Attribute<MSG> {
let class_list =
pair.into_iter()
.filter_map(|(class, flag)| if flag { Some(class.into()) } else { None });
let class_list = pair
.into_iter()
.filter_map(|(class, flag)| if flag { Some(class.into()) } else { None });

classes(class_list)
}
Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub mod prelude {
pub use crate::svg::commons::*;
pub use crate::svg::special::*;
pub use crate::vdom::{
diff, Attribute, AttributeValue, Element, EventCallback, Node, NodeTrait, Patch, TreePath, Value,
diff, Attribute, AttributeValue, Element, EventCallback, Node, NodeTrait, Patch, TreePath,
Value,
};

use cfg_if::cfg_if;
Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/vdom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
//!
use crate::dom::Event;
pub use attribute::Attribute;
pub use attribute::Callback;
pub use element::Element;
pub use leaf::Leaf;
pub use node_trait::NodeTrait;
pub use attribute::Callback;

mod attribute;
mod element;
mod leaf;
mod map_msg;
mod node_trait;

pub use attribute::special::{key, replace, skip, skip_criteria};
pub(crate) use attribute::special::{KEY, REPLACE, SKIP, SKIP_CRITERIA};
pub use attribute::{
attr, attr_ns, merge_attributes_of_same_name, AttributeName,
AttributeValue, Namespace, Style, Tag, Value,
attr, attr_ns, merge_attributes_of_same_name, AttributeName, AttributeValue, Namespace, Style,
Tag, Value,
};
pub use diff::{diff, diff_recursive};
pub use node::{element, element_ns, fragment, leaf, node_list, Node};
pub use patch::{Patch, PatchType, TreePath};
pub(crate) use attribute::special::{KEY, REPLACE, SKIP, SKIP_CRITERIA};
pub use attribute::special::{key, replace, skip, skip_criteria};

pub mod diff;
mod diff_lis;
Expand Down
9 changes: 5 additions & 4 deletions crates/core/src/vdom/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ pub use value::Value;

mod attribute_value;
pub mod callback;
pub(crate) mod special;
mod style;
mod value;
pub(crate) mod special;

/// The type of the Namspace
pub type Namespace = &'static str;
Expand Down Expand Up @@ -81,7 +81,10 @@ impl<MSG> Attribute<MSG> {

/// returns true if this attribute is an event listener
pub fn is_event_listener(&self) -> bool {
self.value.first().map(|v|v.is_event_listener()).unwrap_or(false)
self.value
.first()
.map(|v| v.is_event_listener())
.unwrap_or(false)
}
}

Expand Down Expand Up @@ -151,5 +154,3 @@ pub fn group_attributes_per_name<MSG>(
}
grouped
}


3 changes: 0 additions & 3 deletions crates/core/src/vdom/attribute/special.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::vdom::AttributeName;
/// Special Node attributes that are treated differently
/// such as key and skip which both greatly affects the diffing algorithm

/// The key attribute
pub static KEY: &AttributeName = &"key";

Expand All @@ -20,8 +19,6 @@ pub static SKIP: &AttributeName = &"skip";
/// The skip criteria attribute
pub static SKIP_CRITERIA: &AttributeName = &"skip_criteria";



/// creates a key attribute using a formatter
/// # Examples
/// ```rust
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/vdom/attribute/value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt;
use std::borrow::Cow;
use std::fmt;

/// Wraps different primitive variants used as values in html
/// This is needed since html attributes can have different value types
Expand Down
22 changes: 9 additions & 13 deletions crates/core/src/vdom/leaf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Leaf node for html dom tree
use std::borrow::Cow;
use crate::dom::StatefulComponent;
use std::borrow::Cow;
use std::fmt;

/// A leaf node value of html dom tree
Expand All @@ -18,10 +18,9 @@ pub enum Leaf<MSG> {
Component(Box<dyn StatefulComponent<MSG>>),
}

impl<MSG> Clone for Leaf<MSG>{

impl<MSG> Clone for Leaf<MSG> {
fn clone(&self) -> Self {
match self{
match self {
Self::Text(v) => Self::Text(v.clone()),
Self::SafeHtml(v) => Self::SafeHtml(v.clone()),
Self::Comment(v) => Self::Comment(v.clone()),
Expand All @@ -31,24 +30,21 @@ impl<MSG> Clone for Leaf<MSG>{
}
}

impl<MSG> fmt::Debug for Leaf<MSG>{

impl<MSG> fmt::Debug for Leaf<MSG> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self{
Self::Text(v) => write!(f, "Text({v})"),
match self {
Self::Text(v) => write!(f, "Text({v})"),
Self::SafeHtml(v) => write!(f, "SafeHtml({v})"),
Self::Comment(v) => write!(f, "Comment({v})"),
Self::DocType(v) => write!(f, "DocType({v}"),
Self::Component(_v) => todo!(),
}
}

}

impl<MSG> PartialEq for Leaf<MSG>{

impl<MSG> PartialEq for Leaf<MSG> {
fn eq(&self, other: &Self) -> bool {
match (self, other){
match (self, other) {
(Self::Text(v), Self::Text(o)) => v == o,
(Self::SafeHtml(v), Self::SafeHtml(o)) => v == o,
(Self::Comment(v), Self::Comment(o)) => v == o,
Expand All @@ -59,7 +55,7 @@ impl<MSG> PartialEq for Leaf<MSG>{
}
}

impl<MSG> Eq for Leaf<MSG>{ }
impl<MSG> Eq for Leaf<MSG> {}

impl<MSG> Leaf<MSG> {
/// returns true if this a text node
Expand Down
5 changes: 2 additions & 3 deletions crates/core/src/vdom/map_msg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::vdom::{Attribute, AttributeValue, Element, EventCallback, Node, Leaf};
use crate::vdom::{Attribute, AttributeValue, Element, EventCallback, Leaf, Node};

impl<MSG> Node<MSG> {
/// map the msg of this node such that Node<MSG> becomes Node<MSG2>
Expand Down Expand Up @@ -115,8 +115,7 @@ impl<MSG> AttributeValue<MSG> {
}
}

impl<MSG> Leaf<MSG>{

impl<MSG> Leaf<MSG> {
/// mape the msg of this Leaf such that `Leaf<MSG>` becomes `Leaf<MSG2>`
pub fn map_msg<F, MSG2>(self, _cb: F) -> Leaf<MSG2>
where
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/vdom/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use crate::vdom::Attribute;
use crate::vdom::AttributeValue;
use crate::vdom::Element;
use crate::vdom::Leaf;
use crate::vdom::Value;
use derive_where::derive_where;
use std::fmt;
use std::fmt::{Debug, Formatter};
use crate::vdom::Value;

/// represents a node in a virtual dom
/// A node could be an element which can contain one or more children of nodes.
Expand Down
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ pub use prelude::*;
pub mod prelude {
pub use sauron_core::prelude::*;
pub use sauron_core::*;
#[cfg(feature = "with-jss")]
pub use sauron_macro::{jss, jss_with_media, style};
#[cfg(feature = "with-node-macro")]
pub use sauron_macro::node;
#[cfg(feature = "custom_element")]
pub use sauron_macro::custom_element;
#[cfg(feature = "with-node-macro")]
pub use sauron_macro::node;
#[cfg(feature = "with-jss")]
pub use sauron_macro::{jss, jss_with_media, style};
}


5 changes: 4 additions & 1 deletion tests/test_fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ impl Application<()> for SimpleComponent {
}

fn view(&self) -> Node<()> {
div(vec![sauron::on_click(|_|{}), sauron::on_click(|_|{})], vec![])
div(
vec![sauron::on_click(|_| {}), sauron::on_click(|_| {})],
vec![],
)
}
}

Expand Down

0 comments on commit 09faeb6

Please sign in to comment.