Skip to content

Commit

Permalink
Merge pull request #71 from WorldSEnder/eager-parsing
Browse files Browse the repository at this point in the history
Implement eager parsing, getting rid of lifetime on StyleSource
  • Loading branch information
futursolo authored Apr 17, 2022
2 parents 28fdd37 + e62a13b commit fd5ed37
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 117 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## v0.11.0

### Breaking Changes:
- `StyleSource` does not take a lifetime argument
- Feature `parser`: `StyleSource` now eagerly parses its input.
- Feature `parser`: The conversion from `str` have been changed to `TryFrom`
instead of `From`. If you're using `yew`, the `IntoPropValue<StyleSource>`
impls still exist, but now panic early during conversion.

### Other Changes:
- The `Style::new_*` API is more open for accepted types of the `Css` parameter.

## v0.10.0

### Breaking Changes:
Expand Down
18 changes: 10 additions & 8 deletions examples/benchmarks/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ impl Component for Benchmarks {
}

impl YieldStyle for Benchmarks {
fn style_from(&self) -> StyleSource<'static> {
r#"
fn style_from(&self) -> StyleSource {
stylist::css!(
r#"
display: flex;
justify-content: center;
align-items: center;
Expand Down Expand Up @@ -300,8 +301,8 @@ impl YieldStyle for Benchmarks {
tbody tr:nth-child(even) {
background-color: rgb(240, 240, 240);
}
"#
.into()
"#
)
}
}

Expand Down Expand Up @@ -357,8 +358,9 @@ impl Component for App {
}

impl YieldStyle for App {
fn style_from(&self) -> StyleSource<'static> {
r#"
fn style_from(&self) -> StyleSource {
stylist::css!(
r#"
display: flex;
justify-content: center;
align-items: center;
Expand All @@ -373,8 +375,8 @@ impl YieldStyle for App {
height: 50px;
font-size: 20px;
}
"#
.into()
"#
)
}
}

Expand Down
9 changes: 5 additions & 4 deletions examples/yew-shadow/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ impl Component for App {
}

impl YieldStyle for App {
fn style_from(&self) -> StyleSource<'static> {
r#"
fn style_from(&self) -> StyleSource {
stylist::css!(
r#"
box-shadow: 0 0 5px 1px rgba(0, 0, 0, 0.7);
height: 500px;
width: 500px;
Expand All @@ -133,8 +134,8 @@ impl YieldStyle for App {
flex-direction: column;
background-color: white;
"#
.into()
"#
)
}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/stylist-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ pub enum Error {
Web(Option<wasm_bindgen::JsValue>),
}

impl From<std::convert::Infallible> for Error {
fn from(infallible: std::convert::Infallible) -> Self {
match infallible {}
}
}

pub type Result<T> = std::result::Result<T, Error>;

pub trait ResultDisplay<T> {
Expand Down
21 changes: 10 additions & 11 deletions packages/stylist/src/global_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ pub struct GlobalStyle {
impl GlobalStyle {
// The big method is monomorphic, so less code duplication and code bloat through generics
// and inlining
fn create_impl(css: StyleSource<'_>, manager: StyleManager) -> Result<Self> {
#[cfg(all(debug_assertions, feature = "parser"))]
use crate::ast::Sheet;

fn create_impl(css: StyleSource, manager: StyleManager) -> Result<Self> {
let prefix = format!("{}-global", manager.prefix());
let css = css.try_into_sheet()?;
let css = css.into_sheet();

// Creates the StyleKey, return from registry if already cached.
let key = StyleKey {
Expand All @@ -51,7 +48,7 @@ impl GlobalStyle {
// not corrupting the stylesheet.
#[cfg(all(debug_assertions, feature = "parser"))]
style_str
.parse::<Sheet>()
.parse::<crate::ast::Sheet>()
.expect_display("debug: Stylist failed to parse the style with interpolated values");

let new_style = Self {
Expand Down Expand Up @@ -83,21 +80,23 @@ impl GlobalStyle {
/// let style = Style::new("background-color: red;")?;
/// # Ok::<(), stylist::Error>(())
/// ```
pub fn new<'a, Css>(css: Css) -> Result<Self>
pub fn new<Css>(css: Css) -> Result<Self>
where
Css: Into<StyleSource<'a>>,
Css: TryInto<StyleSource>,
crate::Error: From<Css::Error>,
{
Self::new_with_manager(css, StyleManager::default())
}

/// Creates a new style using a custom manager.
pub fn new_with_manager<'a, Css, M>(css: Css, manager: M) -> Result<Self>
pub fn new_with_manager<Css, M>(css: Css, manager: M) -> Result<Self>
where
Css: Into<StyleSource<'a>>,
Css: TryInto<StyleSource>,
crate::Error: From<Css::Error>,
M: Into<StyleManager>,
{
let mgr = manager.into();
Self::create_impl(css.into(), mgr)
Self::create_impl(css.try_into()?, mgr)
}

/// Get the parsed and generated style in `&str`.
Expand Down
2 changes: 1 addition & 1 deletion packages/stylist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
//! pub struct Component;
//!
//! impl YieldStyle for Component {
//! fn style_from(&self) -> StyleSource<'static> {
//! fn style_from(&self) -> StyleSource {
//! css!("color: red;")
//! }
//! }
Expand Down
35 changes: 18 additions & 17 deletions packages/stylist/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,10 @@ impl Style {
// and inlining
fn create_impl(
class_prefix: Cow<'static, str>,
css: StyleSource<'_>,
css: StyleSource,
manager: StyleManager,
) -> Result<Self> {
#[cfg(all(debug_assertions, feature = "parser"))]
use crate::ast::Sheet;

let css = css.try_into_sheet()?;
let css = css.into_sheet();

// Creates the StyleKey, return from registry if already cached.
let key = StyleKey {
Expand All @@ -183,7 +180,7 @@ impl Style {
// not corrupting the stylesheet.
#[cfg(all(debug_assertions, feature = "parser"))]
style_str
.parse::<Sheet>()
.parse::<crate::ast::Sheet>()
.expect_display("debug: Stylist failed to parse the style with interpolated values");

let new_style = Self {
Expand Down Expand Up @@ -215,9 +212,10 @@ impl Style {
/// let style = Style::new("background-color: red;")?;
/// # Ok::<(), stylist::Error>(())
/// ```
pub fn new<'a, Css>(css: Css) -> Result<Self>
pub fn new<Css>(css: Css) -> Result<Self>
where
Css: Into<StyleSource<'a>>,
Css: TryInto<StyleSource>,
crate::Error: From<Css::Error>,
{
Self::create(StyleManager::default().prefix(), css)
}
Expand All @@ -232,34 +230,37 @@ impl Style {
/// let style = Style::create("my-component", "background-color: red;")?;
/// # Ok::<(), stylist::Error>(())
/// ```
pub fn create<'a, N, Css>(class_prefix: N, css: Css) -> Result<Self>
pub fn create<N, Css>(class_prefix: N, css: Css) -> Result<Self>
where
N: Into<Cow<'static, str>>,
Css: Into<StyleSource<'a>>,
Css: TryInto<StyleSource>,
crate::Error: From<Css::Error>,
{
Self::create_impl(class_prefix.into(), css.into(), StyleManager::default())
Self::create_with_manager(class_prefix, css, StyleManager::default())
}

/// Creates a new style from some parsable css with a default prefix using a custom
/// manager.
pub fn new_with_manager<'a, Css, M>(css: Css, manager: M) -> Result<Self>
pub fn new_with_manager<Css, M>(css: Css, manager: M) -> Result<Self>
where
Css: Into<StyleSource<'a>>,
Css: TryInto<StyleSource>,
crate::Error: From<Css::Error>,
M: Into<StyleManager>,
{
let mgr = manager.into();
Self::create_impl(mgr.prefix(), css.into(), mgr.clone())
Self::create_with_manager(mgr.prefix(), css, mgr.clone())
}

/// Creates a new style with a custom class prefix from some parsable css using a custom
/// manager.
pub fn create_with_manager<'a, N, Css, M>(class_prefix: N, css: Css, manager: M) -> Result<Self>
pub fn create_with_manager<N, Css, M>(class_prefix: N, css: Css, manager: M) -> Result<Self>
where
N: Into<Cow<'static, str>>,
Css: Into<StyleSource<'a>>,
Css: TryInto<StyleSource>,
crate::Error: From<Css::Error>,
M: Into<StyleManager>,
{
Self::create_impl(class_prefix.into(), css.into(), manager.into())
Self::create_impl(class_prefix.into(), css.try_into()?, manager.into())
}

/// Returns the class name for current style
Expand Down
Loading

0 comments on commit fd5ed37

Please sign in to comment.