pub enum Object {
Numerical(f64),
Boolean(bool),
Str(String),
List(Vec<Object>),
Dictionary(HashMap<String, Object>),
None,
}Variants§
Numerical(f64)
Boolean(bool)
Str(String)
List(Vec<Object>)
Dictionary(HashMap<String, Object>)
None
Implementations§
Source§impl Object
impl Object
Sourcepub fn new<T: Into<Object>>(value: T) -> Self
pub fn new<T: Into<Object>>(value: T) -> Self
Creates a new Object from a value. This function will convert the value into an Object.
§Example
use akari::Object;
use std::collections::HashMap;
let obj = Object::new(42);
assert_eq!(obj, Object::Numerical(42.0));
let obj = Object::new("hello");
assert_eq!(obj, Object::Str("hello".to_string()));
let obj = Object::new(true);
assert_eq!(obj, Object::Boolean(true));
let obj = Object::new(vec![1, 2, 3]);
assert_eq!(obj, Object::List(vec![Object::Numerical(1.0), Object::Numerical(2.0), Object::Numerical(3.0)]));
let obj = Object::new(HashMap::from([("key", "value")]));
assert_eq!(obj, Object::Dictionary(HashMap::from([("key".to_string(), Object::Str("value".to_string()))]))); §Old grammar
use akari::Object;
use std::collections::HashMap;
let obj = Object::new(vec![Object::new(1), Object::new(2), Object::new(3)]);
assert_eq!(obj, Object::List(vec![Object::Numerical(1.0), Object::Numerical(2.0), Object::Numerical(3.0)]));
let obj = Object::new(HashMap::from([("key".to_string(), Object::Str("value".to_string()))]));
assert_eq!(obj, Object::Dictionary(HashMap::from([("key".to_string(), Object::Str("value".to_string()))]))); pub fn type_of(&self) -> String
Sourcepub fn numerical(&self) -> f64
pub fn numerical(&self) -> f64
Converts the Object into a numerical value. This function will return a numerical value based on the type of the Object. If the Object is not a number, it will return 0.0. If the object is a boolean, it will return 1.0 for true and 0.0 for false.
Sourcepub fn integer(&self) -> i64
pub fn integer(&self) -> i64
Converts the Object into an integer value. The rule is the same as numerical, but it will return an i64 value.
Sourcepub fn into_json(&self) -> String
pub fn into_json(&self) -> String
Converts the Object into a JSON string representation. This function will return a string that is a valid JSON representation of the Object.
§Example
use akari::object::Object;
let obj = Object::Dictionary(HashMap::from([
("key".to_string(), Object::Str("value".to_string())),
("number".to_string(), Object::Numerical(42.0)),
("list".to_string(), Object::List(vec![Object::Numerical(1.0), Object::Numerical(2.0), Object::Numerical(3.0)])),
]));
let json = obj.into_json();
println!(json); // Output: {"key": "value", "number": 42, "list": [1, 2, 3]} Sourcepub fn into_jsonf(&self, file_path: &str) -> Result<(), String>
pub fn into_jsonf(&self, file_path: &str) -> Result<(), String>
Converts the Object into a JSON string representation and writes it to a file. This function will return an error if the file cannot be written.
§Example
use akari::Object;
use akari::object;
// Write a JSON file at "data.json" by using into_jsonf
object!({
key: "value",
number: 42,
list: [1, 2, 3],
}).into_jsonf("data.json").expect("Failed to write JSON file"); Sourcepub fn from_json(json: &str) -> Result<Self, String>
pub fn from_json(json: &str) -> Result<Self, String>
Parses a JSON string and returns an Object. This function will return an error if the JSON is invalid or if there are extra characters after the JSON value.
§Example
use akari::object::Object;
let json = r#"{"key": "value", "number": 42, "list": [1, 2, 3]}"#;
let obj = Object::from_json(json).expect("Failed to parse JSON");
assert_eq!(obj, Object::Dictionary(HashMap::from([
("key".to_string(), Object::Str("value".to_string())),
("number".to_string(), Object::Numerical(42.0)),
("list".to_string(), Object::List(vec![Object::Numerical(1.0), Object::Numerical(2.0), Object::Numerical(3.0)])),
]))); §Errors
This function will return an error if the JSON is invalid or if there are extra characters after the JSON value.
Sourcepub fn from_jsonf(file_path: &str) -> Result<Self, String>
pub fn from_jsonf(file_path: &str) -> Result<Self, String>
Parses a JSON file and returns an Object. This function will return an error if the file cannot be read or if the JSON is invalid.
§Example
use akari::Object;
use akari::object;
use std::fs;
// Create a JSON file at "data.json"
fs::write("data.json", r#"{"key": "value", "number": 42, "list": [1, 2, 3]}"#).unwrap();
// Read the JSON file and parse it into an Object
let obj = Object::from_jsonf("data.json").expect("Failed to parse JSON file");
assert_eq!(obj, object!({
key: "value",
number: 42,
list: [1, 2, 3],
}));
// Delete the JSON file after use
fs::remove_file("data.json").unwrap(); Sourcepub fn get_path(&self, path: &str) -> Option<&Object>
pub fn get_path(&self, path: &str) -> Option<&Object>
Retrieves a value from the dictionary by path. This function will return None if the path is invalid or if the key does not exist.
§Example
use akari::object::Object;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("key".to_string(), Object::Str("value".to_string()));
let obj = Object::Dictionary(map);
let value = obj.get_path("key");
assert_eq!(value, Some(&Object::Str("value".to_string()))); Sourcepub fn get(&self, key: &str) -> Option<&Object>
pub fn get(&self, key: &str) -> Option<&Object>
Retrieves a value from the dictionary by key.
§Example
use akari::object::Object;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("key".to_string(), Object::Str("value".to_string()));
let obj = Object::Dictionary(map);
let value = obj.get("key");
assert_eq!(value, Some(&Object::Str("value".to_string()))); Sourcepub fn set(&mut self, key: String, value: Object)
pub fn set(&mut self, key: String, value: Object)
Sets a value in the dictionary by key.
§Example
use akari::object::Object;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("key".to_string(), Object::Str("value".to_string()));
let mut obj = Object::Dictionary(map);
obj.set("key".to_string(), Object::Str("new_value".to_string()));
let value = obj.get("key");
assert_eq!(value, Some(&Object::Str("new_value".to_string()))); Sourcepub fn delete(&mut self, key: &str) -> Option<Object>
pub fn delete(&mut self, key: &str) -> Option<Object>
Deletes a value from the dictionary by key.
§Example
use akari::object::Object;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("key".to_string(), Object::Str("value".to_string()));
let mut obj = Object::Dictionary(map);
let value = obj.delete("key");
assert_eq!(value, Some(Object::Str("value".to_string()))); This function will return None if the key does not exist.
Sourcepub fn idx(&self, index: usize) -> Option<&Object>
pub fn idx(&self, index: usize) -> Option<&Object>
Retrieves a value from the list by index.
§Example
use akari::object::Object;
use std::collections::HashMap;
let list = Object::List(vec![Object::Str("value1".to_string()), Object::Str("value2".to_string())]);
let value = list.idx(1);
assert_eq!(value, Some(&Object::Str("value2".to_string()))); Sourcepub fn insert(&mut self, index: usize, value: Object)
pub fn insert(&mut self, index: usize, value: Object)
Sets a value in the list by index.
§Example
use akari::object::Object;
use std::collections::HashMap;
let mut list = Object::List(vec![Object::Str("value1".to_string()), Object::Str("value2".to_string())]);
list.insert(1, Object::Str("new_value".to_string()));
let value = list.idx(1);
assert_eq!(value, Some(&Object::Str("new_value".to_string()))); This function will push the value to the end of the list if the index is out of bounds.
Sourcepub fn push(&mut self, value: Object)
pub fn push(&mut self, value: Object)
Pushes a value to the end of the list.
§Example
use akari::object::Object;
use std::collections::HashMap;
let mut list = Object::List(vec![Object::Str("value1".to_string()), Object::Str("value2".to_string())]);
list.push(Object::Str("new_value".to_string()));
let value = list.idx(2);
assert_eq!(value, Some(&Object::Str("new_value".to_string()))); This function will push the value to the end of the list.
Sourcepub fn pop(&mut self) -> Option<Object>
pub fn pop(&mut self) -> Option<Object>
Pops a value from the end of the list.
§Example
use akari::object::Object;
use std::collections::HashMap;
let mut list = Object::List(vec![Object::Str("value1".to_string()), Object::Str("value2".to_string())]);
let value = list.pop();
assert_eq!(value, Some(Object::Str("value2".to_string()))); Sourcepub fn remove(&mut self, index: usize) -> Option<Object>
pub fn remove(&mut self, index: usize) -> Option<Object>
Removes a value from the list by index.
§Example
use akari::object::Object;
use std::collections::HashMap;
let mut list = Object::List(vec![Object::Str("value1".to_string()), Object::Str("value2".to_string())]);
let value = list.remove(1);
assert_eq!(value, Some(Object::Str("value2".to_string()))); Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the Object.
§Example
use akari::object::Object;
use std::collections::HashMap;
let list = Object::List(vec![Object::Str("value1".to_string()), Object::Str("value2".to_string())]);
let length = list.len();
assert_eq!(length, 2);
let dict = Object::Dictionary(HashMap::from([
("key".to_string(), Object::Str("value".to_string())),
]));
let length = dict.len();
assert_eq!(length, 1); This function will return the length of the Object.