diff --git a/rust-connector-sdk/src/connector.rs b/rust-connector-sdk/src/connector.rs index 0c4488c9..4fca1bdf 100644 --- a/rust-connector-sdk/src/connector.rs +++ b/rust-connector-sdk/src/connector.rs @@ -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; @@ -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), +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), } -#[derive(Debug, Clone, Serialize)] -pub struct InvalidRange { - pub path: Vec, +/// 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, +} + +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); + +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)] @@ -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. /// @@ -200,7 +263,7 @@ pub trait Connector { /// returning a configuration error or a validated [`Connector::Configuration`]. async fn parse_configuration( configuration_dir: impl AsRef + Send, - ) -> Result; + ) -> Result; /// Initialize the connector's in-memory state. /// diff --git a/rust-connector-sdk/src/connector/example.rs b/rust-connector-sdk/src/connector/example.rs index 0a19db29..d55f23af 100644 --- a/rust-connector-sdk/src/connector/example.rs +++ b/rust-connector-sdk/src/connector/example.rs @@ -16,7 +16,7 @@ impl Connector for Example { async fn parse_configuration( _configuration_dir: impl AsRef + Send, - ) -> Result { + ) -> Result { Ok(()) }