(
_state: &mut State<'gc>,
args: Vec<Value<'gc>>,
)
| 108 | } |
| 109 | |
| 110 | fn serde_to_file<'gc>( |
| 111 | _state: &mut State<'gc>, |
| 112 | args: Vec<Value<'gc>>, |
| 113 | ) -> Result<Value<'gc>, VmError> { |
| 114 | // First extract keyword args |
| 115 | let (positional, keyword) = extract_keyword_args(&args)?; |
| 116 | |
| 117 | if positional.len() != 2 { |
| 118 | return Err(VmError::RuntimeError( |
| 119 | "to_file() requires path and value arguments".into(), |
| 120 | )); |
| 121 | } |
| 122 | |
| 123 | let path = string_arg!(&positional, 0, "to_file")?; |
| 124 | |
| 125 | let pretty = if let Some(pretty_val) = keyword.get("pretty") { |
| 126 | match pretty_val { |
| 127 | Value::Boolean(b) => *b, |
| 128 | _ => { |
| 129 | return Err(VmError::RuntimeError( |
| 130 | "pretty argument must be a boolean".into(), |
| 131 | )); |
| 132 | } |
| 133 | } |
| 134 | } else { |
| 135 | false |
| 136 | }; |
| 137 | |
| 138 | // Convert AIScript Value to JSON value |
| 139 | let json_value = to_json_value(&positional[1])?; |
| 140 | |
| 141 | // Serialize to string with appropriate formatting |
| 142 | let json_str = if pretty { |
| 143 | serde_json::to_string_pretty(&json_value) |
| 144 | } else { |
| 145 | serde_json::to_string(&json_value) |
| 146 | } |
| 147 | .map_err(|e| VmError::RuntimeError(format!("Failed to serialize to JSON: {}", e)))?; |
| 148 | |
| 149 | // Write to file |
| 150 | fs::write(path.to_str().unwrap(), json_str) |
| 151 | .map_err(|e| VmError::RuntimeError(format!("Failed to write to file: {}", e)))?; |
| 152 | |
| 153 | Ok(Value::Boolean(true)) |
| 154 | } |
| 155 | |
| 156 | // Helper function to convert AIScript Value to serde_json::Value |
| 157 | fn to_json_value(value: &Value) -> Result<serde_json::Value, VmError> { |
nothing calls this directly
no test coverage detected