|
46 | 46 | //! | Feature | Description | |
47 | 47 | //! | - | - | |
48 | 48 | //! | from_instruction | Enables functionality that allows graphs to be parsed from a list of instructions. | |
| 49 | +//! | serde | Serde serialization and deserialization support for some objects. | |
49 | 50 |
|
50 | 51 | use std::{fmt::{Display, Debug}, rc::Rc, cell::RefCell, collections::HashMap, hash::Hash}; |
| 52 | + |
| 53 | +#[cfg(feature = "serde")] |
| 54 | +use serde::{Serialize, Deserialize}; |
| 55 | + |
51 | 56 | /// Contains implementations for the graph to work. |
52 | 57 | mod graph; |
53 | 58 | /// Contains all algorithms that are implemented in this crate. |
@@ -120,6 +125,7 @@ impl<T: Display + Eq + Hash> Graph<T> { |
120 | 125 | /// Structure to store the shortest path and distance from one node |
121 | 126 | /// to other nodes after a pathfinding algorithm has been run on a graph. |
122 | 127 | #[derive(Eq, PartialEq, Debug)] |
| 128 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
123 | 129 | pub struct ShortestPathTree<T: Display + Clone + Hash + Eq> { |
124 | 130 | source: T, |
125 | 131 | results: HashMap<T, Option<(i32, ShortestPath<T>)>>, |
@@ -213,6 +219,7 @@ impl<T: Display + Clone + Eq + Hash> ShortestPathTree<T> { |
213 | 219 |
|
214 | 220 | /// The shortest path from one node to another. |
215 | 221 | #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] |
| 222 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
216 | 223 | pub struct ShortestPath<T: Display + Clone> {// Add documentation |
217 | 224 | /// Contains a list of node ids, first entry is the start node, last entry is the target node. |
218 | 225 | path: Vec<T>, //TODO maybe add later that the distance between each node is stored as well |
@@ -374,10 +381,11 @@ impl<T: Display + Clone + Eq> TryFrom<&Rc<RefCell<Node<T>>>> for ShortestPath<T> |
374 | 381 | } |
375 | 382 | } |
376 | 383 |
|
377 | | -#[derive(Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Debug)] |
378 | 384 | /// Errors that can occur when edges are added to the graph. |
379 | 385 | /// |
380 | 386 | /// Variants specify what exact node is missing. |
| 387 | +#[derive(Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Debug)] |
| 388 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
381 | 389 | pub enum AddEdgeError { |
382 | 390 | /// Indicates that the source node is missing from the graph, |
383 | 391 | SourceMissing, |
|
0 commit comments