toasty_core/error/invalid_connection_url.rs
1use crate::Error;
2
3/// Error when a database connection URL is malformed or invalid.
4///
5/// This occurs when a connection string cannot be parsed or contains
6/// invalid parameters (e.g., unknown scheme, missing host, bad port).
7#[derive(Debug)]
8pub(super) struct InvalidConnectionUrl {
9 pub(super) message: Box<str>,
10}
11
12impl Error {
13 /// Creates an invalid connection URL error.
14 ///
15 /// # Examples
16 ///
17 /// ```
18 /// use toasty_core::Error;
19 ///
20 /// let err = Error::invalid_connection_url("missing host in connection string");
21 /// assert!(err.is_invalid_connection_url());
22 /// assert_eq!(
23 /// err.to_string(),
24 /// "invalid connection URL: missing host in connection string"
25 /// );
26 /// ```
27 pub fn invalid_connection_url(message: impl Into<String>) -> Error {
28 Error::from(super::ErrorKind::InvalidConnectionUrl(
29 InvalidConnectionUrl {
30 message: message.into().into(),
31 },
32 ))
33 }
34
35 /// Returns `true` if this error is an invalid connection URL error.
36 pub fn is_invalid_connection_url(&self) -> bool {
37 matches!(self.kind(), super::ErrorKind::InvalidConnectionUrl(_))
38 }
39}
40
41impl std::fmt::Display for InvalidConnectionUrl {
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 write!(f, "invalid connection URL: {}", self.message)
44 }
45}