Skip to content

Commit

Permalink
Rename and extend ValidateError. (#102)
Browse files Browse the repository at this point in the history
We now need to conduct I/O during `parse_configuration`, which means the current `ValidateError` isn't quite enough.

I've renamed it to `ParseError` (to complement `parse_configuration`), and added a few extra cases.

1. `ParseError`, which denotes the file path and position where parsing failed.
2. `ValidateError`, as before, but including the file path, wrapping the vector in `InvalidNodes` so we can implement `Display`.
3. `IoError`, for when we fail to read a file.
4. `Other`, because I can't think of everything.
  • Loading branch information
SamirTalwar authored Feb 20, 2024
1 parent d0765f8 commit 42b3cfa
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
88 changes: 79 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,26 +14,94 @@ 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(LocatedError),
#[error("error validating configuration: {0}")]
ValidateError(InvalidNodes),
#[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>,
/// An error associated with the position of a single character in a text file.
#[derive(Debug, Clone)]
pub struct LocatedError {
pub file_path: PathBuf,
pub line: usize,
pub column: usize,
pub message: String,
}

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

/// An error associated with a node in a graph structure.
#[derive(Debug, Clone)]
pub struct InvalidNode {
pub file_path: PathBuf,
pub node_path: Vec<KeyOrIndex>,
pub message: String,
}

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

/// A set of invalid nodes.
#[derive(Debug, Clone)]
pub struct InvalidNodes(pub Vec<InvalidNode>);

impl Display for InvalidNodes {
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(())
}
}

/// A segment in a node path, used with [InvalidNode].
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum KeyOrIndex {
Key(String),
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 +270,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 42b3cfa

Please sign in to comment.