hellman_output/
lib.rs

1use std::{fmt::Display, str::FromStr, string::ParseError};
2
3/// Helper for the following requirements for output:
4///
5/// - Any stdout output line with a required result must have OUTPUT as the first word (token).
6/// - All numerical values required by an assignment must have whitespace (which includes end of line) around them.
7/// - Required textual output should be surrounded by single colons (:).
8/// - The order for results is assignment specific and important, they must emanate from the application in the order dictated by the assignment.
9#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
10pub struct HellmanOutput(String);
11
12impl Default for HellmanOutput {
13    fn default() -> Self {
14        Self("OUTPUT".to_string())
15    }
16}
17
18impl FromStr for HellmanOutput {
19    type Err = ParseError;
20
21    fn from_str(s: &str) -> Result<Self, Self::Err> {
22        Ok(Self::default().push_str(s))
23    }
24}
25
26impl Display for HellmanOutput {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        write!(f, "{}", self.0)
29    }
30}
31
32impl HellmanOutput {
33    /// Push a number to the output string
34    pub fn push_numeric(&self, num: isize) -> Self {
35        let num_str = &format!(" {} ", num);
36        Self(self.0.clone() + num_str)
37    }
38
39    /// Push a textual element to the output string
40    pub fn push_str(&self, s: &str) -> Self {
41        let new_addition = &format!(" :{}: ", s);
42        Self(self.0.clone() + new_addition)
43    }
44}