Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/parse/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ pub trait Context {
&self,
anchor: &Anchor<SimpleExtensionUri>,
) -> Result<&SimpleExtensions, ContextError>;

/// Add a [ExtensionFunction] to this context. Must return an error for duplicate
/// anchors, when the URI is not supported or if the definition does not contain a matching function with the given name.
///
/// This function must eagerly resolve and parse the simple extension, returning an
/// error if either fails.
fn add_extension_function(
&mut self,
extension_function: &ExtensionFunction,
) -> Result<&SimpleExtensions, ContextError>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return types and comments of these functions should be updated.


/// Add an [ExtensionType] to this context. Must return an error for duplicate
/// anchors, when the URI is not supported or if the definition does not contain a matching function with the given name.
///
/// This function must eagerly resolve and parse the simple extension, returning an
/// error if either fails.
fn add_extension_type(
&mut self,
extension: &ExtensionType,
) -> Result<&SimpleExtensions, ContextError>;

/// Add a [ExtensionTypeVariation] to this context. Must return an error for duplicate
/// anchors, when the URI is not supported or if the definition does not contain a matching function with the given name.
///
/// This function must eagerly resolve and parse the simple extension, returning an
/// error if either fails.
fn add_extension_type_variation(
&mut self,
extension_type_variation: &ExtensionTypeVariation,
) -> Result<&SimpleExtensions, ContextError>;
}

/// Parse context errors.
Expand Down
2 changes: 2 additions & 0 deletions src/parse/proto/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

//! Parsing of [proto::extensions] types.

mod simple_extension_declaration;
mod simple_extension_uri;

pub use simple_extension_uri::SimpleExtensionUri;
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use super::simple_extension_declaration::SimpleExtensionDeclarationError;
use crate::{
parse::{Anchor, Context, Parse},
proto,
};

#[derive(Clone, Debug, PartialEq)]
pub struct ExtensionFunction {
anchor: Anchor<Self>,
name: Name<Self>,
extension_uri_reference: Anchor<SimpleExtensionUri>,
}

impl<C: Context> Parse<C> for proto::extensions::simple_extension_declaration::ExtensionFunction {
type Parsed = ExtensionFunction;
type Error = SimpleExtensionDeclarationError;

fn parse(self, _ctx: &mut C) -> Result<Self::Parsed, Self::Error> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parser should add the function to the context, checking that the referenced extension uri is valid, the used anchor is unique, and that the definition contains a matching function with the given name in the referenced extension.

let proto::extensions::simple_extension_declaration::ExtensionFunction {
extension_uri_reference,
function_anchor,
name,
} = self;

// Construct the parsed ExtensionFunction.
let extension_function = ExtensionFunction {
extension_uri_reference: Anchor::new(extension_uri_reference),
name,
anchor: Anchor::new(function_anchor),
};

// Add the ExtensionFunction to the given context.
ctx.add_extension_function(&extension_function)?;

Ok(extension_function)
}
}

impl From<ExtensionFunction>
for proto::extensions::simple_extension_declaration::ExtensionFunction
{
fn from(value: ExtensionFunction) -> Self {
let ExtensionFunction {
anchor,
name,
extension_uri_reference,
} = value;

Self {
extension_uri_reference: extension_uri_reference.into_inner(),
function_anchor: anchor.into_inner(),
name: name,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::parse::{Anchor, Context, Parse};
use crate::proto;
Comment on lines +3 to +4

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can you merge those?


use super::simple_extension_declaration::SimpleExtensionDeclarationError;

#[derive(Clone, Debug, PartialEq)]
pub struct ExtensionType {
anchor: Anchor<Self>,
name: Name<Self>,
extension_uri_reference: Anchor<SimpleExtensionUri>,
}

impl<C: Context> Parse<C> for proto::extensions::simple_extension_declaration::ExtensionType {
type Parsed = ExtensionType;
type Error = SimpleExtensionDeclarationError;

fn parse(self, _ctx: &mut C) -> Result<Self::Parsed, Self::Error> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parser should also add this declaration to the context.

let proto::extensions::simple_extension_declaration::ExtensionType {
extension_uri_reference,
type_anchor,
name,
} = self;

// Construct the parsed ExtensionType.
let extension_type_variation = ExtensionType {
extension_uri_reference: Anchor::new(extension_uri_reference),
name,
anchor: Anchor::new(type_anchor),
};

// Add the ExtensionType to the given context.
ctx.add_extension_type();

Ok(extension_type_variation)
}
}

impl From<ExtensionType> for proto::extensions::simple_extension_declaration::ExtensionType {
fn from(value: ExtensionType) -> Self {
let ExtensionType {
anchor,
name,
extension_uri_reference,
} = value;

Self {
extension_uri_reference: extension_uri_reference.into_inner(),
type_anchor: anchor.into_inner(),
name: name,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use crate::parse::{Anchor, Context, Parse};
use crate::proto;
Comment on lines +3 to +4

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can you merge those?


use super::simple_extension_declaration::SimpleExtensionDeclarationError;

#[derive(Clone, Debug, PartialEq)]
pub struct ExtensionTypeVariation {
anchor: Anchor<Self>,
name: Name<Self>,
extension_uri_reference: Anchor<SimpleExtensionUri>,
}

impl<C: Context> Parse<C>
for proto::extensions::simple_extension_declaration::ExtensionTypeVariation
{
type Parsed = ExtensionTypeVariation;
type Error = SimpleExtensionDeclarationError;

fn parse(self, _ctx: &mut C) -> Result<Self::Parsed, Self::Error> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

let proto::extensions::simple_extension_declaration::ExtensionTypeVariation {
extension_uri_reference,
type_variation_anchor,
name,
} = self;

// Construct the parsed ExtensionTypeVariation.
let extension_type_variation = ExtensionTypeVariation {
extension_uri_reference: Anchor::new(extension_uri_reference),
name,
anchor: Anchor::new(type_variation_anchor),
};

// Add the ExtensionTypeVariation to the given context.
ctx.add_extension_type_variation(&extension_type_variation)?;

Ok(extension_type_variation)
}
}

impl From<ExtensionTypeVariation>
for proto::extensions::simple_extension_declaration::ExtensionTypeVariation
{
fn from(value: ExtensionTypeVariation) -> Self {
let ExtensionTypeVariation {
anchor,
name,
extension_uri_reference,
} = value;

Self {
extension_uri_reference: extension_uri_reference.into_inner(),
type_variation_anchor: anchor.into_inner(),
name: name,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::{
parse::{Context, Parse},
proto,
};

use super::{
extension_function, extension_type, extension_type_variation,
simple_extension_declaration::SimpleExtensionDeclarationError,
};

/// MappingType
#[derive(Clone, Debug, PartialEq)]
pub enum MappingType {
ExtensionType(extension_type::ExtensionType),
ExtensionTypeVariation(extension_type_variation::ExtensionTypeVariation),
ExtensionFunction(extension_function::ExtensionFunction),
}

impl<C: Context> Parse<C> for proto::extensions::simple_extension_declaration::MappingType {
type Parsed = MappingType;
type Error = SimpleExtensionDeclarationError;

fn parse(self, ctx: &mut C) -> Result<Self::Parsed, Self::Error> {
Ok(match self {
proto::extensions::simple_extension_declaration::MappingType::ExtensionFunction(inner) => MappingType::ExtensionFunction(inner.parse(ctx)?),
proto::extensions::simple_extension_declaration::MappingType::ExtensionType(inner) => MappingType::ExtensionType(inner.parse(ctx)?),
proto::extensions::simple_extension_declaration::MappingType::ExtensionTypeVariation(inner) => MappingType::ExtensionTypeVariation(inner.parse(ctx)?),
})
}
}

impl From<MappingType> for proto::extensions::simple_extension_declaration::MappingType {
fn from(value: MappingType) -> Self {
match value {
MappingType::ExtensionFunction(inner) => Self::ExtensionFunction(inner.into()),
MappingType::ExtensionType(inner) => Self::ExtensionType(inner.into()),
MappingType::ExtensionTypeVariation(inner) => {
Self::ExtensionTypeVariation(inner.into())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod extension_function;
pub mod extension_type;
pub mod extension_type_variation;
pub mod mapping_type;
pub mod simple_extension_declaration;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::{
parse::{Context, Parse},
proto,
};
use thiserror::Error;

use super::mapping_type::MappingType;

// SimpleExtensionDeclaration
#[derive(Clone, Debug, PartialEq)]
pub struct SimpleExtensionDeclaration {
pub mapping_type: Option<MappingType>,
Comment thread
ilikepi63 marked this conversation as resolved.
Outdated
Comment thread
ilikepi63 marked this conversation as resolved.
Outdated
}

#[derive(Debug, PartialEq, Error)]
pub enum SimpleExtensionDeclarationError {
#[error("No Mapping Type specified on Extension Declaration.")]
MissingMappingType,

/// Context error
#[error(transparent)]
Context(#[from] ContextError),
}

impl<C: Context> Parse<C> for proto::extensions::SimpleExtensionDeclaration {
type Parsed = SimpleExtensionDeclaration;
type Error = SimpleExtensionDeclarationError;

fn parse(self, ctx: &mut C) -> Result<Self::Parsed, Self::Error> {
let proto::extensions::SimpleExtensionDeclaration { mapping_type } = self;

Ok(SimpleExtensionDeclaration {
mapping_type: Some(
mapping_type
.ok_or(SimpleExtensionDeclarationError::MissingMappingType)?
.parse(ctx)?,
),
})
}
}

impl From<SimpleExtensionDeclaration> for proto::extensions::SimpleExtensionDeclaration {
fn from(declaration: SimpleExtensionDeclaration) -> Self {
let SimpleExtensionDeclaration { mapping_type } = declaration;

proto::extensions::SimpleExtensionDeclaration {
mapping_type: mapping_type.map(|mapping_type| mapping_type.into()),
Comment thread
ilikepi63 marked this conversation as resolved.
Outdated
}
}
}