Skip to content

Commit 6ceb4ac

Browse files
committed
Updated code to be in line with C-SERDE Rust API Guideline
1 parent 3b52c32 commit 6ceb4ac

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ edition = "2021"
77

88
[features]
99
from_instruction = []
10+
serde = ["dep:serde"]
1011

1112
[dependencies]
13+
serde = { version = "1.0", optional = true, features = ["derive"] }
1214

1315
[dev-dependencies]
1416
criterion = { version = "0.4", features = ["html_reports"] }
1517

1618
[[bench]]
1719
name = "algorithm_benchmark"
18-
harness = false
20+
harness = false

src/algorithms.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
//! | Dijkstra | 52.3155 ms |
1818
use std::{fmt::Display, hash::Hash, collections::{BinaryHeap, HashSet}, rc::Rc, cell::RefCell};
1919

20+
#[cfg(feature = "serde")]
21+
use serde::{Serialize, Deserialize};
22+
2023
use crate::{Graph, Node, ShortestPathTree};
2124

2225
/// Calculates the shortest distance between one source node and all other nodes on the graph using
@@ -239,6 +242,7 @@ pub fn bellman_ford<T: Display + Eq + Clone + Hash>(graph: &mut Graph<T>, source
239242

240243
/// Errors that can occur when algorithms are run.
241244
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Copy, Hash)]
245+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
242246
pub enum RunAlgorithmError {
243247
/// Indicates that the source node is not contained within the graph.
244248
SourceNodeMissing,

src/instruction.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,24 @@
3737
//! ```
3838
use std::{fmt::Display, hash::Hash};
3939

40+
#[cfg(feature = "serde")]
41+
use serde::{Serialize, Deserialize};
42+
4043
use crate::Graph;
4144

42-
#[cfg(feature = "from_instruction")]
4345
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
46+
#[cfg(feature = "from_instruction")]
47+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4448
enum Instruction<T: Display + Clone> {
4549
AddNode(T),
4650
AddEdge(i32, T, T),
4751
AddDoubleEdge(i32, T, T),
4852
}
4953

5054
/// A list of instructions used to construct a graph.
51-
#[cfg(feature = "from_instruction")]
5255
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
56+
#[cfg(feature = "from_instruction")]
57+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5358
pub struct Instructions<T: Display + Clone> {
5459
instructions: Vec<Instruction<T>>,
5560
}

src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@
4646
//! | Feature | Description |
4747
//! | - | - |
4848
//! | 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. |
4950
5051
use std::{fmt::{Display, Debug}, rc::Rc, cell::RefCell, collections::HashMap, hash::Hash};
52+
53+
#[cfg(feature = "serde")]
54+
use serde::{Serialize, Deserialize};
55+
5156
/// Contains implementations for the graph to work.
5257
mod graph;
5358
/// Contains all algorithms that are implemented in this crate.
@@ -120,6 +125,7 @@ impl<T: Display + Eq + Hash> Graph<T> {
120125
/// Structure to store the shortest path and distance from one node
121126
/// to other nodes after a pathfinding algorithm has been run on a graph.
122127
#[derive(Eq, PartialEq, Debug)]
128+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
123129
pub struct ShortestPathTree<T: Display + Clone + Hash + Eq> {
124130
source: T,
125131
results: HashMap<T, Option<(i32, ShortestPath<T>)>>,
@@ -213,6 +219,7 @@ impl<T: Display + Clone + Eq + Hash> ShortestPathTree<T> {
213219

214220
/// The shortest path from one node to another.
215221
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
222+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
216223
pub struct ShortestPath<T: Display + Clone> {// Add documentation
217224
/// Contains a list of node ids, first entry is the start node, last entry is the target node.
218225
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>
374381
}
375382
}
376383

377-
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Debug)]
378384
/// Errors that can occur when edges are added to the graph.
379385
///
380386
/// Variants specify what exact node is missing.
387+
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Debug)]
388+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
381389
pub enum AddEdgeError {
382390
/// Indicates that the source node is missing from the graph,
383391
SourceMissing,

0 commit comments

Comments
 (0)