use crate::il::*;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Default)]
pub struct Edge {
head: usize,
tail: usize,
condition: Option<Expression>,
comment: Option<String>,
}
impl Edge {
pub(crate) fn new(head: usize, tail: usize, condition: Option<Expression>) -> Edge {
Edge {
head,
tail,
condition,
comment: None,
}
}
pub fn condition(&self) -> Option<&Expression> {
self.condition.as_ref()
}
pub fn condition_mut(&mut self) -> Option<&mut Expression> {
self.condition.as_mut()
}
pub fn head(&self) -> usize {
self.head
}
pub fn tail(&self) -> usize {
self.tail
}
pub fn set_comment(&mut self, comment: Option<String>) {
self.comment = comment;
}
pub fn comment(&self) -> &Option<String> {
&self.comment
}
}
impl fmt::Display for Edge {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(ref comment) = self.comment {
writeln!(f, "// {}", comment)?
}
if let Some(ref condition) = self.condition {
write!(
f,
"(0x{:X}->0x{:X}) ? ({})",
self.head, self.tail, condition
)?
} else {
write!(f, "(0x{:X}->0x{:X})", self.head, self.tail)?
}
Ok(())
}
}
impl graph::Edge for Edge {
fn head(&self) -> usize {
self.head
}
fn tail(&self) -> usize {
self.tail
}
fn dot_label(&self) -> String {
match self.condition {
Some(ref condition) => format!("{}", condition),
None => "".to_string(),
}
}
}