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

Add new String type wrapper named StringBox #63

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion pax-compiler/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,10 @@ fn resolve_symbol_as_invocation(
ExpressionSpecInvocation {
root_identifier,
is_numeric: ExpressionSpecInvocation::is_numeric(&property_properties_coproduct_type),
is_primitive_nonnumeric: ExpressionSpecInvocation::is_primitive_nonnumeric(
is_bool: ExpressionSpecInvocation::is_primitive_bool(
&property_properties_coproduct_type,
),
is_string: ExpressionSpecInvocation::is_primitive_string(
&property_properties_coproduct_type,
),
escaped_identifier,
Expand Down
1 change: 1 addition & 0 deletions pax-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ fn generate_and_overwrite_properties_coproduct(
("Rotation", "pax_runtime_api::Rotation"),
("SizePixels", "pax_runtime_api::SizePixels"),
("Numeric", "pax_runtime_api::Numeric"),
("StringBox", "pax_runtime_api::StringBox"),
];

TYPES_COPRODUCT_BUILT_INS.iter().for_each(|builtin| {
Expand Down
11 changes: 8 additions & 3 deletions pax-compiler/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ pub struct ExpressionSpecInvocation {

/// Flags used for particular corner cases of `Repeat` codegen
pub is_numeric: bool,
pub is_primitive_nonnumeric: bool,
pub is_bool: bool,
pub is_string: bool,

/// Flags describing attributes of properties
pub property_flags: PropertyDefinitionFlags,
Expand All @@ -110,8 +111,12 @@ pub const SUPPORTED_NUMERIC_PRIMITIVES: [&str; 13] = [
pub const SUPPORTED_NONNUMERIC_PRIMITIVES: [&str; 2] = ["String", "bool"];

impl ExpressionSpecInvocation {
pub fn is_primitive_nonnumeric(property_properties_coproduct_type: &str) -> bool {
SUPPORTED_NONNUMERIC_PRIMITIVES.contains(&property_properties_coproduct_type)
pub fn is_primitive_string(property_properties_coproduct_type: &str) -> bool {
&SUPPORTED_NONNUMERIC_PRIMITIVES[0] == &property_properties_coproduct_type
}

pub fn is_primitive_bool(property_properties_coproduct_type: &str) -> bool {
&SUPPORTED_NONNUMERIC_PRIMITIVES[1] == &property_properties_coproduct_type
}

pub fn is_numeric(property_properties_coproduct_type: &str) -> bool {
Expand Down
12 changes: 10 additions & 2 deletions pax-compiler/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ fn recurse_pratt_parse_to_string<'a>(
format!("Numeric::from({})", value)
},
Rule::string => {
//TODO: figure out string concatenation. Might need to introduce another operator? Or perhaps a higher-level string type, which supports addition-as-concatenation — like we do with Numeric
literal_kind.as_str().to_string() + ".to_string()"
format!("StringBox::from({})",literal_kind.as_str().to_string())
},
_ => {
/* {literal_enum_value | literal_tuple_access | literal_tuple | string } */
Expand Down Expand Up @@ -1309,6 +1308,15 @@ impl Reflectable for pax_runtime_api::Transform2D {
}
}

impl Reflectable for pax_runtime_api::StringBox {
fn get_import_path() -> String {
"pax_lang::api::StringBox".to_string()
}
fn get_self_pascal_identifier() -> String {
"StringBox".to_string()
}
}

impl<T: Reflectable> Reflectable for std::vec::Vec<T> {
fn parse_to_manifest(mut ctx: ParsingContext) -> (ParsingContext, Vec<PropertyDefinition>) {
let type_id = Self::get_type_id();
Expand Down
9 changes: 7 additions & 2 deletions pax-compiler/templates/cartridge-lib.tera
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ pub fn instantiate_expression_table<R: 'static + RenderContext>() -> HashMap<usi
{# For known numeric primitives, we can safely deref-copy and wrap in Numeric #}
let unwrapped = if let PropertiesCoproduct::{{invocation.iterable_type_id_escaped}}(i) = **elem {i} else {unreachable!()};
Numeric::from(unwrapped)
{% elif invocation.is_primitive_nonnumeric %}
//iterable primitive non-numeric
{% elif invocation.is_string %}
let unwrapped = if let PropertiesCoproduct::{{invocation.iterable_type_id_escaped}}(i) = **elem {i} else {unreachable!()};
StringBox::from(unwrapped)
{% elif invocation.is_bool %}
elem.clone()
{% else %}
//iterable complex type
Expand All @@ -53,6 +55,9 @@ pub fn instantiate_expression_table<R: 'static + RenderContext>() -> HashMap<usi
{% if invocation.is_numeric %}
//binding simple numeric property
Numeric::from(p.{{invocation.root_identifier}}.get())
{% elif invocation.is_string %}
//binding simple stringbox property
StringBox::from(p.{{invocation.root_identifier}}.get())
{% else %}
//binding cloneable property
p.{{invocation.root_identifier}}.get().clone()
Expand Down
1 change: 1 addition & 0 deletions pax-properties-coproduct/src/lib.gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum TypesCoproduct {
stdCOCOvecCOCOVecLABRstdCOCOrcCOCORcLABRPropertiesCoproductRABRRABR(Vec<Rc<PropertiesCoproduct>>),
String(String),
//generated
StringBox(pax_example::pax_reexports::pax_std::types::StringBox),
Transform2D(pax_example::pax_reexports::Transform2D),
SizePixels(pax_example::pax_reexports::SizePixels),
Stroke(pax_example::pax_reexports::pax_std::types::Stroke),
Expand Down
1 change: 1 addition & 0 deletions pax-properties-coproduct/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum TypesCoproduct {
Size(pax_runtime_api::Size),
Rotation(pax_runtime_api::Rotation),
Numeric(pax_runtime_api::Numeric),
StringBox(pax_runtime_api::StringBox)
//generated / userland
}

Expand Down
44 changes: 44 additions & 0 deletions pax-runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,3 +1066,47 @@ impl ZIndex {
}
}
}

#[derive(Clone,Debug)]
pub struct StringBox{
pub string: String,
}

impl Add for StringBox{
type Output = Self;

fn add(mut self, rhs: Self) -> Self::Output {
self.string.push_str(&rhs.string.as_str());
self
}
}

impl Default for StringBox{
fn default() -> Self {
Self { string: String::new() }
}
}

impl From<&str> for StringBox{
fn from(value: &str) -> Self {
StringBox { string: value.to_string() }
}
}

impl From<String> for StringBox{
fn from(value: String) -> Self {
StringBox{string:value}
}
}

impl From<&String> for StringBox{
fn from(value: &String) -> Self {
StringBox{string:value.to_string()}
}
}

impl From<StringBox> for String{
fn from(value: StringBox) -> Self {
String::from(value.string)
}
}
8 changes: 4 additions & 4 deletions pax-std/pax-std-primitives/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<R: 'static + RenderContext> RenderNode<R> for ImageInstance<R> {
} else {
unreachable!()
};
properties.path.set(new_value);
properties.path.set(pax_runtime_api::StringBox { string: new_value });
}

self.common_properties.compute_properties(rtc);
Expand Down Expand Up @@ -97,12 +97,12 @@ impl<R: 'static + RenderContext> RenderNode<R> for ImageInstance<R> {
let properties = &mut *self.properties.as_ref().borrow_mut();
let val = properties.path.get();
let is_new_value = match &last_patch.path {
Some(cached_value) => !val.eq(cached_value),
Some(cached_value) => !val.string.eq(cached_value),
None => true,
};
if is_new_value {
new_message.path = Some(val.clone());
last_patch.path = Some(val.clone());
new_message.path = Some(val.string.clone());
last_patch.path = Some(val.string.clone());
has_any_updates = true;
}

Expand Down
7 changes: 3 additions & 4 deletions pax-std/pax-std-primitives/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use pax_core::{
RenderNodePtr, RenderNodePtrList, RenderTreeContext,
};
use pax_message::{AnyCreatePatch, TextPatch};
use pax_runtime_api::{CommonProperties, Layer, SizePixels};
use pax_runtime_api::{CommonProperties, Layer, SizePixels, StringBox};
use pax_std::primitives::Text;
use piet::RenderContext;
use std::collections::HashMap;
Expand Down Expand Up @@ -61,11 +61,10 @@ impl<R: 'static + RenderContext> RenderNode<R> for TextInstance<R> {
Rc::new(RefCell::new(vec![]))
}
fn compute_properties(&mut self, rtc: &mut RenderTreeContext<R>) {

let properties = &mut *self.properties.as_ref().borrow_mut();

if let Some(text) = rtc.compute_vtable_value(properties.text._get_vtable_id()) {
let new_value = unsafe_unwrap!(text, TypesCoproduct, String);
let new_value = unsafe_unwrap!(text, TypesCoproduct, StringBox);
properties.text.set(new_value);
}

Expand Down Expand Up @@ -197,7 +196,7 @@ impl<R: 'static + RenderContext> RenderNode<R> for TextInstance<R> {

let properties = &mut *self.properties.as_ref().borrow_mut();

let val = properties.text.get();
let val = properties.text.get().string.clone();
let is_new_value = match &last_patch.content {
Some(cached_value) => !val.eq(cached_value),
None => true,
Expand Down
5 changes: 3 additions & 2 deletions pax-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod components {
pub mod primitives {
use pax_lang::Pax;
use pax_runtime_api::Size;
use pax_runtime_api::StringBox;

use crate::types::text::TextStyle;
use crate::types::PathSegment;
Expand Down Expand Up @@ -63,7 +64,7 @@ pub mod primitives {
#[custom(Imports)]
#[primitive("pax_std_primitives::text::TextInstance")]
pub struct Text {
pub text: pax_lang::Property<String>,
pub text: pax_lang::Property<StringBox>,
pub style: pax_lang::Property<TextStyle>,
pub style_link: pax_lang::Property<TextStyle>,
}
Expand All @@ -72,6 +73,6 @@ pub mod primitives {
#[custom(Imports)]
#[primitive("pax_std_primitives::image::ImageInstance")]
pub struct Image {
pub path: pax_lang::Property<String>,
pub path: pax_lang::Property<StringBox>,
}
}
39 changes: 20 additions & 19 deletions pax-std/src/types/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::types::Color;
use api::StringBox;
use pax_lang::api::{Numeric, Property, PropertyLiteral, SizePixels};
use pax_lang::*;
use pax_message::{
Expand Down Expand Up @@ -126,15 +127,15 @@ impl Default for Font {
#[derive(Pax)]
#[custom(Imports, Default)]
pub struct SystemFont {
pub family: String,
pub family: StringBox,
pub style: FontStyle,
pub weight: FontWeight,
}

impl Default for SystemFont {
fn default() -> Self {
Self {
family: "Arial".to_string(),
family: "Arial".to_string().into(),
style: FontStyle::Italic,
weight: FontWeight::Bold,
}
Expand All @@ -144,17 +145,17 @@ impl Default for SystemFont {
#[derive(Pax)]
#[custom(Imports)]
pub struct WebFont {
pub family: String,
pub url: String,
pub family: StringBox,
pub url: StringBox,
pub style: FontStyle,
pub weight: FontWeight,
}

#[derive(Pax)]
#[custom(Imports)]
pub struct LocalFont {
pub family: String,
pub path: String,
pub family: StringBox,
pub path: StringBox,
pub style: FontStyle,
pub weight: FontWeight,
}
Expand Down Expand Up @@ -268,19 +269,19 @@ impl From<Font> for FontPatch {
fn from(font: Font) -> Self {
match font {
Font::System(system_font) => FontPatch::System(SystemFontMessage {
family: Some(system_font.family),
family: Some(system_font.family.string),
style: Some(system_font.style.into()),
weight: Some(system_font.weight.into()),
}),
Font::Web(web_font) => FontPatch::Web(WebFontMessage {
family: Some(web_font.family),
url: Some(web_font.url),
family: Some(web_font.family.string.into()),
url: Some(web_font.url.string.into()),
style: Some(web_font.style.into()),
weight: Some(web_font.weight.into()),
}),
Font::Local(local_font) => FontPatch::Local(LocalFontMessage {
family: Some(local_font.family),
path: Some(local_font.path),
family: Some(local_font.family.string),
path: Some(local_font.path.string.into()),
style: Some(local_font.style.into()),
weight: Some(local_font.weight.into()),
}),
Expand Down Expand Up @@ -323,7 +324,7 @@ impl PartialEq<FontPatch> for Font {
system_font_patch
.family
.as_ref()
.map_or(false, |family| *family == system_font.family)
.map_or(false, |family| *family == system_font.family.string)
&& system_font_patch
.style
.as_ref()
Expand All @@ -337,11 +338,11 @@ impl PartialEq<FontPatch> for Font {
web_font_patch
.family
.as_ref()
.map_or(false, |family| *family == web_font.family)
.map_or(false, |family| *family == web_font.family.string)
&& web_font_patch
.url
.as_ref()
.map_or(false, |url| *url == web_font.url)
.map_or(false, |url| *url == web_font.url.string)
&& web_font_patch
.style
.as_ref()
Expand All @@ -355,11 +356,11 @@ impl PartialEq<FontPatch> for Font {
local_font_patch
.family
.as_ref()
.map_or(false, |family| *family == local_font.family)
.map_or(false, |family| *family == local_font.family.string)
&& local_font_patch
.path
.as_ref()
.map_or(false, |path| *path == local_font.path)
.map_or(false, |path| *path == local_font.path.string)
&& local_font_patch
.style
.as_ref()
Expand All @@ -375,15 +376,15 @@ impl PartialEq<FontPatch> for Font {
}

impl Font {
pub fn system(family: String, style: FontStyle, weight: FontWeight) -> Self {
pub fn system(family: StringBox, style: FontStyle, weight: FontWeight) -> Self {
Self::System(SystemFont {
family,
style,
weight,
})
}

pub fn web(family: String, url: String, style: FontStyle, weight: FontWeight) -> Self {
pub fn web(family: StringBox, url: StringBox, style: FontStyle, weight: FontWeight) -> Self {
Self::Web(WebFont {
family,
url,
Expand All @@ -392,7 +393,7 @@ impl Font {
})
}

pub fn local(family: String, path: String, style: FontStyle, weight: FontWeight) -> Self {
pub fn local(family: StringBox, path: StringBox, style: FontStyle, weight: FontWeight) -> Self {
Self::Local(LocalFont {
family,
path,
Expand Down