Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TryFrom<Expr> for _ and From<_> for Expr #34

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,27 @@ fn be_var_par_identifier<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PR
Ok(BoolExpr::VarParIdentifier(id))
}

impl TryFrom<Expr> for BoolExpr {
type Error = ();

fn try_from(expr: Expr) -> Result<Self, Self::Error> {
match expr {
Expr::VarParIdentifier(id) => Ok(Self::VarParIdentifier(id)),
Expr::Bool(value) => Ok(Self::Bool(value)),
_ => Err(()),
}
}
}

impl From<BoolExpr> for Expr {
fn from(expr: BoolExpr) -> Self {
match expr {
BoolExpr::Bool(value) => Self::Bool(value),
BoolExpr::VarParIdentifier(id) => self::Expr::VarParIdentifier(id),
}
}
}

#[derive(PartialEq, Clone, Debug)]
pub enum IntExpr {
Int(i128),
Expand Down Expand Up @@ -330,6 +351,27 @@ fn ie_var_par_identifier<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PR
Ok(IntExpr::VarParIdentifier(id))
}

impl TryFrom<Expr> for IntExpr {
type Error = ();

fn try_from(expr: Expr) -> Result<Self, Self::Error> {
match expr {
Expr::VarParIdentifier(id) => Ok(Self::VarParIdentifier(id)),
Expr::Int(value) => Ok(Self::Int(value)),
_ => Err(()),
}
}
}

impl From<IntExpr> for Expr {
fn from(expr: IntExpr) -> Self {
match expr {
IntExpr::Int(value) => Self::Int(value),
IntExpr::VarParIdentifier(id) => self::Expr::VarParIdentifier(id),
}
}
}

#[derive(PartialEq, Clone, Debug)]
pub enum FloatExpr {
Float(f64),
Expand Down Expand Up @@ -358,6 +400,27 @@ fn fe_var_par_identifier<'a, E: ParserError<&'a str>>(
Ok(FloatExpr::VarParIdentifier(id))
}

impl TryFrom<Expr> for FloatExpr {
type Error = ();

fn try_from(expr: Expr) -> Result<Self, Self::Error> {
match expr {
Expr::VarParIdentifier(id) => Ok(Self::VarParIdentifier(id)),
Expr::Float(value) => Ok(Self::Float(value)),
_ => Err(()),
}
}
}

impl From<FloatExpr> for Expr {
fn from(expr: FloatExpr) -> Self {
match expr {
FloatExpr::Float(value) => Self::Float(value),
FloatExpr::VarParIdentifier(id) => self::Expr::VarParIdentifier(id),
}
}
}

#[derive(PartialEq, Clone, Debug)]
pub enum SetExpr {
Set(SetLiteralExpr),
Expand Down Expand Up @@ -386,6 +449,27 @@ fn se_var_par_identifier<'a, E: ParserError<&'a str>>(input: &mut &'a str) -> PR
Ok(SetExpr::VarParIdentifier(id))
}

impl TryFrom<Expr> for SetExpr {
type Error = ();

fn try_from(expr: Expr) -> Result<Self, Self::Error> {
match expr {
Expr::VarParIdentifier(id) => Ok(Self::VarParIdentifier(id)),
Expr::Set(value) => Ok(Self::Set(value)),
_ => Err(()),
}
}
}

impl From<SetExpr> for Expr {
fn from(expr: SetExpr) -> Self {
match expr {
SetExpr::Set(value) => Self::Set(value),
SetExpr::VarParIdentifier(id) => self::Expr::VarParIdentifier(id),
}
}
}

#[derive(PartialEq, Clone, Debug)]
pub enum Expr {
VarParIdentifier(String),
Expand Down Expand Up @@ -683,6 +767,27 @@ pub fn array_of_bool_literal<'a, E: ParserError<&'a str>>(
Ok(al)
}

impl TryFrom<Expr> for ArrayOfBoolExpr {
type Error = ();

fn try_from(expr: Expr) -> Result<Self, Self::Error> {
match expr {
Expr::VarParIdentifier(id) => Ok(Self::VarParIdentifier(id)),
Expr::ArrayOfBool(value) => Ok(Self::Array(value)),
_ => Err(()),
}
}
}

impl From<ArrayOfBoolExpr> for Expr {
fn from(expr: ArrayOfBoolExpr) -> Self {
match expr {
ArrayOfBoolExpr::Array(value) => Self::ArrayOfBool(value),
ArrayOfBoolExpr::VarParIdentifier(id) => self::Expr::VarParIdentifier(id),
}
}
}

#[derive(PartialEq, Clone, Debug)]
pub enum ArrayOfIntExpr {
Array(Vec<IntExpr>),
Expand Down Expand Up @@ -732,6 +837,27 @@ where
Ok(al)
}

impl TryFrom<Expr> for ArrayOfIntExpr {
type Error = ();

fn try_from(expr: Expr) -> Result<Self, Self::Error> {
match expr {
Expr::VarParIdentifier(id) => Ok(Self::VarParIdentifier(id)),
Expr::ArrayOfInt(value) => Ok(Self::Array(value)),
_ => Err(()),
}
}
}

impl From<ArrayOfIntExpr> for Expr {
fn from(expr: ArrayOfIntExpr) -> Self {
match expr {
ArrayOfIntExpr::Array(value) => Self::ArrayOfInt(value),
ArrayOfIntExpr::VarParIdentifier(id) => self::Expr::VarParIdentifier(id),
}
}
}

#[derive(PartialEq, Clone, Debug)]
pub enum ArrayOfFloatExpr {
Array(Vec<FloatExpr>),
Expand Down Expand Up @@ -781,6 +907,27 @@ where
Ok(al)
}

impl TryFrom<Expr> for ArrayOfFloatExpr {
type Error = ();

fn try_from(expr: Expr) -> Result<Self, Self::Error> {
match expr {
Expr::VarParIdentifier(id) => Ok(Self::VarParIdentifier(id)),
Expr::ArrayOfFloat(value) => Ok(Self::Array(value)),
_ => Err(()),
}
}
}

impl From<ArrayOfFloatExpr> for Expr {
fn from(expr: ArrayOfFloatExpr) -> Self {
match expr {
ArrayOfFloatExpr::Array(value) => Self::ArrayOfFloat(value),
ArrayOfFloatExpr::VarParIdentifier(id) => self::Expr::VarParIdentifier(id),
}
}
}

#[derive(PartialEq, Clone, Debug)]
pub enum ArrayOfSetExpr {
Array(Vec<SetExpr>),
Expand Down Expand Up @@ -832,3 +979,24 @@ where
']'.parse_next(input)?;
Ok(al)
}

impl TryFrom<Expr> for ArrayOfSetExpr {
type Error = ();

fn try_from(expr: Expr) -> Result<Self, Self::Error> {
match expr {
Expr::VarParIdentifier(id) => Ok(Self::VarParIdentifier(id)),
Expr::ArrayOfSet(value) => Ok(Self::Array(value)),
_ => Err(()),
}
}
}

impl From<ArrayOfSetExpr> for Expr {
fn from(expr: ArrayOfSetExpr) -> Self {
match expr {
ArrayOfSetExpr::Array(value) => Self::ArrayOfSet(value),
ArrayOfSetExpr::VarParIdentifier(id) => self::Expr::VarParIdentifier(id),
}
}
}