-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parse)!: add
proto::extensions::SimpleExtensionUri
parser (#169)
Adds a parser for `proto::extensions::SimpleExtensionUri` that parses the uri and adds the extension to the parse context. Breaking change because the parse `Context` trait gets two new functions to support this: - `add_simple_extension_uri`: this is used to add a simple extensions, the parse context must directly resolve the uri and return the parsed simple extensions - struct stub for that is added in this PR (the parser is TODO). - `simple_extensions`: given an reference (anchor) to a simple extensions - check if this was added to the parse context and return it
- Loading branch information
Showing
11 changed files
with
405 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Parsing of [proto::extensions] types. | ||
mod simple_extension_uri; | ||
pub use simple_extension_uri::SimpleExtensionUri; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Parsing of [proto::extensions::SimpleExtensionUri]. | ||
use thiserror::Error; | ||
use url::Url; | ||
|
||
use crate::{ | ||
parse::{context::ContextError, Anchor, Context, Parse}, | ||
proto, | ||
}; | ||
|
||
/// A parsed [proto::extensions::SimpleExtensionUri]. | ||
#[derive(Clone, Debug, PartialEq)] | ||
pub struct SimpleExtensionUri { | ||
/// The URI of this simple extension. | ||
uri: Url, | ||
|
||
/// The anchor value of this simple extension. | ||
anchor: Anchor<Self>, | ||
} | ||
|
||
impl SimpleExtensionUri { | ||
/// Returns the uri of this simple extension. | ||
/// | ||
/// See [proto::extensions::SimpleExtensionUri::uri]. | ||
pub fn uri(&self) -> &Url { | ||
&self.uri | ||
} | ||
|
||
/// Returns the anchor value of this simple extension. | ||
/// | ||
/// See [proto::extensions::SimpleExtensionUri::extension_uri_anchor]. | ||
pub fn anchor(&self) -> Anchor<Self> { | ||
self.anchor | ||
} | ||
} | ||
|
||
/// Parse errors for [proto::extensions::SimpleExtensionUri]. | ||
#[derive(Debug, Error, PartialEq)] | ||
pub enum SimpleExtensionUriError { | ||
/// Invalid URI | ||
#[error("invalid URI: {0}")] | ||
InvalidURI(#[from] url::ParseError), | ||
|
||
/// Context error | ||
#[error(transparent)] | ||
Context(#[from] ContextError), | ||
} | ||
|
||
impl<C: Context> Parse<C> for proto::extensions::SimpleExtensionUri { | ||
type Parsed = SimpleExtensionUri; | ||
type Error = SimpleExtensionUriError; | ||
|
||
fn parse(self, ctx: &mut C) -> Result<Self::Parsed, Self::Error> { | ||
let proto::extensions::SimpleExtensionUri { | ||
extension_uri_anchor: anchor, | ||
uri, | ||
} = self; | ||
|
||
// The uri is is required and must be valid. | ||
let uri = Url::parse(&uri)?; | ||
|
||
// Construct the parsed simple extension URI. | ||
let simple_extension_uri = SimpleExtensionUri { | ||
uri, | ||
anchor: Anchor::new(anchor), | ||
}; | ||
|
||
// Make sure the URI is supported by this parse context, resolves and | ||
// parses, and the anchor is unique. | ||
ctx.add_simple_extension_uri(&simple_extension_uri)?; | ||
|
||
Ok(simple_extension_uri) | ||
} | ||
} | ||
|
||
impl From<SimpleExtensionUri> for proto::extensions::SimpleExtensionUri { | ||
fn from(simple_extension_uri: SimpleExtensionUri) -> Self { | ||
let SimpleExtensionUri { uri, anchor } = simple_extension_uri; | ||
proto::extensions::SimpleExtensionUri { | ||
uri: uri.to_string(), | ||
extension_uri_anchor: anchor.into_inner(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::parse::{context::tests::Context, Context as _}; | ||
|
||
#[test] | ||
fn parse() -> Result<(), SimpleExtensionUriError> { | ||
let simple_extension_uri = proto::extensions::SimpleExtensionUri { | ||
extension_uri_anchor: 1, | ||
uri: "https://substrait.io".to_string(), | ||
}; | ||
let simple_extension_uri = simple_extension_uri.parse(&mut Context::default())?; | ||
assert_eq!(simple_extension_uri.anchor(), Anchor::new(1)); | ||
assert_eq!(simple_extension_uri.uri().as_str(), "https://substrait.io/"); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn invalid_uri() { | ||
let simple_extension_uri = proto::extensions::SimpleExtensionUri::default(); | ||
assert_eq!( | ||
simple_extension_uri.parse(&mut Context::default()), | ||
Err(SimpleExtensionUriError::InvalidURI( | ||
url::ParseError::RelativeUrlWithoutBase | ||
)) | ||
); | ||
let simple_extension_uri = proto::extensions::SimpleExtensionUri { | ||
extension_uri_anchor: 1, | ||
uri: "http://".to_string(), | ||
}; | ||
assert_eq!( | ||
simple_extension_uri.parse(&mut Context::default()), | ||
Err(SimpleExtensionUriError::InvalidURI( | ||
url::ParseError::EmptyHost | ||
)) | ||
); | ||
} | ||
|
||
#[test] | ||
fn duplicate_simple_extension() { | ||
let mut ctx = Context::default(); | ||
let simple_extension_uri = proto::extensions::SimpleExtensionUri { | ||
extension_uri_anchor: 1, | ||
uri: "https://substrait.io".to_string(), | ||
}; | ||
assert!(ctx.parse(simple_extension_uri.clone()).is_ok()); | ||
assert_eq!( | ||
ctx.parse(simple_extension_uri), | ||
Err(SimpleExtensionUriError::Context( | ||
ContextError::DuplicateSimpleExtension(Anchor::new(1)) | ||
)) | ||
); | ||
} | ||
|
||
#[test] | ||
fn unsupported_uri() { | ||
let simple_extension_uri = proto::extensions::SimpleExtensionUri { | ||
extension_uri_anchor: 1, | ||
uri: "ftp://substrait.io".to_string(), | ||
}; | ||
assert_eq!( | ||
simple_extension_uri.parse(&mut Context::default()), | ||
Err(SimpleExtensionUriError::Context( | ||
ContextError::UnsupportedURI("`ftp` scheme not supported".to_string()) | ||
)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Parsing of [text](crate::text) types. | ||
pub mod simple_extensions; |
Oops, something went wrong.