graphrs/graph/
ensure.rs

1use super::Graph;
2use crate::{Error, ErrorKind};
3use std::fmt::Display;
4use std::hash::Hash;
5
6impl<T, A> Graph<T, A>
7where
8    T: Eq + Clone + PartialOrd + Ord + Hash + Send + Sync + Display,
9    A: Clone,
10{
11    /// Returns an `Err` if the `graph` is not a directed graph.
12    pub fn ensure_directed(&self) -> Result<(), Error>
13    where
14        T: Hash + Eq + Clone + Ord + Display + Send + Sync,
15        A: Clone + Send + Sync,
16    {
17        if !self.specs.directed {
18            return Err(Error {
19                kind: ErrorKind::WrongMethod,
20                message: "This method is not applicable to undirected graphs.".to_string(),
21            });
22        }
23        Ok(())
24    }
25
26    /// Returns an `Err` if the `graph` is a directed graph.
27    pub fn ensure_undirected(&self) -> Result<(), Error>
28    where
29        T: Hash + Eq + Clone + Ord + Display + Send + Sync,
30        A: Clone + Send + Sync,
31    {
32        if self.specs.directed {
33            return Err(Error {
34                kind: ErrorKind::WrongMethod,
35                message: "This method is not applicable to directed graphs.".to_string(),
36            });
37        }
38        Ok(())
39    }
40
41    /// Returns an `Err` if the `graph` is not a directed graph.
42    pub fn ensure_not_multi_edges(&self) -> Result<(), Error>
43    where
44        T: Hash + Eq + Clone + Ord + Display + Send + Sync,
45        A: Clone + Send + Sync,
46    {
47        if self.specs.multi_edges {
48            return Err(Error {
49                kind: ErrorKind::WrongMethod,
50                message: "This method is not applicable to multi-edge graphs.".to_string(),
51            });
52        }
53        Ok(())
54    }
55
56    /// Returns an `Err` if the any of the `graph`'s edges do not have a weight.
57    pub fn ensure_weighted(&self) -> Result<(), Error>
58    where
59        T: Hash + Eq + Clone + Ord,
60        A: Clone,
61    {
62        if !self.edges_have_weight() {
63            return Err(Error {
64                kind: ErrorKind::EdgeWeightNotSpecified,
65                message: "Not all edges in the graph have a weight.".to_string(),
66            });
67        }
68        Ok(())
69    }
70}