summaryrefslogtreecommitdiffstats
path: root/vendor/semver/src/display.rs
blob: 3c2871bb9783fcac79e91f81073f0abde97dd102 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use crate::{BuildMetadata, Comparator, Op, Prerelease, Version, VersionReq};
use core::fmt::{self, Alignment, Debug, Display, Write};

impl Display for Version {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        let do_display = |formatter: &mut fmt::Formatter| -> fmt::Result {
            write!(formatter, "{}.{}.{}", self.major, self.minor, self.patch)?;
            if !self.pre.is_empty() {
                write!(formatter, "-{}", self.pre)?;
            }
            if !self.build.is_empty() {
                write!(formatter, "+{}", self.build)?;
            }
            Ok(())
        };

        let do_len = || -> usize {
            digits(self.major)
                + 1
                + digits(self.minor)
                + 1
                + digits(self.patch)
                + !self.pre.is_empty() as usize
                + self.pre.len()
                + !self.build.is_empty() as usize
                + self.build.len()
        };

        pad(formatter, do_display, do_len)
    }
}

impl Display for VersionReq {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        if self.comparators.is_empty() {
            return formatter.write_str("*");
        }
        for (i, comparator) in self.comparators.iter().enumerate() {
            if i > 0 {
                formatter.write_str(", ")?;
            }
            write!(formatter, "{}", comparator)?;
        }
        Ok(())
    }
}

impl Display for Comparator {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        let op = match self.op {
            Op::Exact => "=",
            Op::Greater => ">",
            Op::GreaterEq => ">=",
            Op::Less => "<",
            Op::LessEq => "<=",
            Op::Tilde => "~",
            Op::Caret => "^",
            Op::Wildcard => "",
            #[cfg(no_non_exhaustive)]
            Op::__NonExhaustive => unreachable!(),
        };
        formatter.write_str(op)?;
        write!(formatter, "{}", self.major)?;
        if let Some(minor) = &self.minor {
            write!(formatter, ".{}", minor)?;
            if let Some(patch) = &self.patch {
                write!(formatter, ".{}", patch)?;
                if !self.pre.is_empty() {
                    write!(formatter, "-{}", self.pre)?;
                }
            } else if self.op == Op::Wildcard {
                formatter.write_str(".*")?;
            }
        } else if self.op == Op::Wildcard {
            formatter.write_str(".*")?;
        }
        Ok(())
    }
}

impl Display for Prerelease {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

impl Display for BuildMetadata {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

impl Debug for Version {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        let mut debug = formatter.debug_struct("Version");
        debug
            .field("major", &self.major)
            .field("minor", &self.minor)
            .field("patch", &self.patch);
        if !self.pre.is_empty() {
            debug.field("pre", &self.pre);
        }
        if !self.build.is_empty() {
            debug.field("build", &self.build);
        }
        debug.finish()
    }
}

impl Debug for Prerelease {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "Prerelease(\"{}\")", self)
    }
}

impl Debug for BuildMetadata {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "BuildMetadata(\"{}\")", self)
    }
}

fn pad(
    formatter: &mut fmt::Formatter,
    do_display: impl FnOnce(&mut fmt::Formatter) -> fmt::Result,
    do_len: impl FnOnce() -> usize,
) -> fmt::Result {
    let min_width = match formatter.width() {
        Some(min_width) => min_width,
        None => return do_display(formatter),
    };

    let len = do_len();
    if len >= min_width {
        return do_display(formatter);
    }

    let default_align = Alignment::Left;
    let align = formatter.align().unwrap_or(default_align);
    let padding = min_width - len;
    let (pre_pad, post_pad) = match align {
        Alignment::Left => (0, padding),
        Alignment::Right => (padding, 0),
        Alignment::Center => (padding / 2, (padding + 1) / 2),
    };

    let fill = formatter.fill();
    for _ in 0..pre_pad {
        formatter.write_char(fill)?;
    }

    do_display(formatter)?;

    for _ in 0..post_pad {
        formatter.write_char(fill)?;
    }
    Ok(())
}

fn digits(val: u64) -> usize {
    if val < 10 {
        1
    } else {
        1 + digits(val / 10)
    }
}