mirror of
https://github.com/mii443/encrypt.git
synced 2025-08-22 15:05:33 +00:00
fix type system and add write_value,read_value
This commit is contained in:
@ -1,7 +1,5 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
let t: Vec<String> = vec("");
|
let users: Vec<String> = vec("");
|
||||||
t[0] = "test";
|
let tmp: Vec<eep> = vec(encrypt(0));
|
||||||
t = push(t, "test2");
|
let polls: Vec<Vec<eep>> = vec(tmp);
|
||||||
println(decrypt(a + b));
|
|
||||||
println(t[0] + t[1]);
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use primitive_types::U512;
|
use primitive_types::U512;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::io::{stdout, Write};
|
use std::{io::{stdout, Write, Read}, fs};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
elliptic_curve::{elliptic_curve::EllipticCurvePoint, encryption::Encryption},
|
elliptic_curve::{elliptic_curve::EllipticCurvePoint, encryption::Encryption},
|
||||||
@ -40,6 +40,34 @@ 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 {
|
||||||
|
"read_value" => {
|
||||||
|
let file_name = args[0].clone();
|
||||||
|
let mut file = fs::File::open(file_name.extract_text().unwrap()).unwrap();
|
||||||
|
let mut contents = String::default();
|
||||||
|
file.read_to_string(&mut contents).unwrap();
|
||||||
|
ExternalFuncReturn {
|
||||||
|
status: ExternalFuncStatus::SUCCESS,
|
||||||
|
value: Some(serde_json::from_str(&contents).unwrap()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"write_value" => {
|
||||||
|
if !accept.contains(&Permission::FileWrite) || reject.contains(&Permission::FileWrite) {
|
||||||
|
return ExternalFuncReturn {
|
||||||
|
status: ExternalFuncStatus::REJECTED,
|
||||||
|
value: None,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
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(serde_json::to_string(&content).unwrap().as_bytes())
|
||||||
|
.unwrap();
|
||||||
|
ExternalFuncReturn {
|
||||||
|
status: ExternalFuncStatus::SUCCESS,
|
||||||
|
value: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
"write" => {
|
"write" => {
|
||||||
if !accept.contains(&Permission::FileWrite) || reject.contains(&Permission::FileWrite) {
|
if !accept.contains(&Permission::FileWrite) || reject.contains(&Permission::FileWrite) {
|
||||||
return ExternalFuncReturn {
|
return ExternalFuncReturn {
|
||||||
@ -106,6 +134,7 @@ pub const STD_FUNC: fn(
|
|||||||
if arg.get_type() == typ.to_str() {
|
if arg.get_type() == typ.to_str() {
|
||||||
vec.push(arg);
|
vec.push(arg);
|
||||||
} else {
|
} else {
|
||||||
|
println!("{} != {}", arg.get_type(), typ.to_str());
|
||||||
return ExternalFuncReturn {
|
return ExternalFuncReturn {
|
||||||
status: ExternalFuncStatus::ERROR,
|
status: ExternalFuncStatus::ERROR,
|
||||||
value: None,
|
value: None,
|
||||||
|
@ -21,12 +21,43 @@ impl GPSLType {
|
|||||||
let children: Vec<String> = self.child.iter().map(|c| c.to_str()).collect();
|
let children: Vec<String> = self.child.iter().map(|c| c.to_str()).collect();
|
||||||
if children.len() > 0 {
|
if children.len() > 0 {
|
||||||
s.push_str("<");
|
s.push_str("<");
|
||||||
s.push_str(&children.join(","));
|
s.push_str(&children.join(""));
|
||||||
s.push_str(">");
|
s.push_str(">");
|
||||||
}
|
}
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_str(s: &str) -> Result<GPSLType, String> {
|
||||||
|
let mut typ = GPSLType {
|
||||||
|
type_str: String::new(),
|
||||||
|
child: Vec::new(),
|
||||||
|
};
|
||||||
|
let mut main = String::new();
|
||||||
|
let mut read_count = 0;
|
||||||
|
for x in s.chars() {
|
||||||
|
if !x.is_alphabetic() && x != '<' && x != '>' && x != ',' {
|
||||||
|
return Err(format!("Invalid character: {}", x));
|
||||||
|
}
|
||||||
|
if x != '<' && x != '>' && x != ',' {
|
||||||
|
main.push(x);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
read_count += 1;
|
||||||
|
}
|
||||||
|
typ.type_str = main.clone();
|
||||||
|
|
||||||
|
if read_count != s.len() {
|
||||||
|
let mut child = s[read_count..].to_string();
|
||||||
|
child.pop().unwrap();
|
||||||
|
child.remove(0);
|
||||||
|
|
||||||
|
typ.child.push(GPSLType::from_str(&child)?);
|
||||||
|
}
|
||||||
|
Ok(typ)
|
||||||
|
}
|
||||||
|
/*
|
||||||
pub fn from_str(s: &str) -> Result<GPSLType, String> {
|
pub fn from_str(s: &str) -> Result<GPSLType, String> {
|
||||||
let mut type_str = String::new();
|
let mut type_str = String::new();
|
||||||
let mut child = Vec::new();
|
let mut child = Vec::new();
|
||||||
@ -64,7 +95,7 @@ impl GPSLType {
|
|||||||
}
|
}
|
||||||
Ok(GPSLType { type_str, child })
|
Ok(GPSLType { type_str, child })
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
pub fn is_correct(&self) -> bool {
|
pub fn is_correct(&self) -> bool {
|
||||||
if self.type_str == "U512"
|
if self.type_str == "U512"
|
||||||
|| self.type_str == "num"
|
|| self.type_str == "num"
|
||||||
@ -85,3 +116,14 @@ impl GPSLType {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::gpsl::gpsl_type::GPSLType;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn to_str_test() {
|
||||||
|
let typ = GPSLType::from_str("Vec<eep>").unwrap();
|
||||||
|
assert_eq!(typ, GPSLType::from_str(&typ.to_str()).unwrap());
|
||||||
|
}
|
||||||
|
}
|
1
test.gvalue
Normal file
1
test.gvalue
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"Vec":{"value":[{"Text":{"value":"test"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}},{"Text":{"value":"test2"}}],"gpsl_type":{"type_str":"Vec","child":[{"type_str":"String","child":[]}]}}}
|
Reference in New Issue
Block a user