rust_pgdatadiff/diff/sequence/query/output/
mod.rs

1use crate::diff::diff_output::DiffOutput;
2use crate::diff::types::DiffOutputMarker;
3use colored::{ColoredString, Colorize};
4use std::fmt::Display;
5
6/// Represents the source of a sequence.
7#[derive(Clone)]
8pub enum SequenceSource {
9    First,
10    Second,
11}
12
13impl Display for SequenceSource {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        match self {
16            Self::First => write!(f, "first"),
17            Self::Second => write!(f, "second"),
18        }
19    }
20}
21
22/// Represents the difference in count between two sequences.
23#[derive(Clone)]
24pub struct SequenceCountDiff(i64, i64);
25
26impl SequenceCountDiff {
27    /// Creates a new `SequenceCountDiff` instance with the given counts.
28    pub fn new(first: i64, second: i64) -> Self {
29        Self(first, second)
30    }
31
32    /// Returns the count of the first sequence.
33    pub fn first(&self) -> i64 {
34        self.0
35    }
36
37    /// Returns the count of the second sequence.
38    pub fn second(&self) -> i64 {
39        self.1
40    }
41}
42
43#[derive(Clone)]
44/// Represents the output of a sequence difference.
45pub enum SequenceDiffOutput {
46    /// Indicates that there is no difference between the sequences.
47    NoDiff(String),
48    /// Indicates that a sequence does not exist in a specific source.
49    NotExists(String, SequenceSource),
50    /// Indicates a difference in count between the sequences.
51    Diff(String, SequenceCountDiff),
52}
53
54impl SequenceDiffOutput {
55    /// Converts the `SequenceDiffOutput` to a colored string representation.
56    pub fn to_string(&self) -> ColoredString {
57        match self {
58            Self::NoDiff(sequence) => format!("{} - No difference\n", sequence).green().bold(),
59            Self::NotExists(sequence, source) => {
60                format!("{} - Does not exist in {}\n", sequence, source)
61                    .red()
62                    .bold()
63                    .underline()
64            }
65            Self::Diff(sequence, diffs) => format!(
66                "Difference in sequence:{} - First: {}, Second: {}\n",
67                sequence,
68                diffs.first(),
69                diffs.second()
70            )
71            .red()
72            .bold()
73            .underline(),
74        }
75    }
76}
77
78impl DiffOutputMarker for SequenceDiffOutput {
79    fn convert(self) -> DiffOutput {
80        DiffOutput::SequenceDiff(self.clone())
81    }
82}
83
84impl From<SequenceDiffOutput> for DiffOutput {
85    fn from(val: SequenceDiffOutput) -> Self {
86        DiffOutput::SequenceDiff(val)
87    }
88}