Enum Object

Source
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

Source

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()))]))); 
Source

pub fn type_of(&self) -> String

Source

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.

Source

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.

Source

pub fn boolean(&self) -> bool

Converts the Object into a boolean value.

Source

pub fn string(&self) -> String

Converts the Object into a string representation.

Source

pub fn list(&self) -> Vec<Object>

Converts the Object into a list of Objects.

Source

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]} 
Source

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"); 
Source

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.

Source

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(); 
Source

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()))); 
Source

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()))); 
Source

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()))); 
Source

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.

Source

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()))); 
Source

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.

Source

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.

Source

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()))); 
Source

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()))); 
Source

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.

Source

pub fn interal_value_as_string(&self) -> String

Source

pub fn format(&self) -> String

Trait Implementations§

Source§

impl Clone for Object

Source§

fn clone(&self) -> Object

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Object

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Object

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&String> for Object

Source§

fn from(s: &String) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Object

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl<S, T> From<HashMap<S, T>> for Object
where S: Into<String> + Hash + Eq, T: Into<Object>,

Source§

fn from(map: HashMap<S, T>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Object

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Vec<T>> for Object
where T: Into<Object>,

Source§

fn from(vec: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Object

Source§

fn from(b: bool) -> Self

Converts to this type from the input type.
Source§

impl From<char> for Object

Source§

fn from(c: char) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Object

Source§

fn from(n: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Object

Source§

fn from(n: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for Object

Source§

fn from(n: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Object

Source§

fn from(n: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Object

Source§

fn from(n: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Object

Source§

fn from(n: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Object

Source§

fn from(n: i8) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for Object

Source§

fn from(n: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for Object

Source§

fn from(n: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Object

Source§

fn from(n: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Object

Source§

fn from(n: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Object

Source§

fn from(n: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Object

Source§

fn from(n: u8) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Object

Source§

fn from(n: usize) -> Self

Converts to this type from the input type.
Source§

impl Hash for Object

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Into<String> for Object

Source§

fn into(self) -> String

Converts this type into the (usually inferred) input type.
Source§

impl Into<bool> for Object

Source§

fn into(self) -> bool

Converts this type into the (usually inferred) input type.
Source§

impl Into<char> for Object

Source§

fn into(self) -> char

Converts this type into the (usually inferred) input type.
Source§

impl Into<f32> for Object

Source§

fn into(self) -> f32

Converts this type into the (usually inferred) input type.
Source§

impl Into<f64> for Object

Source§

fn into(self) -> f64

Converts this type into the (usually inferred) input type.
Source§

impl Into<i128> for Object

Source§

fn into(self) -> i128

Converts this type into the (usually inferred) input type.
Source§

impl Into<i16> for Object

Source§

fn into(self) -> i16

Converts this type into the (usually inferred) input type.
Source§

impl Into<i32> for Object

Source§

fn into(self) -> i32

Converts this type into the (usually inferred) input type.
Source§

impl Into<i64> for Object

Source§

fn into(self) -> i64

Converts this type into the (usually inferred) input type.
Source§

impl Into<i8> for Object

Source§

fn into(self) -> i8

Converts this type into the (usually inferred) input type.
Source§

impl Into<isize> for Object

Source§

fn into(self) -> isize

Converts this type into the (usually inferred) input type.
Source§

impl Into<u128> for Object

Source§

fn into(self) -> u128

Converts this type into the (usually inferred) input type.
Source§

impl Into<u16> for Object

Source§

fn into(self) -> u16

Converts this type into the (usually inferred) input type.
Source§

impl Into<u32> for Object

Source§

fn into(self) -> u32

Converts this type into the (usually inferred) input type.
Source§

impl Into<u64> for Object

Source§

fn into(self) -> u64

Converts this type into the (usually inferred) input type.
Source§

impl Into<u8> for Object

Source§

fn into(self) -> u8

Converts this type into the (usually inferred) input type.
Source§

impl Into<usize> for Object

Source§

fn into(self) -> usize

Converts this type into the (usually inferred) input type.
Source§

impl PartialEq for Object

Source§

fn eq(&self, other: &Object) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Object

Source§

impl StructuralPartialEq for Object

Auto Trait Implementations§

§

impl Freeze for Object

§

impl RefUnwindSafe for Object

§

impl Send for Object

§

impl Sync for Object

§

impl Unpin for Object

§

impl UnwindSafe for Object

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.