Start implementing Rust

This commit is contained in:
Fabian Stamm
2022-04-13 18:58:53 +00:00
parent 339d3006d6
commit 7b6ef3231f
13 changed files with 343 additions and 45 deletions

14
templates/Rust/Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "${NAME}"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
int-enum = "0.4.0"
serde = { version = "1.0.136", features = ["derive"] }
# These might not have to be included, since the serialization could be implementation dependent and having a dependency less is always a good thing.
# serde_json = "1.0.79"
# rmp-serde = "1.0.0"

View File

@ -4,13 +4,24 @@ export class VerificationError extends Error {
public readonly field?: string,
public readonly value?: any
) {
super("Parameter verification failed! " +(type ? "Expected " + type + "! " :"") + (field ? "At: " + field + "! " : ""));
super(
"Parameter verification failed! " +
(type ? "Expected " + type + "! " : "") +
(field ? "At: " + field + "! " : "")
);
}
}
export function apply_number(data: any) {
export function apply_int(data: any) {
data = Math.floor(Number(data));
if (Number.isNaN(data)) throw new VerificationError("int", undefined, data);
return data;
}
export function apply_float(data: any) {
data = Number(data);
if(Number.isNaN(data)) throw new VerificationError("number", undefined, data);
if (Number.isNaN(data))
throw new VerificationError("float", undefined, data);
return data;
}