diff --git a/kclvm/runtime/src/file/mod.rs b/kclvm/runtime/src/file/mod.rs index 424eaf70e..59a4c06ca 100644 --- a/kclvm/runtime/src/file/mod.rs +++ b/kclvm/runtime/src/file/mod.rs @@ -4,8 +4,8 @@ use std::{fs, io::ErrorKind}; use crate::*; use glob::glob; -use std::path::Path; use std::io::Write; +use std::path::Path; #[no_mangle] #[runtime_fn] @@ -284,13 +284,14 @@ pub extern "C" fn kclvm_file_write( if let Some(path) = get_call_arg_str(args, kwargs, 0, Some("filepath")) { if let Some(content) = get_call_arg_str(args, kwargs, 1, Some("content")) { - if let Ok(mut file) = fs::File::create(&path) { - if let Err(e) = file.write_all(content.as_bytes()) { - panic!("Failed to write to '{}': {}", path, e); + match fs::File::create(&path) { + Ok(mut file) => { + if let Err(e) = file.write_all(content.as_bytes()) { + panic!("Failed to write to '{}': {}", path, e); + } + return ValueRef::none().into_raw(ctx); } - return ValueRef::none().into_raw(ctx); - } else if let Err(e) = fs::File::create(&path) { - panic!("Failed to create file '{}': {}", path, e); + Err(e) => panic!("Failed to create file '{}': {}", path, e), } } else { panic!("write() missing 'content' argument"); @@ -298,4 +299,4 @@ pub extern "C" fn kclvm_file_write( } else { panic!("write() missing 'filepath' argument"); } -} \ No newline at end of file +}