Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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,50 @@
use super::simple_extension_declaration::SimpleExtensionDeclarationError;
use crate::parse::{Anchor, Context, Parse};
use crate::proto;
Comment thread
ilikepi63 marked this conversation as resolved.
Outdated

#[derive(Clone, Debug, PartialEq)]
pub struct ExtensionFunction {
anchor: Anchor<Self>,
name: String,
Comment thread
ilikepi63 marked this conversation as resolved.
Outdated
extension_uri_reference: Anchor<Self>,
Comment thread
ilikepi63 marked this conversation as resolved.
Outdated
}

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),
};

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,49 @@
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: String,
Comment thread
ilikepi63 marked this conversation as resolved.
Outdated
extension_uri_reference: Anchor<Self>,
Comment thread
ilikepi63 marked this conversation as resolved.
Outdated
}

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),
};

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,53 @@
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: String,
Comment thread
ilikepi63 marked this conversation as resolved.
Outdated
extension_uri_reference: Anchor<Self>,
Comment thread
ilikepi63 marked this conversation as resolved.
Outdated
}

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),
};

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,52 @@
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) => {
proto::extensions::simple_extension_declaration::MappingType::ExtensionFunction(
inner.into(),
)
}
MappingType::ExtensionType(inner) => {
proto::extensions::simple_extension_declaration::MappingType::ExtensionType(
inner.into(),
)
}
MappingType::ExtensionTypeVariation(inner) => {
proto::extensions::simple_extension_declaration::MappingType::ExtensionTypeVariation(
Comment thread
ilikepi63 marked this conversation as resolved.
Outdated
inner.into(),
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod extension_function;
mod extension_type;
mod extension_type_variation;
mod mapping_type;
mod simple_extension_declaration;
Comment thread
ilikepi63 marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 {}

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: mapping_type
.map(|mapping_type| mapping_type.parse(ctx).ok())

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.

We don't want this to silently error on parser errors.

.flatten(),
})
}
}

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
}
}
}