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
// Copyright 2018-2021 Cargill Incorporated // // 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. //! Module containing InvalidStateError implementation. use std::error; use std::fmt; /// An error returned when an operation cannot be completed because the state of the underlying /// struct is inconsistent. /// /// This can be caused by a caller when a sequence of functions is called in a way that results in /// a state which is inconsistent. /// /// This usually indicates a programming error on behalf of the caller. #[derive(Debug)] pub struct InvalidStateError { message: String, } impl InvalidStateError { /// Constructs a new `InvalidStateError` with a specified message string. /// /// The implementation of `std::fmt::Display` for this error will be the message string /// provided. /// /// # Examples /// /// ``` /// use grid_sdk::error::InvalidStateError; /// /// let invalid_state_error = InvalidStateError::with_message("oops".to_string()); /// assert_eq!(format!("{}", invalid_state_error), "oops"); /// ``` pub fn with_message(message: String) -> Self { Self { message } } } impl error::Error for InvalidStateError {} impl fmt::Display for InvalidStateError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", &self.message) } } #[cfg(test)] pub mod tests { use super::*; /// Tests that error constructed with `InvalidStateError::with_message` return message as the /// display string. #[test] fn test_display_with_message() { let msg = "test message"; let err = InvalidStateError::with_message(msg.to_string()); assert_eq!(format!("{}", err), msg); } }