pub enum ErrorKind {
VertexNotFound,
EdgeNotFound,
InvalidEdgeId,
}
pub struct Error {
kind: ErrorKind,
msg: String,
}
impl Error {
pub fn new(kind: ErrorKind, msg: String) -> Self {
Error { kind, msg }
}
pub fn new_vnf(vertex_id: usize) -> Self {
Error {
kind: ErrorKind::VertexNotFound,
msg: format!("Vertex with id: {} not found", vertex_id),
}
}
pub fn new_enf(edge_id: usize) -> Self {
Error {
kind: ErrorKind::EdgeNotFound,
msg: format!("Edge with id: {} not found", edge_id),
}
}
pub fn new_iei(src_id: usize, dst_id: usize, edge_id: usize) -> Self {
Error {
kind: ErrorKind::InvalidEdgeId,
msg: format!(
"Edge with id: {} exists but it's not from vertex: {} to vertex: {}",
edge_id, src_id, dst_id
),
}
}
pub fn msg(&self) -> &String {
&self.msg
}
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
}
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.msg())
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.msg())
}
}
impl std::error::Error for Error {}