Skip to content

Commit

Permalink
Property getters seems to be working fine too
Browse files Browse the repository at this point in the history
  • Loading branch information
Rojods committed Jun 3, 2024
1 parent 4922596 commit 1a3dbb0
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 156 deletions.
2 changes: 1 addition & 1 deletion rust-core/src/fiodl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashMap, sync::Arc};
use nom::{
branch::alt, bytes::complete::{is_not, tag, take_until, take_while}, character::complete::{alphanumeric1, char, digit1, multispace0}, combinator::{fail, opt, recognize}, error::ParseError, multi::{many0, separated_list0}, number::complete::{double, float}, sequence::{delimited, preceded, terminated, tuple}, IResult, Parser
};
use petgraph::visit::{EdgeRef, NodeIndexable};
use petgraph::visit::NodeIndexable;

use crate::{EdgeInfo, ModelDriver, OpaqueTrait, SystemGraph, Trait, Vertex, VertexProperty};

Expand Down
68 changes: 35 additions & 33 deletions rust-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,106 +66,108 @@ pub enum VertexProperty {
MapProperty(HashMap<String, VertexProperty>),
}

impl TryFrom<VertexProperty> for bool {
impl TryFrom<&VertexProperty> for bool {
type Error = String;

fn try_from(value: VertexProperty) -> Result<Self, Self::Error> {
fn try_from(value: &VertexProperty) -> Result<Self, Self::Error> {
match value {
VertexProperty::BooleanProperty(v) => Ok(v),
VertexProperty::BooleanProperty(v) => Ok(*v),
_ => Err("VertexProperty is not a boolean".to_string()),
}
}
}

impl TryFrom<VertexProperty> for i32 {
impl TryFrom<&VertexProperty> for i32 {
type Error = String;

fn try_from(value: VertexProperty) -> Result<Self, Self::Error> {
fn try_from(value: &VertexProperty) -> Result<Self, Self::Error> {
match value {
VertexProperty::IntProperty(v) => Ok(v),
VertexProperty::IntProperty(v) => Ok(*v),
_ => Err("VertexProperty is not an integer".to_string()),
}
}
}

impl TryFrom<VertexProperty> for u32 {
impl TryFrom<&VertexProperty> for u32 {
type Error = String;

fn try_from(value: VertexProperty) -> Result<Self, Self::Error> {
fn try_from(value: &VertexProperty) -> Result<Self, Self::Error> {
match value {
VertexProperty::UIntProperty(v) => Ok(v),
VertexProperty::UIntProperty(v) => Ok(*v),
_ => Err("VertexProperty is not an unsigned integer".to_string()),
}
}
}

impl TryFrom<VertexProperty> for i64 {
impl TryFrom<&VertexProperty> for i64 {
type Error = String;

fn try_from(value: VertexProperty) -> Result<Self, Self::Error> {
fn try_from(value: &VertexProperty) -> Result<Self, Self::Error> {
match value {
VertexProperty::LongProperty(v) => Ok(v),
VertexProperty::LongProperty(v) => Ok(*v),
_ => Err("VertexProperty is not a long".to_string()),
}
}
}

impl TryFrom<VertexProperty> for u64 {
impl TryFrom<&VertexProperty> for u64 {
type Error = String;

fn try_from(value: VertexProperty) -> Result<Self, Self::Error> {
fn try_from(value: &VertexProperty) -> Result<Self, Self::Error> {
match value {
VertexProperty::ULongProperty(v) => Ok(v),
VertexProperty::ULongProperty(v) => Ok(*v),
_ => Err("VertexProperty is not an unsigned long".to_string()),
}
}
}

impl TryFrom<VertexProperty> for f32 {
impl TryFrom<&VertexProperty> for f32 {
type Error = String;

fn try_from(value: VertexProperty) -> Result<Self, Self::Error> {
fn try_from(value: &VertexProperty) -> Result<Self, Self::Error> {
match value {
VertexProperty::FloatProperty(v) => Ok(v),
VertexProperty::FloatProperty(v) => Ok(*v),
_ => Err("VertexProperty is not a float".to_string()),
}
}
}

impl TryFrom<VertexProperty> for f64 {
impl TryFrom<&VertexProperty> for f64 {
type Error = String;

fn try_from(value: VertexProperty) -> Result<Self, Self::Error> {
fn try_from(value: &VertexProperty) -> Result<Self, Self::Error> {
match value {
VertexProperty::DoubleProperty(v) => Ok(v),
VertexProperty::DoubleProperty(v) => Ok(*v),
_ => Err("VertexProperty is not a double".to_string()),
}
}
}

impl TryFrom<VertexProperty> for String {
impl TryFrom<&VertexProperty> for String {
type Error = String;

fn try_from(value: VertexProperty) -> Result<Self, Self::Error> {
fn try_from(value: &VertexProperty) -> Result<Self, Self::Error> {
match value {
VertexProperty::StringProperty(v) => Ok(v),
VertexProperty::StringProperty(v) => Ok(v.clone()),
_ => Err("VertexProperty is not a string".to_string()),
}
}
}

impl<T: TryFrom<VertexProperty>> TryFrom<VertexProperty> for Vec<T>
where
String: From<<T as TryFrom<VertexProperty>>::Error>
impl<'a, T> TryFrom<&'a VertexProperty> for Vec<T>
where
T: TryFrom<&'a VertexProperty>,
String: From<<T as TryFrom<&'a VertexProperty>>::Error>
{
type Error = String;

fn try_from(value: VertexProperty) -> Result<Self, Self::Error> {
fn try_from(value: &'a VertexProperty) -> Result<Self, Self::Error> {
match value {
VertexProperty::ArrayProperty(v) => {
let mut result = Vec::new();
for item in v {
result.push(T::try_from(item)?);
let n = T::try_from(item)?;
result.push(n);
}
Ok(result)
},
Expand All @@ -174,18 +176,18 @@ where
}
}

impl<T: TryFrom<VertexProperty>> TryFrom<VertexProperty> for HashMap<String, T>
impl<'a, T: TryFrom<&'a VertexProperty>> TryFrom<&'a VertexProperty> for HashMap<String, T>
where
String: From<<T as TryFrom<VertexProperty>>::Error>
String: From<<T as TryFrom<&'a VertexProperty>>::Error>
{
type Error = String;

fn try_from(value: VertexProperty) -> Result<Self, Self::Error> {
fn try_from(value: &'a VertexProperty) -> Result<Self, Self::Error> {
match value {
VertexProperty::MapProperty(v) => {
let mut mapping = HashMap::new();
for (key, item) in v {
mapping.insert(key, T::try_from(item)?);
mapping.insert(key.clone(), T::try_from(item)?);
}
Ok(mapping)
},
Expand Down
1 change: 1 addition & 0 deletions rust-generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ quote.workspace = true
serde.workspace = true
serde_json.workspace = true
syn.workspace = true
petgraph.workspace = true
Loading

0 comments on commit 1a3dbb0

Please sign in to comment.