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
/*
 * Copyright (C) 2015 Benjamin Fry <benjaminfry@me.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
use std::error::Error;
use std::fmt;
use std::num;
use std::io;
use std::net::AddrParseError;

use super::DecodeError;
use super::LexerError;
use ::serialize::txt::Token;

pub enum ParseError {
  LexerError(LexerError),
  DecodeError(DecodeError),
  UnexpectedToken(Token),
  OriginIsUndefined,
  RecordTypeNotSpecified,
  RecordNameNotSpecified,
  RecordClassNotSpecified,
  RecordTTLNotSpecified,
  RecordDataNotSpecified,
  SoaAlreadySpecified,
  MissingToken(String),
  IoError(io::Error),
  ParseIntError(num::ParseIntError),
  AddrParseError(AddrParseError),
  CharToIntError(char),
  ParseTimeError(String),
}

impl fmt::Debug for ParseError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    fmt::Display::fmt(&self, f)
  }
}

impl fmt::Display for ParseError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match *self {
      ParseError::LexerError(ref err) => err.fmt(f),
      ParseError::DecodeError(ref err) => err.fmt(f),
      ParseError::UnexpectedToken(ref t) => write!(f, "Unrecognized Token in stream: {:?}", t),
      ParseError::OriginIsUndefined => write!(f, "$ORIGIN was not specified"),
      ParseError::RecordTypeNotSpecified => write!(f, "Record type not specified"),
      ParseError::RecordNameNotSpecified => write!(f, "Record name not specified"),
      ParseError::RecordClassNotSpecified => write!(f, "Record class not specified"),
      ParseError::RecordTTLNotSpecified => write!(f, "Record ttl not specified"),
      ParseError::RecordDataNotSpecified => write!(f, "Record data not specified"),
      ParseError::SoaAlreadySpecified => write!(f, "SOA is already specified"),
      ParseError::MissingToken(ref s) => write!(f, "Token is missing: {}", s),
      ParseError::IoError(ref err) => err.fmt(f),
      ParseError::ParseIntError(ref err) => err.fmt(f),
      ParseError::AddrParseError(ref s) => write!(f, "Could not parse address: {:?}", s),
      ParseError::CharToIntError(c) => write!(f, "Invalid numerical character: {}", c),
      ParseError::ParseTimeError(ref s) => write!(f, "Invalid time string: {}", s),
    }
  }
}

impl Error for ParseError {
  fn description(&self) -> &str {
    match *self {
      ParseError::LexerError(ref err) => err.description(),
      ParseError::DecodeError(ref err) => err.description(),
      ParseError::UnexpectedToken(..) => "Unrecognized Token",
      ParseError::OriginIsUndefined => "$ORIGIN was not specified",
      ParseError::RecordTypeNotSpecified => "Record type not specified",
      ParseError::RecordNameNotSpecified => "Record name not specified",
      ParseError::RecordClassNotSpecified => "Record class not specified",
      ParseError::RecordTTLNotSpecified => "Record ttl not specified",
      ParseError::RecordDataNotSpecified => "Record data not specified",
      ParseError::SoaAlreadySpecified => "SOA is already specified",
      ParseError::MissingToken(..) => "Token is missing",
      ParseError::IoError(ref err) => err.description(),
      ParseError::ParseIntError(ref err) => err.description(),
      ParseError::AddrParseError(..) => "Could not parse address",
      ParseError::CharToIntError(..) => "Invalid numerical character",
      ParseError::ParseTimeError(..) => "Invalid time string",
    }
  }

  fn cause(&self) -> Option<&Error> {
    match *self {
      ParseError::LexerError(ref err) => Some(err),
      ParseError::DecodeError(ref err) => Some(err),
      ParseError::IoError(ref err) => Some(err),
      ParseError::ParseIntError(ref err) => Some(err),
      _ => None,
    }
  }
}

impl From<LexerError> for ParseError {
  fn from(err: LexerError) -> ParseError {
    ParseError::LexerError(err)
  }
}

impl From<DecodeError> for ParseError {
  fn from(err: DecodeError) -> ParseError {
    ParseError::DecodeError(err)
  }
}

impl From<io::Error> for ParseError {
  fn from(err: io::Error) -> ParseError {
    ParseError::IoError(err)
  }
}

impl From<num::ParseIntError> for ParseError {
  fn from(err: num::ParseIntError) -> ParseError {
    ParseError::ParseIntError(err)
  }
}

impl From<AddrParseError> for ParseError {
  fn from(err: AddrParseError) -> ParseError {
    ParseError::AddrParseError(err)
  }
}