Skip to content

Commit a5d9c17

Browse files
authored
Refactor Value/ValueType methods into built-in traits (#18)
* Make Value and ValueTypes conversions into generic functions/traits * Make Value accessors into a generic function with trait * Rustfmt
1 parent 623d007 commit a5d9c17

17 files changed

+436
-379
lines changed

lib/src/json/json_read.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ pub fn jtoken_to_runtime_object(
9494
"Failed to convert token to runtime RTObject: {}",
9595
token
9696
))),
97-
serde_json::Value::Bool(value) => Ok(Rc::new(Value::new_bool(value.to_owned()))),
97+
serde_json::Value::Bool(value) => Ok(Rc::new(Value::new::<bool>(value.to_owned()))),
9898
serde_json::Value::Number(_) => {
9999
if token.is_i64() {
100100
let val: i32 = token.as_i64().unwrap().try_into().unwrap();
101-
Ok(Rc::new(Value::new_int(val)))
101+
Ok(Rc::new(Value::new::<i32>(val)))
102102
} else {
103103
let val: f32 = token.as_f64().unwrap() as f32;
104-
Ok(Rc::new(Value::new_float(val)))
104+
Ok(Rc::new(Value::new::<f32>(val)))
105105
}
106106
}
107107

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

119119
// Glue
@@ -153,7 +153,7 @@ pub fn jtoken_to_runtime_object(
153153
let prop_value = obj.get("^->");
154154

155155
if let Some(prop_value) = prop_value {
156-
return Ok(Rc::new(Value::new_divert_target(
156+
return Ok(Rc::new(Value::new::<Path>(
157157
Path::new_with_components_string(prop_value.as_str()),
158158
)));
159159
}
@@ -336,7 +336,7 @@ pub fn jtoken_to_runtime_object(
336336
raw_list.items.insert(item, v.as_i64().unwrap() as i32);
337337
}
338338

339-
return Ok(Rc::new(Value::new_list(raw_list)));
339+
return Ok(Rc::new(Value::new::<InkList>(raw_list)));
340340
}
341341

342342
// Used when serialising save state only

lib/src/json/json_read_stream.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ fn jtoken_to_runtime_object(
123123
) -> Result<ArrayElement, StoryError> {
124124
match value {
125125
JsonValue::Null => Ok(ArrayElement::NullElement),
126-
JsonValue::Boolean(value) => Ok(ArrayElement::RTObject(Rc::new(Value::new_bool(value)))),
126+
JsonValue::Boolean(value) => Ok(ArrayElement::RTObject(Rc::new(Value::new::<bool>(value)))),
127127
JsonValue::Number(value) => {
128128
if value.is_integer() {
129129
let val: i32 = value.as_integer().unwrap();
130-
Ok(ArrayElement::RTObject(Rc::new(Value::new_int(val))))
130+
Ok(ArrayElement::RTObject(Rc::new(Value::new::<i32>(val))))
131131
} else {
132132
let val: f32 = value.as_float().unwrap();
133-
Ok(ArrayElement::RTObject(Rc::new(Value::new_float(val))))
133+
Ok(ArrayElement::RTObject(Rc::new(Value::new::<f32>(val))))
134134
}
135135
}
136136
JsonValue::String(value) => {
@@ -139,11 +139,11 @@ fn jtoken_to_runtime_object(
139139
// String value
140140
let first_char = str.chars().next().unwrap();
141141
if first_char == '^' {
142-
return Ok(ArrayElement::RTObject(Rc::new(Value::new_string(
142+
return Ok(ArrayElement::RTObject(Rc::new(Value::new::<&str>(
143143
&str[1..],
144144
))));
145145
} else if first_char == '\n' && str.len() == 1 {
146-
return Ok(ArrayElement::RTObject(Rc::new(Value::new_string("\n"))));
146+
return Ok(ArrayElement::RTObject(Rc::new(Value::new::<&str>("\n"))));
147147
}
148148

149149
// Glue
@@ -185,7 +185,7 @@ fn jtoken_to_runtime_object(
185185
// Divert target value to path
186186
if prop == "^->" {
187187
tok.expect('}')?;
188-
return Ok(ArrayElement::RTObject(Rc::new(Value::new_divert_target(
188+
return Ok(ArrayElement::RTObject(Rc::new(Value::new::<Path>(
189189
Path::new_with_components_string(prop_value.as_str()),
190190
))));
191191
}
@@ -375,7 +375,9 @@ fn jtoken_to_runtime_object(
375375
}
376376

377377
tok.expect('}')?;
378-
return Ok(ArrayElement::RTObject(Rc::new(Value::new_list(raw_list))));
378+
return Ok(ArrayElement::RTObject(Rc::new(Value::new::<InkList>(
379+
raw_list,
380+
))));
379381
}
380382

381383
// Used when serialising save state only

lib/src/json/json_write.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@ use std::{collections::HashMap, rc::Rc};
33
use serde_json::{json, Map};
44

55
use crate::{
6-
choice::Choice, choice_point::ChoicePoint, container::Container,
7-
control_command::ControlCommand, divert::Divert, glue::Glue, ink_list::InkList,
8-
native_function_call::NativeFunctionCall, object::RTObject, push_pop::PushPopType,
9-
story_error::StoryError, tag::Tag, value::Value, variable_assigment::VariableAssignment,
10-
variable_reference::VariableReference, void::Void,
6+
choice::Choice,
7+
choice_point::ChoicePoint,
8+
container::Container,
9+
control_command::ControlCommand,
10+
divert::Divert,
11+
glue::Glue,
12+
ink_list::InkList,
13+
native_function_call::NativeFunctionCall,
14+
object::RTObject,
15+
path::Path,
16+
push_pop::PushPopType,
17+
story_error::StoryError,
18+
tag::Tag,
19+
value::Value,
20+
value_type::{StringValue, VariablePointerValue},
21+
variable_assigment::VariableAssignment,
22+
variable_reference::VariableReference,
23+
void::Void,
1124
};
1225

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

82-
if let Some(v) = Value::get_int_value(o.as_ref()) {
95+
if let Some(v) = Value::get_value::<i32>(o.as_ref()) {
8396
return Ok(json!(v));
8497
}
8598

86-
if let Some(v) = Value::get_float_value(o.as_ref()) {
99+
if let Some(v) = Value::get_value::<f32>(o.as_ref()) {
87100
return Ok(json!(v));
88101
}
89102

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

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

103-
if let Some(v) = Value::get_list_value(o.as_ref()) {
116+
if let Some(v) = Value::get_value::<&InkList>(o.as_ref()) {
104117
return Ok(write_ink_list(v));
105118
}
106119

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

113-
if let Some(v) = Value::get_variable_pointer_value(o.as_ref()) {
126+
if let Some(v) = Value::get_value::<&VariablePointerValue>(o.as_ref()) {
114127
let mut jobj: Map<String, serde_json::Value> = Map::new();
115128
jobj.insert("^var".to_owned(), json!(v.variable_name));
116129
jobj.insert("ci".to_owned(), json!(v.context_index));

lib/src/list_definitions_origin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl ListDefinitionsOrigin {
2424
let mut l = InkList::new();
2525
l.items.insert(key.clone(), *val);
2626

27-
let list_value = Rc::new(Value::new_list(l));
27+
let list_value = Rc::new(Value::new::<InkList>(l));
2828

2929
list_definitions_origin
3030
.all_unambiguous_list_value_cache

0 commit comments

Comments
 (0)