Skip to content

Commit

Permalink
Render composite types with the new renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Oct 21, 2022
1 parent 873cfc1 commit 82db30b
Show file tree
Hide file tree
Showing 13 changed files with 564 additions and 112 deletions.
14 changes: 11 additions & 3 deletions introspection-engine/datamodel-renderer/src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use std::fmt;
//! Types related to the _configuration section_ in the PSL.
//!
//! Includes the `datasource` and `generator` definitions.

mod datasource;
mod generator;

use crate::{Datasource, Generator};
pub use datasource::Datasource;
pub use generator::Generator;

use std::fmt;

/// The configuration part of a data model. First the generators, then
/// the datasources.
Expand Down Expand Up @@ -53,7 +61,7 @@ impl<'a> fmt::Display for Configuration<'a> {

#[cfg(test)]
mod tests {
use crate::*;
use crate::{configuration::*, value::*};
use expect_test::expect;

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::value::{Array, Documentation, Env, Text, Value};
use core::fmt;
use std::default::Default;

use psl::datamodel_connector::RelationMode;

use crate::{Array, Documentation, Env, Text, Value};
use std::default::Default;

/// The datasource block in a PSL file.
#[derive(Debug)]
Expand Down Expand Up @@ -157,7 +155,7 @@ impl<'a> fmt::Display for Datasource<'a> {

#[cfg(test)]
mod tests {
use crate::*;
use crate::{configuration::*, value::*};
use expect_test::expect;
use psl::datamodel_connector::RelationMode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::fmt;

use crate::value::{Array, Documentation, Env, Text};
use psl::PreviewFeature;

use crate::{Array, Documentation, Env, Text};
use std::fmt;

/// The generator block of the datasource.
#[derive(Debug)]
Expand Down Expand Up @@ -162,7 +160,7 @@ impl<'a> fmt::Display for Generator<'a> {

#[cfg(test)]
mod tests {
use crate::*;
use crate::{configuration::*, value::*};
use expect_test::expect;
use psl::PreviewFeature;

Expand Down
16 changes: 13 additions & 3 deletions introspection-engine/datamodel-renderer/src/datamodel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
use std::fmt;
//! Types related to the _datamodel section_ in the PSL.
//!
//! Includes the `model`, `enum` and `type` definitions.

mod attributes;
mod composite_type;
mod default;
mod enumerator;

use crate::Enum;
pub use composite_type::{CompositeType, CompositeTypeField};
pub use default::DefaultValue;
pub use enumerator::{Enum, EnumVariant};
use std::fmt;

/// The PSL data model declaration.
#[derive(Default, Debug)]
Expand Down Expand Up @@ -38,7 +48,7 @@ impl<'a> fmt::Display for Datamodel<'a> {

#[cfg(test)]
mod tests {
use crate::*;
use super::*;
use expect_test::expect;

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::fmt;

use crate::value::{Function, FunctionParam};

/// Defines a field attribute, wrapping a function.
///
/// ```ignore
/// model X {
/// field Int @map("lol")
/// ^^^^^^^^^^^ this
/// }
/// ```
#[derive(Debug)]
pub(super) struct FieldAttribute<'a> {
attribute: Function<'a>,
prefix: Option<&'a str>,
}

impl<'a> FieldAttribute<'a> {
pub(super) fn new(attribute: Function<'a>) -> Self {
Self {
attribute,
prefix: None,
}
}

/// Adds a prefix to the field attribute. Useful for native types,
/// e.g. `attr.prefix("db")` for a type attribute renders as
/// `@db.Type`.
pub(super) fn prefix(&mut self, prefix: &'a str) {
self.prefix = Some(prefix);
}

/// Add a new parameter to the attribute function.
pub fn push_param(&mut self, param: impl Into<FunctionParam<'a>>) {
self.attribute.push_param(param.into());
}
}

impl<'a> fmt::Display for FieldAttribute<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("@")?;

if let Some(prefix) = self.prefix {
f.write_str(prefix)?;
f.write_str(".")?;
}

self.attribute.fmt(f)?;

Ok(())
}
}

/// Defines a block attribute, wrapping a function.
///
/// ```ignore
/// model X {
/// @@map("lol")
/// ^^^^^^^^^^^^ this
/// }
/// ```
#[derive(Debug)]
pub(super) struct BlockAttribute<'a>(pub(super) Function<'a>);

impl<'a> fmt::Display for BlockAttribute<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("@@")?;
self.0.fmt(f)?;

Ok(())
}
}

0 comments on commit 82db30b

Please sign in to comment.