Skip to content

Commit

Permalink
More docstrings in schema-ast
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhoule committed Dec 20, 2021
1 parent 10a86a7 commit 32e2e43
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 12 deletions.
4 changes: 2 additions & 2 deletions libs/datamodel/parser-database/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod map;
mod native_types;

use crate::{
ast,
ast::{self, WithName, WithSpan},
context::{Arguments, Context},
types::{
EnumAttributes, FieldWithArgs, IndexAlgorithm, IndexAttribute, IndexType, ModelAttributes, RelationField,
Expand Down Expand Up @@ -358,7 +358,7 @@ fn visit_model_ignore(model_id: ast::ModelId, model_data: &mut ModelAttributes<'
DatamodelError::new_attribute_validation_error(
"Fields on an already ignored Model do not need an `@ignore` annotation.",
"ignore",
ctx.db.ast[model_id][field_id].span,
*ctx.db.ast[model_id][field_id].span(),
)
})
.collect();
Expand Down
2 changes: 1 addition & 1 deletion libs/datamodel/parser-database/src/attributes/id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::FieldResolutionError;
use crate::{
ast::{self, WithSpan},
ast::{self, WithName, WithSpan},
attributes::resolve_field_array_with_args,
context::{Arguments, Context},
types::{FieldWithArgs, IdAttribute, ModelAttributes, SortOrder},
Expand Down
2 changes: 1 addition & 1 deletion libs/datamodel/parser-database/src/attributes/map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
ast,
ast::{self, WithName},
context::{Arguments, Context},
types::{CompositeTypeField, ModelAttributes, ScalarField},
DatamodelError,
Expand Down
2 changes: 1 addition & 1 deletion libs/datamodel/parser-database/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{context::Context, walkers::CompositeTypeFieldWalker, walkers::CompositeTypeWalker, DatamodelError};
use schema_ast::ast;
use schema_ast::ast::{self, WithName};
use std::{
collections::{BTreeMap, HashMap},
fmt,
Expand Down
5 changes: 3 additions & 2 deletions libs/datamodel/parser-database/src/walkers/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use super::{
ScalarFieldWalker,
};
use crate::{
ast,
{types::ModelAttributes, ParserDatabase},
ast::{self, WithName},
types::ModelAttributes,
ParserDatabase,
};
use std::hash::{Hash, Hasher};

Expand Down
11 changes: 11 additions & 0 deletions libs/datamodel/schema-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,20 @@ impl SchemaAst {
})
}

/// Iterate over all the top-level items in the schema.
pub fn iter_tops(&self) -> impl Iterator<Item = (TopId, &Top)> {
self.tops
.iter()
.enumerate()
.map(|(top_idx, top)| (top_idx_to_top_id(top_idx, top), top))
}

/// Iterate over all the datasource blocks in the schema.
pub fn sources(&self) -> impl Iterator<Item = &SourceConfig> {
self.tops.iter().filter_map(|top| top.as_source())
}

/// Iterate over all the generator blocks in the schema.
pub fn generators(&self) -> impl Iterator<Item = &GeneratorConfig> {
self.tops.iter().filter_map(|top| top.as_generator())
}
Expand Down Expand Up @@ -135,11 +138,17 @@ pub struct SourceId(u32);
/// syntax to resolve the id to an `ast::Top`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TopId {
/// A composite type
CompositeType(CompositeTypeId),
/// A model declaration
Model(ModelId),
/// An enum declaration
Enum(EnumId),
/// A type alias
Alias(AliasId),
/// A generator block
Generator(GeneratorId),
/// A datasource block
Source(SourceId),
}

Expand All @@ -152,13 +161,15 @@ impl TopId {
}
}

/// Try to interpret the top as a model.
pub fn as_model_id(self) -> Option<ModelId> {
match self {
TopId::Model(model_id) => Some(model_id),
_ => None,
}
}

/// Try to interpret the top as a composite type.
pub fn as_composite_type_id(&self) -> Option<CompositeTypeId> {
match self {
TopId::CompositeType(ctid) => Some(*ctid),
Expand Down
1 change: 1 addition & 0 deletions libs/datamodel/schema-ast/src/ast/find_at_position.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::ast::{self, top_idx_to_top_id, traits::*};

impl ast::SchemaAst {
/// Find the AST node at the given position (byte offset).
pub fn find_at_position(&self, position: usize) -> SchemaPosition<'_> {
self.find_top_at_position(position)
.map(|top_id| match top_id {
Expand Down
4 changes: 4 additions & 0 deletions libs/datamodel/schema-ast/src/ast/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use super::{Span, WithSpan};

/// An identifier.
#[derive(Debug, Clone, PartialEq)]
pub struct Identifier {
/// The identifier contents.
pub name: String,
/// The span of the AST node.
pub span: Span,
}

impl Identifier {
/// Instantiate a new identifier with an empty span.
pub fn new(name: &str) -> Identifier {
Identifier {
name: String::from(name),
Expand Down
4 changes: 0 additions & 4 deletions libs/datamodel/schema-ast/src/ast/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ impl Model {
pub fn find_field_bang(&self, name: &str) -> &Field {
self.find_field(name).unwrap()
}

pub fn name(&self) -> &str {
&self.name.name
}
}

impl WithIdentifier for Model {
Expand Down
15 changes: 15 additions & 0 deletions libs/datamodel/schema-ast/src/ast/top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ use crate::ast::{
/// Enum for distinguishing between top-level entries
#[derive(Debug, Clone, PartialEq)]
pub enum Top {
/// A composite type
CompositeType(CompositeType),
/// An enum declaration
Enum(Enum),
/// A model declaration
Model(Model),
/// A datasource block
Source(SourceConfig),
/// A generator block
Generator(GeneratorConfig),
/// A type alias
Type(Field),
}

impl Top {
/// A string saying what kind of item this is.
pub fn get_type(&self) -> &str {
match self {
Top::CompositeType(_) => "composite type",
Expand All @@ -25,6 +32,7 @@ impl Top {
}
}

/// The name of the item.
pub fn identifier(&self) -> &Identifier {
match self {
Top::CompositeType(ct) => &ct.name,
Expand All @@ -36,45 +44,52 @@ impl Top {
}
}

/// The name of the item.
pub fn name(&self) -> &str {
&self.identifier().name
}

/// Try to interpret the item as a composite type declaration.
pub fn as_composite_type(&self) -> Option<&CompositeType> {
match self {
Top::CompositeType(ct) => Some(ct),
_ => None,
}
}

/// Try to interpret the item as a model declaration.
pub fn as_model(&self) -> Option<&Model> {
match self {
Top::Model(model) => Some(model),
_ => None,
}
}

/// Try to interpret the item as an enum declaration.
pub fn as_enum(&self) -> Option<&Enum> {
match self {
Top::Enum(r#enum) => Some(r#enum),
_ => None,
}
}

/// Try to interpret the item as a generator block.
pub fn as_generator(&self) -> Option<&GeneratorConfig> {
match self {
Top::Generator(gen) => Some(gen),
_ => None,
}
}

/// Try to interpret the item as a type alias.
pub fn as_type_alias(&self) -> Option<&Field> {
match self {
Top::Type(r#type) => Some(r#type),
_ => None,
}
}

/// Try to interpret the item as a datasource block.
pub fn as_source(&self) -> Option<&SourceConfig> {
match self {
Top::Source(source) => Some(source),
Expand Down
11 changes: 11 additions & 0 deletions libs/datamodel/schema-ast/src/ast/traits.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
use super::{Attribute, Comment, Identifier, Span};

/// An AST node with a span.
pub trait WithSpan {
/// The span of the node.
fn span(&self) -> &Span;
}

/// An AST node with a name (from the identifier).
pub trait WithName {
/// The name of the item.
fn name(&self) -> &str;
}

/// An AST node with an identifier.
pub trait WithIdentifier {
/// The identifier.
fn identifier(&self) -> &Identifier;
}

/// An AST node with attributes.
pub trait WithAttributes {
/// The attributes.
fn attributes(&self) -> &[Attribute];
}

/// An AST node with documentation.
pub trait WithDocumentation {
/// The documentation string, if defined.
fn documentation(&self) -> &Option<Comment>;

/// Is this node commented out?
fn is_commented_out(&self) -> bool;
}

Expand Down
2 changes: 1 addition & 1 deletion libs/datamodel/schema-ast/src/parser/datamodel.pest
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ BLOCK_LEVEL_CATCH_ALL = { !BLOCK_CLOSE ~ CATCH_ALL }
function = { non_empty_identifier ~ "(" ~ (expression ~ ("," ~ expression)*)? ~ ")" }
field_with_args = { non_empty_identifier ~ "(" ~ argument ~ ("," ~ argument)* ~")"}
array_expression = { "[" ~ (expression ~ ( "," ~ expression )*)? ~ "]" }
expression = { field_with_args | array_expression | function | numeric_literal | string_literal | constant_literal }
expression = { function | field_with_args | array_expression | numeric_literal | string_literal | constant_literal }

// ######################################
// Literals / Values
Expand Down

0 comments on commit 32e2e43

Please sign in to comment.