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

Refactor Value/ValueType methods into built-in traits #18

Merged
merged 3 commits into from
Dec 5, 2024
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
14 changes: 7 additions & 7 deletions lib/src/json/json_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ pub fn jtoken_to_runtime_object(
"Failed to convert token to runtime RTObject: {}",
token
))),
serde_json::Value::Bool(value) => Ok(Rc::new(Value::new_bool(value.to_owned()))),
serde_json::Value::Bool(value) => Ok(Rc::new(Value::new::<bool>(value.to_owned()))),
serde_json::Value::Number(_) => {
if token.is_i64() {
let val: i32 = token.as_i64().unwrap().try_into().unwrap();
Ok(Rc::new(Value::new_int(val)))
Ok(Rc::new(Value::new::<i32>(val)))
} else {
let val: f32 = token.as_f64().unwrap() as f32;
Ok(Rc::new(Value::new_float(val)))
Ok(Rc::new(Value::new::<f32>(val)))
}
}

Expand All @@ -111,9 +111,9 @@ pub fn jtoken_to_runtime_object(
// String value
let first_char = str.chars().next().unwrap();
if first_char == '^' {
return Ok(Rc::new(Value::new_string(&str[1..])));
return Ok(Rc::new(Value::new::<&str>(&str[1..])));
} else if first_char == '\n' && str.len() == 1 {
return Ok(Rc::new(Value::new_string("\n")));
return Ok(Rc::new(Value::new::<&str>("\n")));
}

// Glue
Expand Down Expand Up @@ -153,7 +153,7 @@ pub fn jtoken_to_runtime_object(
let prop_value = obj.get("^->");

if let Some(prop_value) = prop_value {
return Ok(Rc::new(Value::new_divert_target(
return Ok(Rc::new(Value::new::<Path>(
Path::new_with_components_string(prop_value.as_str()),
)));
}
Expand Down Expand Up @@ -336,7 +336,7 @@ pub fn jtoken_to_runtime_object(
raw_list.items.insert(item, v.as_i64().unwrap() as i32);
}

return Ok(Rc::new(Value::new_list(raw_list)));
return Ok(Rc::new(Value::new::<InkList>(raw_list)));
}

// Used when serialising save state only
Expand Down
16 changes: 9 additions & 7 deletions lib/src/json/json_read_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ fn jtoken_to_runtime_object(
) -> Result<ArrayElement, StoryError> {
match value {
JsonValue::Null => Ok(ArrayElement::NullElement),
JsonValue::Boolean(value) => Ok(ArrayElement::RTObject(Rc::new(Value::new_bool(value)))),
JsonValue::Boolean(value) => Ok(ArrayElement::RTObject(Rc::new(Value::new::<bool>(value)))),
JsonValue::Number(value) => {
if value.is_integer() {
let val: i32 = value.as_integer().unwrap();
Ok(ArrayElement::RTObject(Rc::new(Value::new_int(val))))
Ok(ArrayElement::RTObject(Rc::new(Value::new::<i32>(val))))
} else {
let val: f32 = value.as_float().unwrap();
Ok(ArrayElement::RTObject(Rc::new(Value::new_float(val))))
Ok(ArrayElement::RTObject(Rc::new(Value::new::<f32>(val))))
}
}
JsonValue::String(value) => {
Expand All @@ -139,11 +139,11 @@ fn jtoken_to_runtime_object(
// String value
let first_char = str.chars().next().unwrap();
if first_char == '^' {
return Ok(ArrayElement::RTObject(Rc::new(Value::new_string(
return Ok(ArrayElement::RTObject(Rc::new(Value::new::<&str>(
&str[1..],
))));
} else if first_char == '\n' && str.len() == 1 {
return Ok(ArrayElement::RTObject(Rc::new(Value::new_string("\n"))));
return Ok(ArrayElement::RTObject(Rc::new(Value::new::<&str>("\n"))));
}

// Glue
Expand Down Expand Up @@ -185,7 +185,7 @@ fn jtoken_to_runtime_object(
// Divert target value to path
if prop == "^->" {
tok.expect('}')?;
return Ok(ArrayElement::RTObject(Rc::new(Value::new_divert_target(
return Ok(ArrayElement::RTObject(Rc::new(Value::new::<Path>(
Path::new_with_components_string(prop_value.as_str()),
))));
}
Expand Down Expand Up @@ -375,7 +375,9 @@ fn jtoken_to_runtime_object(
}

tok.expect('}')?;
return Ok(ArrayElement::RTObject(Rc::new(Value::new_list(raw_list))));
return Ok(ArrayElement::RTObject(Rc::new(Value::new::<InkList>(
raw_list,
))));
}

// Used when serialising save state only
Expand Down
35 changes: 24 additions & 11 deletions lib/src/json/json_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@ use std::{collections::HashMap, rc::Rc};
use serde_json::{json, Map};

use crate::{
choice::Choice, choice_point::ChoicePoint, container::Container,
control_command::ControlCommand, divert::Divert, glue::Glue, ink_list::InkList,
native_function_call::NativeFunctionCall, object::RTObject, push_pop::PushPopType,
story_error::StoryError, tag::Tag, value::Value, variable_assigment::VariableAssignment,
variable_reference::VariableReference, void::Void,
choice::Choice,
choice_point::ChoicePoint,
container::Container,
control_command::ControlCommand,
divert::Divert,
glue::Glue,
ink_list::InkList,
native_function_call::NativeFunctionCall,
object::RTObject,
path::Path,
push_pop::PushPopType,
story_error::StoryError,
tag::Tag,
value::Value,
value_type::{StringValue, VariablePointerValue},
variable_assigment::VariableAssignment,
variable_reference::VariableReference,
void::Void,
};

pub fn write_dictionary_values(
Expand Down Expand Up @@ -79,15 +92,15 @@ pub fn write_rtobject(o: Rc<dyn RTObject>) -> Result<serde_json::Value, StoryErr
return Ok(json!(v));
}

if let Some(v) = Value::get_int_value(o.as_ref()) {
if let Some(v) = Value::get_value::<i32>(o.as_ref()) {
return Ok(json!(v));
}

if let Some(v) = Value::get_float_value(o.as_ref()) {
if let Some(v) = Value::get_value::<f32>(o.as_ref()) {
return Ok(json!(v));
}

if let Some(v) = Value::get_string_value(o.as_ref()) {
if let Some(v) = Value::get_value::<&StringValue>(o.as_ref()) {
let mut s = String::new();

if v.is_newline {
Expand All @@ -100,17 +113,17 @@ pub fn write_rtobject(o: Rc<dyn RTObject>) -> Result<serde_json::Value, StoryErr
return Ok(json!(s));
}

if let Some(v) = Value::get_list_value(o.as_ref()) {
if let Some(v) = Value::get_value::<&InkList>(o.as_ref()) {
return Ok(write_ink_list(v));
}

if let Some(v) = Value::get_divert_target_value(o.as_ref()) {
if let Some(v) = Value::get_value::<&Path>(o.as_ref()) {
let mut jobj: Map<String, serde_json::Value> = Map::new();
jobj.insert("^->".to_owned(), json!(v.get_components_string()));
return Ok(serde_json::Value::Object(jobj));
}

if let Some(v) = Value::get_variable_pointer_value(o.as_ref()) {
if let Some(v) = Value::get_value::<&VariablePointerValue>(o.as_ref()) {
let mut jobj: Map<String, serde_json::Value> = Map::new();
jobj.insert("^var".to_owned(), json!(v.variable_name));
jobj.insert("ci".to_owned(), json!(v.context_index));
Expand Down
2 changes: 1 addition & 1 deletion lib/src/list_definitions_origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl ListDefinitionsOrigin {
let mut l = InkList::new();
l.items.insert(key.clone(), *val);

let list_value = Rc::new(Value::new_list(l));
let list_value = Rc::new(Value::new::<InkList>(l));

list_definitions_origin
.all_unambiguous_list_value_cache
Expand Down
Loading
Loading