Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate parsing and core css functionality #10

Merged
merged 22 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
19114e9
sep stylist-core and parsing
WorldSEnder Aug 7, 2021
3c141a1
formatting
WorldSEnder Aug 7, 2021
0138c10
target wasm dependencies
WorldSEnder Aug 7, 2021
87d5daf
rename scopes -> sheet
WorldSEnder Aug 7, 2021
4e44d99
AST follows more closely now the CSS spec v3
WorldSEnder Aug 7, 2021
35663c2
add test cases from documentation
WorldSEnder Aug 7, 2021
6e3d67d
try_from_scopes -> new_from_sheet
WorldSEnder Aug 7, 2021
0ce6566
remove unused Arc
WorldSEnder Aug 8, 2021
da46106
shift clone out of Registry#get
WorldSEnder Aug 8, 2021
8fa22ab
use arch-specific module for internal architecture dependent stuff
WorldSEnder Aug 8, 2021
3bdc8c3
Merge remote-tracking branch 'upstream/master' into stylist
WorldSEnder Aug 8, 2021
bd53eaa
use doc instead of --cfg documenting
WorldSEnder Aug 8, 2021
fb4da59
more documentation
WorldSEnder Aug 8, 2021
ce424bd
revert to using rustdoc-args. cargo test respects doc flags and shoul…
WorldSEnder Aug 8, 2021
148958c
update examples
WorldSEnder Aug 8, 2021
c1232a9
github actions testing examples
WorldSEnder Aug 8, 2021
3651954
move style and registry back into stylist package
WorldSEnder Aug 8, 2021
3c241f1
add back documenting flag
WorldSEnder Aug 8, 2021
f18d5e8
no need for yew to be in dev-deps if style lives in stylist itself
WorldSEnder Aug 8, 2021
3d44e18
Borrow -> AsRef, get rid of TryParseCss for now, use prefix as key again
WorldSEnder Aug 9, 2021
f8b21f6
reorder constructors for better visibility, adjust doc
WorldSEnder Aug 9, 2021
18471ec
add documentation to core ast
WorldSEnder Aug 9, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.wasm32-unknown-unknown]
runner = 'wasm-bindgen-test-runner'
59 changes: 4 additions & 55 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,56 +1,5 @@
[package]
name = "stylist"
version = "0.7.0"
license = "MIT"
repository = "https://github.com/futursolo/stylist-rs"
authors = [
"Kaede Hoshiakwa <futursolo@icloud.com>",
[workspace]
members = [
"packages/stylist",
"packages/stylist-core",
]
edition = "2018"
description = "Stylist is a CSS-in-Rust styling solution for WebAssembly Applications."
keywords = [
"CSS",
"web",
"css-in-rust",
"yew"
]
categories = ["wasm", "web-programming"]
readme = "README.md"
homepage = "https://github.com/futursolo/stylist-rs"


[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
nom = "6.2.1"
once_cell = "1.8.0"
yew = { version = "0.18.0", optional = true, default-features = false, features = ["web_sys"] }
rand = { version = "0.8.4", features = ["small_rng"] }
thiserror = "1.0"
wasm-bindgen = "0.2.74"

[dev-dependencies]
log = "0.4"
env_logger = "0.9"

[dependencies.web-sys]
version = "0.3.51"
features = [
"Window",
"Document",
"Element",
"HtmlElement",
"HtmlHeadElement",
"HtmlStyleElement",
]

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.3", features = ["js"] }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.24"

[package.metadata.docs.rs]
features = ["yew"]
all-features = true
53 changes: 53 additions & 0 deletions packages/stylist-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[package]
name = "stylist-core"
version = "0.7.0"
license = "MIT"
repository = "https://github.com/futursolo/stylist-rs"
authors = [
"Kaede Hoshiakwa <futursolo@icloud.com>",
"Martin Molzer <ga65guy@mytum.de>",
]
edition = "2018"
description = "Stylist is a CSS-in-Rust styling solution for WebAssembly Applications."
keywords = [
"CSS",
"web",
"css-in-rust",
"yew"
]
categories = ["wasm", "web-programming"]
readme = "README.md"
homepage = "https://github.com/futursolo/stylist-rs"

[dependencies]
once_cell = "1.8.0"
yew = { version = "0.18.0", optional = true, default-features = false, features = ["web_sys"] }
rand = { version = "0.8.4", features = ["small_rng"] }

[dev-dependencies]
log = "0.4"
env_logger = "0.9"
wasm-bindgen-test = "0.3.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.3", features = ["js"] }
wasm-bindgen = { version = "^0.2.74" }
web-sys = { version = "0.3.51", features = [
"Window",
"Document",
"Element",
"HtmlElement",
"HtmlHeadElement",
"HtmlStyleElement",
] }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.24"

[features]
default = []
yew_integration = ["yew"]
futursolo marked this conversation as resolved.
Show resolved Hide resolved

[package.metadata.docs.rs]
features = ["yew_integration"]
all-features = true
82 changes: 48 additions & 34 deletions src/ast.rs → packages/stylist-core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
/// Structs implementing this trait should be able to turn into
/// a part of a CSS style sheet.
use std::fmt;
use std::str::FromStr;

use crate::parser::Parser;
use crate::{Error, Result};

pub(crate) trait ToCss {
fn to_css(&self, class_name: &str) -> String {
Expand All @@ -35,8 +31,20 @@ pub(crate) trait ToCss {
}

/// The top node of a style string.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Scopes(pub(crate) Vec<Scope>);
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Scopes(pub Vec<Scope>);

impl Scopes {
pub fn new() -> Self {
Self(Vec::new())
}
}

impl Default for Scopes {
fn default() -> Self {
Self::new()
}
}

impl ToCss for Scopes {
fn write_css<W: fmt::Write>(&self, w: &mut W, class_name: &str) -> fmt::Result {
Expand All @@ -49,18 +57,10 @@ impl ToCss for Scopes {
}
}

impl FromStr for Scopes {
type Err = Error;

fn from_str(s: &str) -> Result<Self> {
Parser::parse(s)
}
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Scope {
pub(crate) condition: Option<String>,
pub(crate) stylesets: Vec<ScopeContent>,
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Scope {
pub condition: Option<String>,
pub stylesets: Vec<ScopeContent>,
}
WorldSEnder marked this conversation as resolved.
Show resolved Hide resolved

impl ToCss for Scope {
Expand Down Expand Up @@ -88,8 +88,8 @@ impl ToCss for Scope {
}

/// Everything that can reside inside a scope.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum ScopeContent {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ScopeContent {
Block(Block),
Rule(Rule),
// e.g. media rules nested in support rules and vice versa
Expand All @@ -105,10 +105,10 @@ pub(crate) enum ScopeContent {
/// color: red;
/// }
/// ```
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Block {
pub(crate) condition: Option<String>,
pub(crate) style_attributes: Vec<StyleAttribute>,
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Block {
pub condition: Option<String>,
pub style_attributes: Vec<StyleAttribute>,
}

impl ToCss for Block {
Expand Down Expand Up @@ -162,10 +162,10 @@ impl ToCss for Block {
/// A simple CSS proprerty in the form of a key value pair.
///
/// E.g.: `color: red`
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct StyleAttribute {
pub(crate) key: String,
pub(crate) value: String,
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StyleAttribute {
pub key: String,
pub value: String,
}

impl ToCss for StyleAttribute {
Expand All @@ -177,10 +177,10 @@ impl ToCss for StyleAttribute {
/// A rule is everything that does not contain any properties.
///
/// An example would be `@keyframes`
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Rule {
pub(crate) condition: String,
pub(crate) content: Vec<RuleContent>,
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Rule {
pub condition: String,
pub content: Vec<RuleContent>,
}

impl ToCss for Rule {
Expand All @@ -196,8 +196,8 @@ impl ToCss for Rule {
}

/// Everything that can be inside a rule.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum RuleContent {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RuleContent {
String(String),
CurlyBraces(Vec<RuleContent>),
}
Expand Down Expand Up @@ -226,6 +226,20 @@ impl From<String> for RuleContent {
}
}

#[cfg(test)]
pub(crate) fn sample_scopes() -> Scopes {
Scopes(vec![Scope {
condition: None,
stylesets: vec![ScopeContent::Block(Block {
condition: None,
style_attributes: vec![StyleAttribute {
key: "color".to_string(),
value: "red".to_string(),
}],
})],
}])
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions packages/stylist-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![deny(clippy::all)]
#![deny(unsafe_code)]
#![deny(non_snake_case)]

pub mod ast;
#[doc(hidden)]
pub mod bindings;
#[doc(hidden)]
pub mod style;

mod registry;
mod utils;

pub use style::Style;
35 changes: 12 additions & 23 deletions src/registry.rs → packages/stylist-core/src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use once_cell::sync::Lazy;

use crate::Style;
use crate::{ast::Scopes, Style};

#[derive(Debug, Clone, PartialEq, Hash, Eq)]
pub(crate) struct StyleKey(pub Cow<'static, str>, pub Cow<'static, str>);

impl PartialEq<(&str, &str)> for StyleKey {
fn eq(&self, other: &(&str, &str)) -> bool {
&(&*self.0, &*self.1) == other
}
}
pub(crate) struct StyleKey(pub Arc<Scopes>);

static REGISTRY: Lazy<Arc<Mutex<StyleRegistry>>> = Lazy::new(|| Arc::new(Mutex::default()));

Expand Down Expand Up @@ -47,18 +40,18 @@ impl StyleRegistry {
#[cfg(test)]
mod tests {
use super::*;
use crate::Result;
use crate::ast::sample_scopes;

fn init() {
let _ = env_logger::builder().is_test(true).try_init();
}

#[test]
fn test_duplicate_style() -> Result<()> {
fn test_duplicate_style() {
init();

let style_a = Style::new(r#"color: red;"#)?;
let style_b = Style::new(r#"color: red;"#)?;
let style_a = Style::try_from_scopes(sample_scopes()).unwrap();
let style_b = Style::try_from_scopes(sample_scopes()).unwrap();

{
let reg = StyleRegistry::get_ref();
Expand All @@ -68,25 +61,23 @@ mod tests {
}

assert_eq!(style_a.get_style_str(), style_b.get_style_str());
Ok(())
}

#[test]
fn test_duplicate_style_different_prefix() -> Result<()> {
fn test_duplicate_style_different_prefix() {
init();

let style_a = Style::create("element-a", r#"color: red;"#)?;
let style_b = Style::create("element-b", r#"color: red;"#)?;
let style_a = Style::create_from_scopes("element-a", sample_scopes());
let style_b = Style::create_from_scopes("element-b", sample_scopes());

assert_ne!(style_a.get_style_str(), style_b.get_style_str());
Ok(())
assert_eq!(style_a.get_style_str(), style_b.get_style_str());
WorldSEnder marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
fn test_unregister() -> Result<()> {
fn test_unregister() {
init();

let style = Style::new(r#"color: red;"#)?;
let style = Style::try_from_scopes(sample_scopes()).unwrap();

{
let reg = REGISTRY.clone();
Expand All @@ -103,7 +94,5 @@ mod tests {

assert!(reg.styles.get(&*style.key()).is_none());
}

Ok(())
}
}
Loading