Skip to content

Commit

Permalink
jani: add Display impls
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp15b committed Oct 18, 2024
1 parent 5c8f8a6 commit 8052332
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
21 changes: 21 additions & 0 deletions jani/src/exprs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Expressions in JANI.
use std::fmt::Display;

use serde::{Deserialize, Serialize};

use crate::Identifier;
Expand All @@ -18,6 +20,15 @@ pub enum MathConstant {
Pi,
}

impl Display for MathConstant {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MathConstant::EulersNumber => write!(f, "e"),
MathConstant::Pi => write!(f, "π"),
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum ConstantValue {
Expand All @@ -36,6 +47,16 @@ impl ConstantValue {
}
}

impl Display for ConstantValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ConstantValue::Number(n) => write!(f, "{}", n),
ConstantValue::Boolean(b) => write!(f, "{}", b),
ConstantValue::MathConstant(c) => write!(f, "{}", c),
}
}
}

/// If-then-else: computes if `if` then `left` else `right`.
///
/// The result type is the type of `left` if that is assignable from the type of
Expand Down
8 changes: 7 additions & 1 deletion jani/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod models;
pub mod properties;
pub mod types;

use std::io::Read;
use std::{fmt::Display, io::Read};

use models::Model;
use serde::{Deserialize, Serialize};
Expand All @@ -21,6 +21,12 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
pub struct Identifier(pub String);

impl Display for Identifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

/// Parse a JANI model from a `&str`.
pub fn from_str(s: &str) -> serde_json::Result<Model> {
serde_json::from_str(s)
Expand Down

0 comments on commit 8052332

Please sign in to comment.