Skip to content

Commit 241658e

Browse files
committed
Merge errors module into crate root
1 parent f4b8b68 commit 241658e

File tree

2 files changed

+149
-153
lines changed

2 files changed

+149
-153
lines changed

src/errors.rs

Lines changed: 0 additions & 149 deletions
This file was deleted.

src/lib.rs

Lines changed: 149 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,16 @@ extern crate doc_comment;
5959
#[cfg(test)]
6060
doctest!("../README.md");
6161

62-
use semver::Identifier;
6362
use std::process::Command;
64-
use std::{env, fmt, str};
63+
use std::{env, error, fmt, io, num, str};
6564
use std::{ffi::OsString, str::FromStr};
6665

66+
use semver::{self, Identifier};
6767
// Convenience re-export to allow version comparison without needing to add
6868
// semver crate.
6969
pub use semver::Version;
7070

71-
mod errors;
72-
pub use crate::errors::{Error, LlvmVersionParseError, Result};
71+
use Error::*;
7372

7473
/// Release channel of the compiler.
7574
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
@@ -282,3 +281,149 @@ pub fn version_meta_for(verbose_version_string: &str) -> Result<VersionMeta> {
282281
llvm_version,
283282
})
284283
}
284+
285+
/// LLVM Version Parse Error
286+
#[derive(Debug)]
287+
pub enum LlvmVersionParseError {
288+
/// An error occurred in parsing a version component as an integer
289+
ParseIntError(num::ParseIntError),
290+
/// A version component must not have leading zeros
291+
ComponentMustNotHaveLeadingZeros,
292+
/// A version component has a sign
293+
ComponentMustNotHaveSign,
294+
/// Minor version component must be zero on LLVM versions later than 4.0
295+
MinorVersionMustBeZeroAfter4,
296+
/// Minor version component is required on LLVM versions earlier than 4.0
297+
MinorVersionRequiredBefore4,
298+
/// Too many components
299+
TooManyComponents,
300+
}
301+
302+
impl From<num::ParseIntError> for LlvmVersionParseError {
303+
fn from(e: num::ParseIntError) -> Self {
304+
LlvmVersionParseError::ParseIntError(e)
305+
}
306+
}
307+
308+
impl fmt::Display for LlvmVersionParseError {
309+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
310+
match self {
311+
LlvmVersionParseError::ParseIntError(e) => {
312+
write!(f, "error parsing LLVM version component: {}", e)
313+
}
314+
LlvmVersionParseError::ComponentMustNotHaveLeadingZeros => {
315+
write!(f, "a version component must not have leading zeros")
316+
}
317+
LlvmVersionParseError::ComponentMustNotHaveSign => {
318+
write!(f, "a version component must not have a sign")
319+
}
320+
LlvmVersionParseError::MinorVersionMustBeZeroAfter4 => write!(
321+
f,
322+
"LLVM's minor version component must be 0 for versions greater than 4.0"
323+
),
324+
LlvmVersionParseError::MinorVersionRequiredBefore4 => write!(
325+
f,
326+
"LLVM's minor version component is required for versions less than 4.0"
327+
),
328+
LlvmVersionParseError::TooManyComponents => write!(f, "too many version components"),
329+
}
330+
}
331+
}
332+
333+
impl error::Error for LlvmVersionParseError {
334+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
335+
match self {
336+
LlvmVersionParseError::ParseIntError(e) => Some(e),
337+
LlvmVersionParseError::ComponentMustNotHaveLeadingZeros
338+
| LlvmVersionParseError::ComponentMustNotHaveSign
339+
| LlvmVersionParseError::MinorVersionMustBeZeroAfter4
340+
| LlvmVersionParseError::MinorVersionRequiredBefore4
341+
| LlvmVersionParseError::TooManyComponents => None,
342+
}
343+
}
344+
}
345+
346+
/// The error type for this crate.
347+
#[derive(Debug)]
348+
pub enum Error {
349+
/// An error occurred while trying to find the `rustc` to run.
350+
CouldNotExecuteCommand(io::Error),
351+
/// Error output from the command that was run.
352+
CommandError {
353+
/// stdout output from the command
354+
stdout: String,
355+
/// stderr output from the command
356+
stderr: String,
357+
},
358+
/// The output of `rustc -vV` was not valid utf-8.
359+
Utf8Error(str::Utf8Error),
360+
/// The output of `rustc -vV` was not in the expected format.
361+
UnexpectedVersionFormat,
362+
/// An error occurred in parsing a `VersionReq`.
363+
ReqParseError(semver::ReqParseError),
364+
/// An error occurred in parsing the semver.
365+
SemVerError(semver::SemVerError),
366+
/// The pre-release tag is unknown.
367+
UnknownPreReleaseTag(Identifier),
368+
/// An error occurred in parsing a `LlvmVersion`.
369+
LlvmVersionError(LlvmVersionParseError),
370+
}
371+
372+
impl fmt::Display for Error {
373+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
374+
match *self {
375+
CouldNotExecuteCommand(ref e) => write!(f, "could not execute command: {}", e),
376+
CommandError {
377+
ref stdout,
378+
ref stderr,
379+
} => write!(
380+
f,
381+
"error from command -- stderr:\n\n{}\n\nstderr:\n\n{}",
382+
stderr, stdout,
383+
),
384+
Utf8Error(_) => write!(f, "invalid UTF-8 output from `rustc -vV`"),
385+
UnexpectedVersionFormat => write!(f, "unexpected `rustc -vV` format"),
386+
ReqParseError(ref e) => write!(f, "error parsing version requirement: {}", e),
387+
SemVerError(ref e) => write!(f, "error parsing version: {}", e),
388+
UnknownPreReleaseTag(ref i) => write!(f, "unknown pre-release tag: {}", i),
389+
LlvmVersionError(ref e) => write!(f, "error parsing LLVM's version: {}", e),
390+
}
391+
}
392+
}
393+
394+
impl error::Error for Error {
395+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
396+
match *self {
397+
CouldNotExecuteCommand(ref e) => Some(e),
398+
CommandError { .. } => None,
399+
Utf8Error(ref e) => Some(e),
400+
UnexpectedVersionFormat => None,
401+
ReqParseError(ref e) => Some(e),
402+
SemVerError(ref e) => Some(e),
403+
UnknownPreReleaseTag(_) => None,
404+
LlvmVersionError(ref e) => Some(e),
405+
}
406+
}
407+
}
408+
409+
macro_rules! impl_from {
410+
($($err_ty:ty => $variant:ident),* $(,)*) => {
411+
$(
412+
impl From<$err_ty> for Error {
413+
fn from(e: $err_ty) -> Error {
414+
Error::$variant(e)
415+
}
416+
}
417+
)*
418+
}
419+
}
420+
421+
impl_from! {
422+
str::Utf8Error => Utf8Error,
423+
semver::SemVerError => SemVerError,
424+
semver::ReqParseError => ReqParseError,
425+
LlvmVersionParseError => LlvmVersionError,
426+
}
427+
428+
/// The result type for this crate.
429+
pub type Result<T, E = Error> = std::result::Result<T, E>;

0 commit comments

Comments
 (0)