add file write function

This commit is contained in:
Masato Imai
2022-08-23 11:06:47 +09:00
parent 1ffbf1a8cb
commit d5525d1215
2 changed files with 30 additions and 0 deletions

View File

@ -38,6 +38,18 @@ pub const STD_FUNC: fn(
) -> ExternalFuncReturn = |name, args, accept, reject, data| { ) -> ExternalFuncReturn = |name, args, accept, reject, data| {
let name = name.as_str(); let name = name.as_str();
match name { match name {
"write" => {
let file_name = args[0].clone();
let content = args[1].clone();
let mut file =
std::fs::File::create(file_name.extract_text().unwrap().as_str()).unwrap();
file.write_all(content.extract_text().unwrap().as_bytes())
.unwrap();
ExternalFuncReturn {
status: ExternalFuncStatus::SUCCESS,
value: None,
}
}
"length" => { "length" => {
let vec = args[0].clone(); let vec = args[0].clone();
match vec { match vec {

View File

@ -54,4 +54,22 @@ impl Variable {
_ => None, _ => None,
} }
} }
pub fn extract_text(&self) -> Option<String> {
match self {
Variable::Text { value } => Some(value.clone()),
Variable::Number { value } => Some(value.to_string()),
Variable::PureEncrypted { value } => Some(value.to_string()),
Variable::PairedEncrypted { value } => Some(value.to_string()),
Variable::U512 { value } => Some(value.to_string()),
Variable::Vec { value, .. } => {
let mut result = String::new();
for v in value {
result.push_str(&v.extract_text().unwrap());
}
Some(result)
}
_ => None,
}
}
} }