| 12 | } |
| 13 | |
| 14 | pub fn copy_the_resource(source_file: &str, type_name: &str) -> Result<(), String> { |
| 15 | // open the source_file, deserialize it from JSON, change the `type` property to type_name, |
| 16 | // serialize it back to JSON and save it to a new file named "<name>.dsc.resource.json" |
| 17 | let file_content = std::fs::read_to_string(source_file) |
| 18 | .map_err(|e| format!("Failed to read source file: {}", e))?; |
| 19 | let mut resource_json: serde_json::Value = serde_json::from_str(&file_content) |
| 20 | .map_err(|e| format!("Failed to parse JSON from source file: {}", e))?; |
| 21 | if let Some(obj) = resource_json.as_object_mut() { |
| 22 | obj.insert("type".to_string(), serde_json::Value::String(type_name.to_string())); |
| 23 | } else { |
| 24 | return Err("Source file not a resource manifest".to_string()); |
| 25 | } |
| 26 | let name_part = type_name.split('/').next_back().unwrap_or(type_name); |
| 27 | let output_file = format!("{name_part}.dsc.resource.json"); |
| 28 | let output_content = serde_json::to_string_pretty(&resource_json) |
| 29 | .map_err(|e| format!("Failed to serialize JSON: {e}"))?; |
| 30 | std::fs::write(&output_file, output_content) |
| 31 | .map_err(|e| format!("Failed to write output file: {e}"))?; |
| 32 | Ok(()) |
| 33 | } |