initial
This commit is contained in:
commit
00b7d42139
6 changed files with 152 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/target
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "freakyvm"
|
||||||
|
version = "0.1.0"
|
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "freakyvm"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
9
LICENSE
Normal file
9
LICENSE
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2024 apache
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
15
src/lib.rs
Normal file
15
src/lib.rs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
mod value;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::value::Value;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
let int = 15;
|
||||||
|
let val: Value = int.into();
|
||||||
|
|
||||||
|
let new_val: f64 = val.extract().unwrap();
|
||||||
|
assert_eq!(new_val, 15.0);
|
||||||
|
}
|
||||||
|
}
|
114
src/value.rs
Normal file
114
src/value.rs
Normal file
|
@ -0,0 +1,114 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
|
pub enum Value {
|
||||||
|
None,
|
||||||
|
True,
|
||||||
|
False,
|
||||||
|
Number(f64),
|
||||||
|
String(String),
|
||||||
|
Table {
|
||||||
|
arr: Vec<Value>,
|
||||||
|
map: HashMap<String, Value>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<i32> for Value {
|
||||||
|
fn from(v: i32) -> Value {
|
||||||
|
Value::Number(f64::from(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<f32> for Value {
|
||||||
|
fn from(v: f32) -> Value {
|
||||||
|
Value::Number(f64::from(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<f64> for Value {
|
||||||
|
fn from(v: f64) -> Value {
|
||||||
|
Value::Number(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<String> for Value {
|
||||||
|
fn from(v: String) -> Value {
|
||||||
|
Value::String(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for Value {
|
||||||
|
fn from(v: &str) -> Value {
|
||||||
|
Value::String(v.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<bool> for Value {
|
||||||
|
fn from(v: bool) -> Value {
|
||||||
|
match v {
|
||||||
|
true => Value::True,
|
||||||
|
false => Value::False,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Into<Value>> From<Vec<T>> for Value {
|
||||||
|
fn from(v: Vec<T>) -> Value {
|
||||||
|
let mut vec: Vec<Value> = Vec::new();
|
||||||
|
for val in v {
|
||||||
|
vec.push(val.into());
|
||||||
|
}
|
||||||
|
Value::Table { arr: vec, map: HashMap::new() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T: Into<Value>> From<HashMap<String, T>> for Value {
|
||||||
|
fn from(v: HashMap<String, T>) -> Value {
|
||||||
|
let mut map: HashMap<String, Value> = HashMap::new();
|
||||||
|
for (k, v) in v {
|
||||||
|
map.insert(k, v.into());
|
||||||
|
}
|
||||||
|
Value::Table { arr: Vec::new(), map}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Into<Value>> From<Option<T>> for Value {
|
||||||
|
fn from(v: Option<T>) -> Value {
|
||||||
|
match v {
|
||||||
|
Some(v) => v.into(),
|
||||||
|
None => Value::None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait FromValue {
|
||||||
|
fn from_value(v: Value) -> Result<Self, &'static str> where Self: Sized;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromValue for f64 {
|
||||||
|
fn from_value(v: Value) -> Result<f64, &'static str> {
|
||||||
|
match v {
|
||||||
|
Value::Number(v) => Ok(v),
|
||||||
|
_ => Err("Value is not a number"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl FromValue for bool {
|
||||||
|
fn from_value(v: Value) -> Result<bool, &'static str> {
|
||||||
|
match v {
|
||||||
|
Value::True => Ok(true),
|
||||||
|
Value::False => Ok(false),
|
||||||
|
_ => Err("Value is not a boolean"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl FromValue for String {
|
||||||
|
fn from_value(v: Value) -> Result<String, &'static str> {
|
||||||
|
match v {
|
||||||
|
Value::String(v) => Ok(v),
|
||||||
|
_ => Err("Value is not a string"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Value {
|
||||||
|
pub fn extract<T: FromValue>(self) -> Result<T, &'static str> {
|
||||||
|
FromValue::from_value(self)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue