From 00b7d4213922e061ac08732e7303ba44717bcb80 Mon Sep 17 00:00:00 2001 From: Apache Date: Fri, 25 Oct 2024 19:01:27 -0500 Subject: [PATCH] initial --- .gitignore | 1 + Cargo.lock | 7 ++++ Cargo.toml | 6 +++ LICENSE | 9 ++++ src/lib.rs | 15 +++++++ src/value.rs | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 152 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 src/lib.rs create mode 100644 src/value.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..9b100f3 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..257c5e0 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "freakyvm" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8346a10 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ffe9f45 --- /dev/null +++ b/src/lib.rs @@ -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); + } +} diff --git a/src/value.rs b/src/value.rs new file mode 100644 index 0000000..6d59348 --- /dev/null +++ b/src/value.rs @@ -0,0 +1,114 @@ +use std::collections::HashMap; + +#[derive(PartialEq, Debug)] +pub enum Value { + None, + True, + False, + Number(f64), + String(String), + Table { + arr: Vec, + map: HashMap, + } +} + +impl From for Value { + fn from(v: i32) -> Value { + Value::Number(f64::from(v)) + } +} +impl From for Value { + fn from(v: f32) -> Value { + Value::Number(f64::from(v)) + } +} +impl From for Value { + fn from(v: f64) -> Value { + Value::Number(v) + } +} +impl From 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 for Value { + fn from(v: bool) -> Value { + match v { + true => Value::True, + false => Value::False, + } + } +} + +impl> From> for Value { + fn from(v: Vec) -> Value { + let mut vec: Vec = Vec::new(); + for val in v { + vec.push(val.into()); + } + Value::Table { arr: vec, map: HashMap::new() } + } +} +impl> From> for Value { + fn from(v: HashMap) -> Value { + let mut map: HashMap = HashMap::new(); + for (k, v) in v { + map.insert(k, v.into()); + } + Value::Table { arr: Vec::new(), map} + } +} + +impl> From> for Value { + fn from(v: Option) -> Value { + match v { + Some(v) => v.into(), + None => Value::None, + } + } +} + +pub trait FromValue { + fn from_value(v: Value) -> Result where Self: Sized; +} + +impl FromValue for f64 { + fn from_value(v: Value) -> Result { + match v { + Value::Number(v) => Ok(v), + _ => Err("Value is not a number"), + } + } +} +impl FromValue for bool { + fn from_value(v: Value) -> Result { + 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 { + match v { + Value::String(v) => Ok(v), + _ => Err("Value is not a string"), + } + } +} + +impl Value { + pub fn extract(self) -> Result { + FromValue::from_value(self) + } +} \ No newline at end of file