this repo has no description
1use nu_ansi_term::Color::{Green, Red, Yellow};
2use std::{cmp::Ordering, fmt::Display};
3
4use super::color;
5
6#[derive(Debug)]
7pub struct VersionComponent(String, Ordering);
8
9#[derive(Debug)]
10pub struct Version(Vec<VersionComponent>);
11
12#[derive(Debug)]
13pub struct VersionList(pub Vec<Version>);
14
15impl VersionComponent {
16 pub fn new(version: String, ordering: Ordering) -> Self {
17 Self(version, ordering)
18 }
19}
20
21impl Version {
22 pub fn new() -> Self {
23 Self(Vec::new())
24 }
25
26 pub fn push(&mut self, version: VersionComponent) {
27 self.0.push(version);
28 }
29}
30
31impl Display for Version {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 let mut out = String::new();
34
35 for component in &self.0 {
36 let val = &component.0;
37 let cmp = component.1;
38
39 let text = if color::color_enabled() {
40 if cmp == Ordering::Less {
41 format!("{}", Red.paint(val))
42 } else if cmp == Ordering::Greater {
43 format!("{}", Green.paint(val))
44 } else {
45 format!("{}", Yellow.paint(val))
46 }
47 } else {
48 val.clone()
49 };
50
51 out.push_str(&text);
52 out.push('.');
53 }
54
55 out.pop(); // remove last comma
56 write!(f, "{out}")
57 }
58}
59
60impl VersionList {
61 pub fn new() -> Self {
62 Self(Vec::new())
63 }
64
65 pub fn push(&mut self, version: Version) {
66 self.0.push(version);
67 }
68}
69
70impl Display for VersionList {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 let out = self
73 .0
74 .iter()
75 .map(ToString::to_string)
76 .collect::<Vec<_>>()
77 .join(", ");
78 write!(f, "{out}")
79 }
80}