Skip to content

Commit bbd2247

Browse files
authored
Minor updates to match CLI (#406)
Some minor API issues showed up when retargeting CLI to the published versions of this crate.
1 parent 8e11e2d commit bbd2247

File tree

7 files changed

+163
-86
lines changed

7 files changed

+163
-86
lines changed

gel-dsn/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "gel-dsn"
33
license = "MIT/Apache-2.0"
4-
version = "0.1.0"
4+
version = "0.1.1"
55
authors = ["MagicStack Inc. <[email protected]>"]
66
edition = "2021"
77
description = "Data-source name (DSN) parser for Gel and PostgreSQL databases."

gel-dsn/src/gel/config.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,17 @@ impl TryInto<Params> for ConnectionOptions {
658658
}
659659

660660
if self.tls_ca.is_some() && self.tls_ca_file.is_some() {
661-
return Err(ParseError::ExclusiveOptions);
661+
return Err(ParseError::ExclusiveOptions(
662+
"tls_ca".to_string(),
663+
"tls_ca_file".to_string(),
664+
));
662665
}
663666

664667
if self.branch.is_some() && self.database.is_some() {
665-
return Err(ParseError::ExclusiveOptions);
668+
return Err(ParseError::ExclusiveOptions(
669+
"branch".to_string(),
670+
"database".to_string(),
671+
));
666672
}
667673

668674
let mut credentials = Param::from_file(self.credentials_file.clone());

gel-dsn/src/gel/env.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{
2-
error::*, BuildContext, ClientSecurity, CloudCerts, FromParamStr, InstanceName, TlsSecurity,
2+
error::*, BuildContext, ClientSecurity, CloudCerts, FromParamStr, InstanceName, ParamSource,
3+
TlsSecurity,
34
};
45
use crate::host::HostType;
56
use crate::EnvVar;
@@ -186,7 +187,7 @@ pub fn get_envs(
186187
Err(std::env::VarError::NotPresent) => continue,
187188
Err(err @ std::env::VarError::NotUnicode(_)) => {
188189
return Err(ParseError::EnvNotFound(
189-
EnvironmentSource::Explicit,
190+
ParamSource::Explicit,
190191
err.to_string(),
191192
));
192193
}

gel-dsn/src/gel/error.rs

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::host::HostParseError;
22
use std::{convert::Infallible, num::ParseIntError};
33

4+
use super::ParamSource;
5+
46
#[derive(Debug, Clone, PartialEq, Eq, derive_more::Display, PartialOrd, Ord)]
57
pub enum CompoundSource {
68
Dsn,
@@ -46,22 +48,14 @@ pub enum InvalidDsnError {
4648
BranchAndDatabase,
4749
}
4850

49-
#[derive(Debug, Clone, PartialEq, Eq, derive_more::Display, PartialOrd, Ord)]
50-
pub enum EnvironmentSource {
51-
#[display("Explicit")]
52-
Explicit,
53-
#[display("Param::Env")]
54-
Param,
55-
}
56-
5751
#[derive(Debug, derive_more::Error, derive_more::Display, PartialEq, Eq, PartialOrd, Ord)]
5852
pub enum ParseError {
5953
#[display("Credentials file not found")]
6054
CredentialsFileNotFound,
61-
#[display("Environment variable not found: {_1} ({_0})")]
62-
EnvNotFound(EnvironmentSource, #[error(not(source))] String),
63-
#[display("Exclusive options")]
64-
ExclusiveOptions,
55+
#[display("Environment variable was not set: {_1} (from {_0})")]
56+
EnvNotFound(ParamSource, #[error(not(source))] String),
57+
#[display("{_0} and {_1} are mutually exclusive and cannot be used together")]
58+
ExclusiveOptions(String, String),
6559
#[display("File not found")]
6660
FileNotFound,
6761
#[display("Invalid credentials file: {_0}")]
@@ -94,7 +88,7 @@ pub enum ParseError {
9488
MultipleCompoundOpts(#[error(not(source))] Vec<CompoundSource>),
9589
#[display("No options or .toml file")]
9690
NoOptionsOrToml,
97-
#[display("Project not initialised")]
91+
#[display("Project not initialized")]
9892
ProjectNotInitialised,
9993
#[display("Secret key not found")]
10094
SecretKeyNotFound,
@@ -107,7 +101,7 @@ impl ParseError {
107101
match self {
108102
Self::EnvNotFound(..) => "env_not_found",
109103
Self::CredentialsFileNotFound => "credentials_file_not_found",
110-
Self::ExclusiveOptions => "exclusive_options",
104+
Self::ExclusiveOptions(..) => "exclusive_options",
111105
Self::FileNotFound => "file_not_found",
112106
Self::InvalidCredentialsFile(_) => "invalid_credentials_file",
113107
Self::InvalidDatabase => "invalid_database",
@@ -136,7 +130,6 @@ impl ParseError {
136130
match self {
137131
Self::EnvNotFound(..)
138132
| Self::CredentialsFileNotFound
139-
| Self::ExclusiveOptions
140133
| Self::FileNotFound
141134
| Self::InvalidCredentialsFile(_)
142135
| Self::InvalidDatabase
@@ -150,15 +143,24 @@ impl ParseError {
150143
| Self::InvalidUser
151144
| Self::InvalidCertificate
152145
| Self::InvalidDuration
153-
| Self::MultipleCompoundEnv(_)
154-
| Self::MultipleCompoundOpts(_)
155-
| Self::NoOptionsOrToml
156-
| Self::ProjectNotInitialised
157146
| Self::UnixSocketUnsupported => {
147+
// The argument is invalid
148+
gel_errors::InvalidArgumentError::with_source(self)
149+
}
150+
Self::MultipleCompoundEnv(_)
151+
| Self::MultipleCompoundOpts(_)
152+
| Self::ExclusiveOptions(..) => {
153+
// The argument is valid, but the use is invalid
154+
gel_errors::InterfaceError::with_source(self)
155+
}
156+
Self::NoOptionsOrToml | Self::ProjectNotInitialised => {
157+
// Credentials are missing
158158
gel_errors::ClientNoCredentialsError::with_source(self)
159159
}
160-
161-
Self::SecretKeyNotFound => gel_errors::NoCloudConfigFound::with_source(self),
160+
Self::SecretKeyNotFound => {
161+
// Required cloud configuration is missing
162+
gel_errors::NoCloudConfigFound::with_source(self)
163+
}
162164
}
163165
}
164166
}

gel-dsn/src/gel/param.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,23 @@ pub enum Param<T: Clone> {
152152
/// Unparsed value.
153153
Unparsed(String),
154154
/// Value from given environment variable.
155-
Env(String),
155+
Env(ParamSource, String),
156156
/// Value from given file.
157157
File(PathBuf),
158158
/// Value from environment variable pointing to a file.
159-
EnvFile(String),
159+
EnvFile(ParamSource, String),
160160
/// Parsed value.
161161
Parsed(T),
162162
}
163163

164+
#[derive(Debug, Copy, Clone, derive_more::Display, PartialEq, Eq, PartialOrd, Ord)]
165+
pub enum ParamSource {
166+
#[display("DSN")]
167+
Dsn,
168+
#[display("explicit")]
169+
Explicit,
170+
}
171+
164172
#[allow(private_bounds)]
165173
impl<T: Clone> Param<T>
166174
where
@@ -199,9 +207,9 @@ where
199207
match self {
200208
Self::None => Ok(Param::None),
201209
Self::Unparsed(value) => Ok(Param::Unparsed(value)),
202-
Self::Env(value) => Ok(Param::Env(value)),
210+
Self::Env(source, value) => Ok(Param::Env(source, value)),
203211
Self::File(value) => Ok(Param::File(value)),
204-
Self::EnvFile(value) => Ok(Param::EnvFile(value)),
212+
Self::EnvFile(source, value) => Ok(Param::EnvFile(source, value)),
205213
Self::Parsed(value) => Err(Self::Parsed(value)),
206214
}
207215
}
@@ -212,15 +220,13 @@ where
212220
return Ok(None);
213221
}
214222
Self::Unparsed(value) => value.clone(),
215-
Self::Env(key) => {
216-
context_trace!(context, "Reading env: {key}");
223+
Self::Env(source, key) => {
224+
context_trace!(context, "Reading env: {key} (from {source})");
217225
context
218226
.env()
219227
.read(key)
220228
.map(|s| s.to_string())
221-
.map_err(|_| {
222-
ParseError::EnvNotFound(EnvironmentSource::Param, key.to_string())
223-
})?
229+
.map_err(|_| ParseError::EnvNotFound(*source, key.to_string()))?
224230
}
225231
Self::File(path) => {
226232
context_trace!(context, "Reading file: {path:?}");
@@ -232,14 +238,12 @@ where
232238
context_trace!(context, "File content: {res:?}");
233239
res?
234240
}
235-
Self::EnvFile(key) => {
236-
context_trace!(context, "Reading env for file: {key}");
241+
Self::EnvFile(source, key) => {
242+
context_trace!(context, "Reading env for file: {key} (from {source})");
237243
let env = context
238244
.env()
239245
.read(key)
240-
.map_err(|_| {
241-
ParseError::EnvNotFound(EnvironmentSource::Param, key.to_string())
242-
})?
246+
.map_err(|_| ParseError::EnvNotFound(*source, key.to_string()))?
243247
.to_string();
244248
context_trace!(context, "Reading file: {env}");
245249
let res = context

0 commit comments

Comments
 (0)