Skip to main content

SValue

Enum SValue 

Source
pub enum SValue {
    Text(ARc<str>),
    Int(i64),
    Float(f64),
    Bool(bool),
}
Expand description

This should model a Scratch value. Scratch treats texts that are non-numeric as the number 0 and also stores numbers inside of arithmetic expressions as texts, at least sometimes.

So it is useful to have a type that mimics this implicit conversion behaviour.

Variants§

§

Text(ARc<str>)

§

Int(i64)

§

Float(f64)

§

Bool(bool)

Implementations§

Source§

impl SValue

Source§

impl SValue

Source§

impl SValue

Source

pub fn q_mul_numbers<Q>(&self, other: &SValue, sink: &mut Q) -> SValue
where Q: QuirkSink<SValueToNumberQ> + QuirkSink<SNumberToFloatQ> + QuirkSink<IntegerMulWouldFailQ>,

Source§

impl SValue

Source§

impl SValue

Source

pub fn q_eq<Q>(&self, other: &SValue, sink: &mut Q) -> bool

Compares two Scratch values for equality

If any of the two values is a non-numeric text (it can’t be parsed as float or int) or a boolean, both values will be converted to strings. Those strings will then be compared for equality.

Otherwise, the numeric equality is used as in SNumber::q_eq.

Source§

impl SValue

Source

pub fn q_and<Q>(&self, other: &SValue, sink: &mut Q) -> bool

Source

pub fn q_or<Q>(&self, other: &SValue, sink: &mut Q) -> bool

Source

pub fn q_not<Q>(&self, sink: &mut Q) -> bool

Source§

impl SValue

Source

pub fn q_lt<Q>(&self, other: &SValue, sink: &mut Q) -> bool

different unicode encodings of similar looking characters?

Source

pub fn q_gt<Q>(&self, other: &SValue, sink: &mut Q) -> bool

like Self::q_lt with swapped arguments

Source§

impl SValue

Source

pub const fn default_empty_cmp_hole() -> Self

For empty wholes in the shape of a comparison block, Scratch automatically treats its value as false

Source

pub fn default_empty_expr_hole() -> Self

For empty wholes in the shape of an expression block, Scratch automatically treats its value as the empty string

Source§

impl SValue

Source

pub fn is_float(&self) -> bool

Source

pub fn is_int(&self) -> bool

Source

pub fn is_number(&self) -> bool

Source

pub fn is_bool_but_not_as_text(&self) -> bool

Comparison operations (=, < and >) treat the texts "true" and "false" for comparisons differently than the boolean literals true and false that could be generated by <>-shaped-blocks.

(Boolean literals true and false are equal to 1 and 0 while "true" and "false" force a textual comparison so then they are not equal to 1 and 0.)

This method is used in cases where it is not allowed to parse the string literals as booleans automatically.

Source

pub fn as_bool(&self) -> bool

Source

pub fn as_text(&self) -> Cow<'_, str>

Source§

impl SValue

Source

pub fn q_as_bool<Q>(&self, sink: &mut Q) -> bool

Converts the value to a boolean according to Scratch logic.

inputassociated boolean
""false
"false"false
"true"true
Int 0 / Float 0.0false
Int 1 / Float 1.0true
other stringtrue
other numbertrue
already booleanunchanged

Occuring behaviour differences Scratch implements to avoid errors will be reported to the sink.

As expression blocks can’t be placed in comparison contexts these quriks should never occur because of a user’s program.

For completness they are reported.

Source§

impl SValue

Source

pub fn q_as_float<Q>(&self, sink: &mut Q) -> f64

Source§

impl SValue

Source

pub fn q_as_number_permissive_bool_text<Q>(&self, sink: &mut Q) -> SNumber

Source

pub fn q_as_number<Q>(&self, sink: &mut Q) -> SNumber

Source

pub fn q_as_number_strict_bool_text<Q>(&self, sink: &mut Q) -> SNumber

Converts the SValue to an SNumber

  • The boolean true will become integer 1
  • The boolean false will become integer 0
  • Any text (also the strings "true", "false") will become integer 0
  • Integers will stay as they are
  • Floats will stay as they are
Source§

impl SValue

Source

pub fn q_as_text<Q>(&self, _: &mut Q) -> Cow<'_, str>

Source§

impl SValue

Source

pub fn q_abs<Q>(&self, sink: &mut Q) -> SNumber

Source

pub fn q_modulo<Q>(&self, other: &Self, sink: &mut Q) -> SNumber

Source§

impl SValue

Source

pub fn floor<Q>(&self, sink: &mut Q) -> SNumber

Source§

impl SValue

Source

pub fn round<Q>(&self, sink: &mut Q) -> SNumber

Source§

impl SValue

Source

pub fn ceil<Q>(&self, sink: &mut Q) -> SNumber

Source§

impl SValue

Source

pub fn sqrt<Q>(&self, sink: &mut Q) -> SNumber

Source§

impl SValue

Source§

impl SValue

Source§

impl SValue

Source§

impl SValue

Source§

impl SValue

Source§

impl SValue

Source§

impl SValue

Source§

impl SValue

Source§

impl SValue

Source§

impl SValue

Source§

impl SValue

Source§

impl SValue

Source

pub fn concat_no_limit(&self, other: &SValue) -> ARc<str>

Source

pub fn concat( &self, other: &SValue, max_string_length: usize, ) -> Option<ARc<str>>

Concatenates this value (as string) with another value (as string)

Source

pub fn contains_text(&self, maybe_contained: &SValue) -> bool

Does this text contain another text?

Source

pub fn textual_length(&self) -> usize

The number of characters in the textual representation of a value

Trait Implementations§

Source§

impl Clone for SValue

Source§

fn clone(&self) -> SValue

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SValue

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for SValue

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<&'a str> for SValue

Source§

fn from(value: &'a str) -> Self

Converts to this type from the input type.
Source§

impl From<Number> for SValue

Available on crate feature serde_json only.
Source§

fn from(value: Number) -> SValue

Converts to this type from the input type.
Source§

impl From<Rc<str>> for SValue

Source§

fn from(value: ARc<str>) -> Self

Converts to this type from the input type.
Source§

impl From<SNumber> for SValue

Source§

fn from(value: SNumber) -> Self

Converts to this type from the input type.
Source§

impl From<String> for SValue

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for SValue

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for SValue

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl FromStr for SValue

Source§

type Err = Infallible

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for SValue

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialEq<&SValue> for SValue

Source§

fn eq(&self, other: &&SValue) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialEq<SValue> for &SValue

Source§

fn eq(&self, other: &SValue) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for SValue

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<Value> for SValue

Available on crate feature serde_json only.
Source§

type Error = JsonValueNoSValue

The type returned in the event of a conversion error.
Source§

fn try_from(value: Value) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.