ch8_isa/error/
binary_error.rs

1/*
2 * binary_error.rs
3 * Defines a struct that holds error data from binary creation
4 * Created on 12/6/2019
5 * Created by Andrew Davis
6 *
7 * Copyright (C) 2019  Andrew Davis
8 *
9 * This program is free software: you can redistribute it and/or modify   
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 * 
19 * You should have received a copy of the GNU General Public License
20 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23//usage statements
24use std::fmt;
25use super::BinaryErrorType;
26
27/// A possible error resulting from attempted binary creation
28pub struct BinaryError {
29    /// The error type 
30    error_type: BinaryErrorType,
31
32    /// The filename of the binary  
33    binary_name: String,
34}
35
36//implementation
37impl BinaryError {
38    /// Constructs a new `BinaryError` instance
39    ///
40    /// # Arguments
41    ///
42    /// * `new_type` - The type of the error
43    /// * `new_name` - The name of the binary
44    /// 
45    /// # Returns
46    ///
47    /// A new `BinaryError` instance with the given properties
48    pub fn new(new_type: BinaryErrorType, new_name: &str) 
49        -> BinaryError {
50        return BinaryError {
51            error_type: new_type,
52            binary_name: String::from(new_name)
53        };
54    }
55
56    /// Gets the error type
57    /// 
58    /// # Returns
59    ///
60    /// The error type
61    pub fn get_type(&self) -> BinaryErrorType {
62        return self.error_type.clone();
63    }
64
65    /// Gets the binary name 
66    ///
67    /// # Returns
68    ///
69    /// The name of the binary that produced an error
70    pub fn get_binary_name(&self) -> &str {
71        return self.binary_name.as_str();
72    }
73}
74
75//Display implementation
76impl fmt::Display for BinaryError {
77    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78        write!(f, "An error occurred creating the Chip-8 binary: {}",
79               self.error_type)
80    }
81}
82
83//Debug implementation
84impl fmt::Debug for BinaryError {
85    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86        write!(f, "{{ file: {}, line: {}, error: {} }}", 
87               file!(), line!(), self.error_type)
88    }
89}
90
91//end of file