Skip to content

Commit

Permalink
add Display trait to VoteErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
milselarch committed Sep 1, 2024
1 parent 0e2dcae commit c5a62cf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trie_rcv"
version = "1.2.0"
version = "1.2.1"
edition = "2021"
description = "Ranked Choice Voting implementation using Tries in Rust"
license = "Apache-2.0"
Expand All @@ -9,5 +9,5 @@ repository = "https://github.com/milselarch/trie_rcv"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
itertools = "0.12.1"
itertools = "0.13.0"
petgraph = "0.6.4"
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl TrieNode {
}

pub fn search_child(&self, vote_value: VoteValues) -> Option<&TrieNode> {
return if let Some(node_ref) = self.children.get(&vote_value) {
if let Some(node_ref) = self.children.get(&vote_value) {
Some(node_ref)
} else {
None
Expand Down Expand Up @@ -100,7 +100,7 @@ fn is_graph_acyclic(graph: &DiGraph<u16, u64>) -> bool {
// get neighbors of node where there is an
// outgoing edge from node to neighbor
let directed_neighbors: Vec<NodeIndex> = graph
.edges_directed(*node, petgraph::Direction::Outgoing)
.edges_directed(*node, Direction::Outgoing)
.map(|edge| { edge.target()} )
.collect();

Expand Down Expand Up @@ -309,9 +309,9 @@ impl RankedChoiceVoteTrie {
);

if pairs_result.1 == false {
return lowest_vote_candidates;
lowest_vote_candidates
} else {
return pairs_result.0;
pairs_result.0
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/vote.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashSet;
use std::fmt;

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum SpecialVotes {
Expand All @@ -22,6 +23,19 @@ pub enum VoteErrors {
VoteIsEmpty
}

impl fmt::Display for VoteErrors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
VoteErrors::InvalidCastToCandidate => write!(f, "Invalid cast to candidate"),
VoteErrors::InvalidCastToSpecialVote => write!(f, "Invalid cast to special vote"),
VoteErrors::ReadOutOfBounds => write!(f, "Read out of bounds"),
VoteErrors::NonFinalSpecialVote => write!(f, "Non-final special vote"),
VoteErrors::DuplicateVotes => write!(f, "Duplicate votes"),
VoteErrors::VoteIsEmpty => write!(f, "Vote is empty"),
}
}
}

impl VoteValues {
pub fn to_int(self) -> i32 {
match self {
Expand Down Expand Up @@ -116,7 +130,7 @@ impl RankedVote {
pub fn from_candidates(
candidates: &[u16]
) -> Result<RankedVote, VoteErrors> {
return Self::from_vector(
Self::from_vector(
&candidates.iter().map(|x| *x as i32).collect()
)
}
Expand Down
1 change: 0 additions & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use itertools::all;
use trie_rcv;
use trie_rcv::{EliminationStrategies, RankedChoiceVoteTrie};
use trie_rcv::vote::{SpecialVotes, RankedVote};
Expand Down

0 comments on commit c5a62cf

Please sign in to comment.