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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
//! Arguments passed to various operations.
use std::{fmt, marker::PhantomData};
use crate::common::*;
use crate::data_streams::DataFormat;
use crate::separator::Separator;
/// Trait used to add new methods to `EnumSet`.
pub(crate) trait EnumSetExt<T: EnumSetType> {
/// Display this `EnumSet` using custom pretty-printing. This requires a
/// wrapper type because we can't define [`fmt::Display`] directly on
/// `EnumSet`, because it was defined in a different crate.
fn display(self) -> DisplayEnumSet<T>;
}
impl<T: EnumSetType> EnumSetExt<T> for EnumSet<T> {
fn display(self) -> DisplayEnumSet<T> {
DisplayEnumSet(self)
}
}
/// A wrapper which allows us to perform custom pretty-printing of an `EnumSet`
/// subtype. Created using [`EnumSetExt::display`].
pub(crate) struct DisplayEnumSet<T: EnumSetType>(pub EnumSet<T>);
/// This is a marker trait used by `SharedArguments`, `SourceArguments` and
/// `DestinationArguments`. We use it to keep track whether or not the arguments
/// have been verified against a driver's `Features` list.
///
/// This is used to implement the [type state][] pattern.
///
/// [type state]: http://cliffle.com/blog/rust-typestate/
pub trait ArgumentState: Clone {}
/// This is used to mark an `*Arguments` structure that has not yet been
/// verified for compatibility with a specific driver. See the [type state][]
/// pattern for details.
///
/// [type state]: http://cliffle.com/blog/rust-typestate/
#[derive(Clone)]
pub enum Unverified {}
impl ArgumentState for Unverified {}
/// This is used to mark an `*Arguments` structure that has not yet been
/// verified for compatibility with a specific driver. See the [type state][]
/// pattern for details.
///
/// [type state]: http://cliffle.com/blog/rust-typestate/
#[derive(Clone)]
pub enum Verified {}
impl ArgumentState for Verified {}
/// Arguments used by both the data source and destination.
#[derive(Clone, Debug)]
pub struct SharedArguments<S: ArgumentState> {
/// The portable data schema describing the table we're transfering.
schema: Schema,
/// Various locations that can be used to store temporary data during
/// the transfer.
temporary_storage: TemporaryStorage,
/// How many streams should we process at once?
max_streams: usize,
/// We need to include a reference to `ArgumentState` somewhere, so use a
/// 0-byte phantom value.
_phantom: PhantomData<S>,
}
impl<S: ArgumentState> SharedArguments<S> {
/// How many concurrent data streams should we attempt to process at once?
///
/// This is available even for unvalidated arguments because it's used by
/// our top-level code as well as some of the individual drivers.
pub fn max_streams(&self) -> usize {
self.max_streams
}
}
// These methods are only available in the `Unverified` state.
impl SharedArguments<Unverified> {
/// Create a new `SharedArguments` structure.
pub fn new(
schema: Schema,
temporary_storage: TemporaryStorage,
max_streams: usize,
) -> Self {
Self {
schema,
temporary_storage,
max_streams,
_phantom: PhantomData,
}
}
/// Verify that this structure only contains supported arguments. This uses
/// the [type state][] pattern to keep track of whether our arguments have
/// been verified to be supported.
///
/// [type state]: http://cliffle.com/blog/rust-typestate/
pub fn verify(self, _features: Features) -> Result<SharedArguments<Verified>> {
// TODO: We do not currently require verification for any of our fields.
Ok(SharedArguments {
schema: self.schema,
temporary_storage: self.temporary_storage,
max_streams: self.max_streams,
_phantom: PhantomData,
})
}
}
// These methods are only available in the `Verified` state.
impl SharedArguments<Verified> {
/// Get the table scheme used for this transfer.
pub fn schema(&self) -> &Schema {
&self.schema
}
/// Get the temporary storage available for use by this transfer.
pub fn temporary_storage(&self) -> &TemporaryStorage {
&self.temporary_storage
}
}
/// What `SourceArguments` features are supported by a given driver?
#[derive(Debug, EnumSetType)]
pub enum SourceArgumentsFeatures {
DriverArgs,
Format,
WhereClause,
}
impl fmt::Display for DisplayEnumSet<SourceArgumentsFeatures> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut sep = Separator::new(" ");
if self.0.contains(SourceArgumentsFeatures::DriverArgs) {
write!(f, "{}--from-arg=$NAME=$VALUE", sep.display())?;
}
if self.0.contains(SourceArgumentsFeatures::Format) {
write!(f, "{}--format=$FORMAT", sep.display())?;
}
if self.0.contains(SourceArgumentsFeatures::WhereClause) {
write!(f, "{}--where=$SQL_EXPR", sep.display())?;
}
Ok(())
}
}
/// Data source arguments.
#[derive(Clone, Debug, Default)]
pub struct SourceArguments<ArgumentState> {
/// Driver-specific arguments for our data source.
driver_args: DriverArguments,
/// The format to convert from, if applicable.
format: Option<DataFormat>,
/// A `WHERE` clause for this query.
where_clause: Option<String>,
/// We need to include a reference to `ArgumentState` somewhere, so use a
/// 0-byte phantom value.
_phantom: PhantomData<ArgumentState>,
}
// These methods are only available in the `Unverified` state.
impl SourceArguments<Unverified> {
/// Construct a new `SourceArguments`.
pub fn new(
driver_args: DriverArguments,
format: Option<DataFormat>,
where_clause: Option<String>,
) -> Self {
Self {
driver_args,
format,
where_clause,
_phantom: PhantomData,
}
}
/// Construct a new `SourceArguments` with typical values for a temporary
/// storage location.
pub fn for_temporary() -> Self {
Self::new(DriverArguments::default(), None, None)
}
/// Set the format to CSV, returning an error if it was already set. This
/// is used by the `csv:` driver for backwards compatibility.
pub fn with_format_csv(mut self) -> Result<Self> {
if self.format.is_some() {
return Err(format_err!(
"--from-format cannot be specified with this driver"
));
}
self.format = Some(DataFormat::Csv);
Ok(self)
}
/// Verify that this structure only contains supported arguments. This uses
/// the [type state][] pattern to keep track of whether our arguments have
/// been verified to be supported.
///
/// [type state]: http://cliffle.com/blog/rust-typestate/
pub fn verify(self, features: Features) -> Result<SourceArguments<Verified>> {
if !features
.source_args
.contains(SourceArgumentsFeatures::DriverArgs)
&& !self.driver_args.is_empty()
{
return Err(format_err!("this data source does not support --from-args"));
}
if !features
.source_args
.contains(SourceArgumentsFeatures::Format)
&& self.format.is_some()
{
return Err(format_err!(
"this data source does not support --from-format"
));
}
if !features
.source_args
.contains(SourceArgumentsFeatures::WhereClause)
&& self.where_clause.is_some()
{
return Err(format_err!("this data source does not support --where"));
}
Ok(SourceArguments {
driver_args: self.driver_args,
format: self.format,
where_clause: self.where_clause,
_phantom: PhantomData,
})
}
}
// These methods are only available in the `Verified` state.
impl SourceArguments<Verified> {
/// Driver-specific arguments for our data source.
pub fn driver_args(&self) -> &DriverArguments {
&self.driver_args
}
/// The format for data associated with this locator, if applicable.
pub fn format(&self) -> Option<&DataFormat> {
self.format.as_ref()
}
/// A `WHERE` clause for this query.
pub fn where_clause(&self) -> Option<&str> {
self.where_clause.as_ref().map(|s| &s[..])
}
}
/// What `DestinationArguments` features are supported by a given driver?
#[derive(Debug, EnumSetType)]
pub enum DestinationArgumentsFeatures {
DriverArgs,
Format,
}
impl fmt::Display for DisplayEnumSet<DestinationArgumentsFeatures> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut sep = Separator::new(" ");
if self.0.contains(DestinationArgumentsFeatures::DriverArgs) {
write!(f, "{}--to-arg=$NAME=$VALUE", sep.display())?;
}
if self.0.contains(DestinationArgumentsFeatures::Format) {
write!(f, "{}--format=$FORMAT", sep.display())?;
}
Ok(())
}
}
/// Data destination arguments.
#[derive(Clone, Debug, Default)]
pub struct DestinationArguments<ArgumentState> {
/// Driver-specific arguments for our data destination.
driver_args: DriverArguments,
/// What to do it the destination already exists.
if_exists: IfExists,
/// The format we're converting to, if applicable.
format: Option<DataFormat>,
/// We need to include a reference to `ArgumentState` somewhere, so use a
/// 0-byte phantom value.
_phantom: PhantomData<ArgumentState>,
}
// These methods are only available in the `Unverified` state.
impl DestinationArguments<Unverified> {
/// Construct a new `DestinationArguments`.
pub fn new(
driver_args: DriverArguments,
format: Option<DataFormat>,
if_exists: IfExists,
) -> Self {
DestinationArguments {
driver_args,
format,
if_exists,
_phantom: PhantomData,
}
}
/// Construct a new `DestinationArguments` with typical values for a
/// temporary storage location.
pub fn for_temporary() -> Self {
Self::new(DriverArguments::default(), None, IfExists::Overwrite)
}
/// Set the format to CSV, returning an error if it was already set. This
/// is used by the `csv:` driver for backwards compatibility.
pub fn with_format_csv(mut self) -> Result<Self> {
if self.format.is_some() {
return Err(format_err!(
"--from-format cannot be specified with this driver"
));
}
self.format = Some(DataFormat::Csv);
Ok(self)
}
/// Verify that this structure only contains supported arguments. This uses
/// the [type state][] pattern to keep track of whether our arguments have
/// been verified to be supported.
///
/// [type state]: http://cliffle.com/blog/rust-typestate/
pub fn verify(self, features: Features) -> Result<DestinationArguments<Verified>> {
if !features
.dest_args
.contains(DestinationArgumentsFeatures::DriverArgs)
&& !self.driver_args.is_empty()
{
return Err(format_err!(
"this data destination does not support --to-args"
));
}
if !features
.dest_args
.contains(DestinationArgumentsFeatures::Format)
&& self.format.is_some()
{
return Err(format_err!(
"this data destination does not support --to-format"
));
}
self.if_exists.verify(features.dest_if_exists)?;
Ok(DestinationArguments {
driver_args: self.driver_args,
if_exists: self.if_exists,
format: self.format,
_phantom: PhantomData,
})
}
}
// These methods are only available in the `Verified` state.
impl DestinationArguments<Verified> {
/// Driver-specific arguments for our data destination.
pub fn driver_args(&self) -> &DriverArguments {
&self.driver_args
}
/// The format for data associated with this locator, if applicable.
pub fn format(&self) -> Option<&DataFormat> {
self.format.as_ref()
}
/// What to do it the destination already exists.
pub fn if_exists(&self) -> &IfExists {
&self.if_exists
}
}