Skip to content

Commit

Permalink
Rename and extend ValidateError.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirTalwar committed Feb 16, 2024
1 parent 660750a commit 5ecdbf1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
81 changes: 72 additions & 9 deletions rust-connector-sdk/src/connector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{error::Error, path::Path};
use std::error::Error;
use std::fmt::Display;
use std::path::{Path, PathBuf};

use async_trait::async_trait;
use ndc_client::models;
Expand All @@ -12,17 +14,69 @@ pub mod example;
/// Errors which occur when trying to validate connector
/// configuration.
///
/// See [`Connector::validate_raw_configuration`].
/// See [`Connector::parse_configuration`].
#[derive(Debug, Error)]
pub enum ValidateError {
#[error("error validating configuration: {0:?}")]
ValidateError(Vec<InvalidRange>),
pub enum ParseError {
#[error("error parsing configuration: {0}")]
ParseError(Position),
#[error("error validating configuration: {0}")]
ValidateError(InvalidPaths),
#[error("error processing configuration: {0}")]
IoError(#[from] std::io::Error),
#[error("error processing configuration: {0}")]
Other(#[from] Box<dyn Error + Send + Sync>),
}

#[derive(Debug, Clone, Serialize)]
pub struct InvalidRange {
pub path: Vec<KeyOrIndex>,
/// The position of a single character in a text file.
#[derive(Debug, Clone)]
pub struct Position {
pub path: PathBuf,
pub line: usize,
pub column: usize,
}

impl Display for Position {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{0}:{1}:{2}",
self.path.display(),
self.line,
self.column
)
}
}

#[derive(Debug, Clone)]
pub struct InvalidPath {
pub message: String,
pub path: Vec<KeyOrIndex>,
}

impl Display for InvalidPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} at ", self.message)?;
for segment in &self.path {
write!(f, ".{}", segment)?;
}
Ok(())
}
}

#[derive(Debug, Clone)]
pub struct InvalidPaths(pub Vec<InvalidPath>);

impl Display for InvalidPaths {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut iterator = self.0.iter();
if let Some(first) = iterator.next() {
first.fmt(f)?;
}
for next in iterator {
write!(f, ", {next}")?;
}
Ok(())
}
}

#[derive(Debug, Clone, Serialize)]
Expand All @@ -32,6 +86,15 @@ pub enum KeyOrIndex {
Index(u32),
}

impl Display for KeyOrIndex {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Key(key) => write!(f, "[{key:?}]"),
Self::Index(index) => write!(f, "[{index}]"),
}
}
}

/// Errors which occur when trying to initialize connector
/// state.
///
Expand Down Expand Up @@ -200,7 +263,7 @@ pub trait Connector {
/// returning a configuration error or a validated [`Connector::Configuration`].
async fn parse_configuration(
configuration_dir: impl AsRef<Path> + Send,
) -> Result<Self::Configuration, ValidateError>;
) -> Result<Self::Configuration, ParseError>;

/// Initialize the connector's in-memory state.
///
Expand Down
2 changes: 1 addition & 1 deletion rust-connector-sdk/src/connector/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Connector for Example {

async fn parse_configuration(
_configuration_dir: impl AsRef<Path> + Send,
) -> Result<Self::Configuration, ValidateError> {
) -> Result<Self::Configuration, ParseError> {
Ok(())
}

Expand Down

0 comments on commit 5ecdbf1

Please sign in to comment.