From acd25fcd571a0f5d54075c7c894ad1e59297d563 Mon Sep 17 00:00:00 2001 From: Hyeon Kim Date: Sun, 29 Sep 2024 00:05:43 +0900 Subject: [PATCH] error.rs: Deprecate LoadError::new() --- obj-rs/src/error.rs | 19 ++++++++++++++----- obj-rs/src/raw/lexer.rs | 4 ++-- obj-rs/src/raw/object.rs | 4 ++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/obj-rs/src/error.rs b/obj-rs/src/error.rs index d0dcb02..fedd193 100644 --- a/obj-rs/src/error.rs +++ b/obj-rs/src/error.rs @@ -1,6 +1,5 @@ //! Contains helper structs for error handling -use std::convert::From; use std::error::Error; use std::fmt; use std::io; @@ -100,10 +99,18 @@ pub enum LoadErrorKind { impl LoadError { /// Creates a new custom error from a specified kind and message. + #[deprecated( + since = "0.7.4", + note = "You shouldn’t need to create a LoadError instance on your own." + )] pub fn new(kind: LoadErrorKind, message: &'static str) -> Self { let message = message.to_string(); LoadError { kind, message } } + + pub(crate) fn new_internal(kind: LoadErrorKind, message: String) -> Self { + LoadError { kind, message } + } } impl Error for LoadError {} @@ -129,10 +136,12 @@ impl fmt::Display for LoadError { macro_rules! make_error { ($kind:ident, $message:expr) => { - return Err(::std::convert::From::from($crate::error::LoadError::new( - $crate::error::LoadErrorKind::$kind, - $message, - ))) + return Err($crate::error::ObjError::Load( + $crate::error::LoadError::new_internal( + $crate::error::LoadErrorKind::$kind, + $message.to_string(), + ), + )) }; } diff --git a/obj-rs/src/raw/lexer.rs b/obj-rs/src/raw/lexer.rs index f07a0ca..b137477 100644 --- a/obj-rs/src/raw/lexer.rs +++ b/obj-rs/src/raw/lexer.rs @@ -67,9 +67,9 @@ impl Iterator for Lexer { let line; match self.stripped_lines.next() { None => { - return Some(Err(ObjError::Load(LoadError::new( + return Some(Err(ObjError::Load(LoadError::new_internal( LoadErrorKind::BackslashAtEOF, - "Expected a line, but met an EOF", + "Expected a line, but met an EOF".to_string(), )))) } Some(Err(e)) => return Some(Err(ObjError::Io(e))), diff --git a/obj-rs/src/raw/object.rs b/obj-rs/src/raw/object.rs index 0e0382b..a22db41 100644 --- a/obj-rs/src/raw/object.rs +++ b/obj-rs/src/raw/object.rs @@ -51,9 +51,9 @@ fn try_index(collection: &[T], input: &str) -> ObjResult { use crate::error::{LoadError, LoadErrorKind, ObjError}; let len: isize = collection.len().try_into().map_err(|_| { - ObjError::Load(LoadError::new( + ObjError::Load(LoadError::new_internal( LoadErrorKind::IndexOutOfRange, - "Too many items in collection", + "Too many items in collection".to_string(), )) })?;