1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::jcli::command::VotesCommand;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::{assert::PathAssert, NamedTempFile};
use std::path::Path;

pub mod committee;
mod crs;
mod tally;

pub use committee::Committee;
pub use crs::Crs;
use jortestkit::prelude::ProcessOutput;
pub use tally::Tally;

pub struct Votes {
    votes_command: VotesCommand,
}

impl Votes {
    pub fn new(votes_command: VotesCommand) -> Self {
        Self { votes_command }
    }

    pub fn committee(self) -> Committee {
        Committee::new(self.votes_command.committee())
    }

    pub fn crs(self) -> Crs {
        Crs::new(self.votes_command.crs())
    }

    pub fn election_public_key<S: Into<String>>(
        self,
        member_key: S,
    ) -> Result<String, std::io::Error> {
        let output_file = NamedTempFile::new("election_public_key.tmp").unwrap();
        self.votes_command
            .election_public_key(member_key, output_file.path())
            .build()
            .assert()
            .success();

        output_file.assert(jortestkit::prelude::file_exists_and_not_empty());
        jortestkit::prelude::read_file(output_file.path())
    }

    pub fn tally(self) -> Tally {
        Tally::new(self.votes_command.tally())
    }

    pub fn update_proposal<P: AsRef<Path>, Q: AsRef<Path>>(self, config: P, secret: Q) -> String {
        self.votes_command
            .update_proposal(config, secret)
            .build()
            .assert()
            .success()
            .get_output()
            .as_single_line()
    }

    pub fn update_vote<R: Into<String>, P: AsRef<Path>>(self, proposal_id: R, secret: P) -> String {
        self.votes_command
            .update_vote(proposal_id, secret)
            .build()
            .assert()
            .success()
            .get_output()
            .as_single_line()
    }
}