Debian Codegraph @

third_party/rust/rustversion/build/rustc.rs

raw
1 use self::Channel::*;
2 use std::fmt::{self, Debug};
3
4 pub enum ParseResult {
5 Success(Version),
6 OopsClippy,
7 OopsMirai,
8 Unrecognized,
9 }
10
11 #[cfg_attr(test, derive(PartialEq))]
12 pub struct Version {
13 pub minor: u16,
14 pub patch: u16,
15 pub channel: Channel,
16 }
17
18 #[cfg_attr(test, derive(PartialEq))]
19 pub enum Channel {
20 Stable,
21 Beta,
22 Nightly(Date),
23 Dev,
24 }
25
26 #[cfg_attr(test, derive(PartialEq))]
27 pub struct Date {
28 pub year: u16,
29 pub month: u8,
30 pub day: u8,
31 }
32
33 pub fn parse(string: &str) -> ParseResult {
34 let last_line = string.lines().last().unwrap_or(string);
35 let mut words = last_line.trim().split(' ');
36
37 match words.next() {
38 Some("rustc") => {}
39 Some(word) if word.starts_with("clippy") => return ParseResult::OopsClippy,
40 Some("mirai") => return ParseResult::OopsMirai,
41 Some(_) | None => return ParseResult::Unrecognized,
42 }
43
44 parse_words(&mut words).map_or(ParseResult::Unrecognized, ParseResult::Success)
45 }
46
47 fn parse_words(words: &mut dyn Iterator<Item = &str>) -> Option<Version> {
48 let mut version_channel = words.next()?.split('-');
49 let version = version_channel.next()?;
50 let channel = version_channel.next();
51
52 let mut digits = version.split('.');
53 let major = digits.next()?;
54 if major != "1" {
55 return None;
56 }
57 let minor = digits.next()?.parse().ok()?;
58 let patch = digits.next().unwrap_or("0").parse().ok()?;
59
60 let channel = match channel {
61 None => Stable,
62 Some("dev") => Dev,
63 Some(channel) if channel.starts_with("beta") => Beta,
64 Some("nightly") => match words.next() {
65 Some(hash) if hash.starts_with('(') => match words.next() {
66 None if hash.ends_with(')') => Dev,
67 Some(date) if date.ends_with(')') => {
68 let mut date = date[..date.len() - 1].split('-');
69 let year = date.next()?.parse().ok()?;
70 let month = date.next()?.parse().ok()?;
71 let day = date.next()?.parse().ok()?;
72 match date.next() {
73 None => Nightly(Date { year, month, day }),
74 Some(_) => return None,
75 }
76 }
77 None | Some(_) => return None,
78 },
79 Some(_) => return None,
80 None => Dev,
81 },
82 Some(_) => return None,
83 };
84
85 Some(Version {
86 minor,
87 patch,
88 channel,
89 })
90 }
91
92 impl Debug for Version {
93 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
94 formatter
95 .debug_struct("crate::version::Version")
96 .field("minor", &self.minor)
97 .field("patch", &self.patch)
98 .field("channel", &self.channel)
99 .finish()
100 }
101 }
102
103 impl Debug for Channel {
104 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
105 match self {
106 Channel::Stable => formatter.write_str("crate::version::Channel::Stable"),
107 Channel::Beta => formatter.write_str("crate::version::Channel::Beta"),
108 Channel::Nightly(date) => formatter
109 .debug_tuple("crate::version::Channel::Nightly")
110 .field(date)
111 .finish(),
112 Channel::Dev => formatter.write_str("crate::version::Channel::Dev"),
113 }
114 }
115 }
116
117 impl Debug for Date {
118 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
119 formatter
120 .debug_struct("crate::date::Date")
121 .field("year", &self.year)
122 .field("month", &self.month)
123 .field("day", &self.day)
124 .finish()
125 }
126 }