Skip to content

Commit

Permalink
Modifies DataModel and Language implementations for code generator (
Browse files Browse the repository at this point in the history
#81)

* Modifies definition of `DataModel`
* Renames `DataModel` to `AbstractDataType`
* Adds parameter to `Sequence` enum variant to store data type
information for the sequence
* Adds `Language` trait for generator
* Adds `RustLanguage` and `JavaLanguage` as implementations of
`Language`
* Moves read and write API generation code to templates
* Reorganizes `CodeGenerator` to keep common behavior between languages
in `CodeGenerator<L>`
* Moves Rust related generation cod e in `CodeGenerator<Rust>`
* Moves Java related generation cod e in `CodeGenerator<Java>`
* Adds new filter `snake` for templates
* Modifies `Import` to only contain name field (Template will use
appopriate casing filter to add an import statement based on `Import`)
  • Loading branch information
desaikd authored Feb 19, 2024
1 parent 6cf4431 commit 8058f0c
Show file tree
Hide file tree
Showing 9 changed files with 493 additions and 2,467 deletions.
2,030 changes: 0 additions & 2,030 deletions Cargo.lock

This file was deleted.

78 changes: 62 additions & 16 deletions src/bin/ion/commands/beta/generate/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,86 @@ use std::fmt::{Display, Formatter};

/// Represents a context that will be used for code generation
pub struct CodeGenContext {
// Initially the data_model field is set to None.
// Once an ISL type definition is mapped to a data model this will have Some value.
pub(crate) data_model: Option<DataModel>,
// Initially the abstract_data_type field is set to None.
// Once an ISL type definition is mapped to an abstract data type this will have Some value.
pub(crate) abstract_data_type: Option<AbstractDataType>,
}

impl CodeGenContext {
pub fn new() -> Self {
Self { data_model: None }
Self {
abstract_data_type: None,
}
}

pub fn with_data_model(&mut self, data_model: DataModel) {
self.data_model = Some(data_model);
pub fn with_abstract_data_type(&mut self, abstract_data_type: AbstractDataType) {
self.abstract_data_type = Some(abstract_data_type);
}
}

/// Represents a data model type that can be used to determine which templates can be used for code generation.
/// A target-language-agnostic data type that determines which template(s) to use for code generation.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum DataModel {
Value, // a struct with a scalar value (used for `type` constraint)
// TODO: Make Sequence parameterized over data type.
// add a data type for sequence here that can be used to read elements for that data type.
Sequence, // a struct with a sequence/collection value (used for `element` constraint)
pub enum AbstractDataType {
// A scalar value (e.g. a string or integer or user defined type)
// e.g. Given below ISL,
// ```
// type::{
// name: value_type,
// type: int
// }
// ```
// Corresponding abstract type in Rust would look like following:
// ```
// struct ValueType {
// value: i64
// }
// ```
Value,
// A series of zero or more values whose type is described by the nested `String` (e.g. a list)
// e.g. Given below ISL,
// ```
// type::{
// name: sequence_type,
// element: int
// }
// ```
// Corresponding abstract type in Rust would look like following:
// ```
// struct SequenceType {
// value: Vec<i64>
// }
// ```
Sequence(String),
// A collection of field name/value pairs (e.g. a map)
// e.g. Given below ISL,
// ```
// type::{
// name: struct_type,
// fields: {
// a: int,
// b: string,
// }
// }
// ```
// Corresponding abstract type in Rust would look like following:
// ```
// struct StructType {
// a: i64,
// b: String,
// }
// ```
Struct,
}

impl Display for DataModel {
impl Display for AbstractDataType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
DataModel::Value => "single value struct",
DataModel::Sequence => "sequence value struct",
DataModel::Struct => "struct",
AbstractDataType::Value => "scalar value struct",
AbstractDataType::Sequence(_) => "sequence",
AbstractDataType::Struct => "struct",
}
)
}
Expand Down
Loading

0 comments on commit 8058f0c

Please sign in to comment.