simplified write!; generic new functions

This commit is contained in:
Kevin Zimmerman
2023-07-26 19:53:32 -05:00
parent b4ba3b378c
commit dc649aaee3

View File

@@ -37,11 +37,11 @@ pub struct ClientIdentifier {
} }
impl ClientIdentifier { impl ClientIdentifier {
pub fn new(application_name: &str, username: &str, pool_name: &str) -> ClientIdentifier { pub fn new<S: ToString>(application_name: S, username: S, pool_name: S) -> ClientIdentifier {
ClientIdentifier { ClientIdentifier {
application_name: application_name.into(), application_name: application_name.to_string(),
username: username.into(), username: username.to_string(),
pool_name: pool_name.into(), pool_name: pool_name.to_string(),
} }
} }
} }
@@ -63,10 +63,10 @@ pub struct ServerIdentifier {
} }
impl ServerIdentifier { impl ServerIdentifier {
pub fn new(username: &str, database: &str) -> ServerIdentifier { pub fn new<S: ToString>(username: S, database: S) -> ServerIdentifier {
ServerIdentifier { ServerIdentifier {
username: username.into(), username: username.to_string(),
database: database.into(), database: database.to_string(),
} }
} }
} }
@@ -84,41 +84,36 @@ impl std::fmt::Display for ServerIdentifier {
impl std::fmt::Display for Error { impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match &self { match &self {
&Error::ClientSocketError(error, client_identifier) => write!( Error::ClientSocketError(error, client_identifier) => {
f, write!(f, "Error reading {error} from client {client_identifier}",)
"Error reading {} from client {}",
error, client_identifier
),
&Error::ClientGeneralError(error, client_identifier) => {
write!(f, "{} {}", error, client_identifier)
} }
&Error::ClientAuthImpossible(username) => write!( Error::ClientGeneralError(error, client_identifier) => {
write!(f, "{error} {client_identifier}")
}
Error::ClientAuthImpossible(username) => write!(
f, f,
"Client auth not possible, \ "Client auth not possible, \
no cleartext password set for username: {} \ no cleartext password set for username: {username} \
in config and auth passthrough (query_auth) \ in config and auth passthrough (query_auth) \
is not set up.", is not set up."
username
), ),
&Error::ClientAuthPassthroughError(error, client_identifier) => write!( Error::ClientAuthPassthroughError(error, client_identifier) => write!(
f, f,
"No cleartext password set, \ "No cleartext password set, \
and no auth passthrough could not \ and no auth passthrough could not \
obtain the hash from server for {}, \ obtain the hash from server for {client_identifier}, \
the error was: {}", the error was: {error}",
client_identifier, error
), ),
&Error::ServerStartupError(error, server_identifier) => write!( Error::ServerStartupError(error, server_identifier) => write!(
f, f,
"Error reading {} on server startup {}", "Error reading {error} on server startup {server_identifier}",
error, server_identifier,
), ),
&Error::ServerAuthError(error, server_identifier) => { Error::ServerAuthError(error, server_identifier) => {
write!(f, "{} for {}", error, server_identifier,) write!(f, "{error} for {server_identifier}")
} }
// The rest can use Debug. // The rest can use Debug.
err => write!(f, "{:?}", err), err => write!(f, "{err:?}"),
} }
} }
} }