From ac40a2b874932896fc7cde3ffb30786f1fca1f6e Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Thu, 21 Sep 2017 17:47:23 +0200
Subject: [PATCH] Data model

---
 src/data/data.ts             | 14 +++++++++++++-
 src/data/schema.ts           |  7 ++++---
 src/data/spec/schema.spec.ts | 13 ++-----------
 src/reader/common/data.ts    |  0
 src/reader/gro/parser.ts     |  0
 src/reader/gro/schema.ts     |  0
 6 files changed, 19 insertions(+), 15 deletions(-)
 create mode 100644 src/reader/common/data.ts
 create mode 100644 src/reader/gro/parser.ts
 create mode 100644 src/reader/gro/schema.ts

diff --git a/src/data/data.ts b/src/data/data.ts
index 49452e5e3..5f1c08cb9 100644
--- a/src/data/data.ts
+++ b/src/data/data.ts
@@ -9,16 +9,28 @@ export interface File {
     readonly blocks: ReadonlyArray<Block>
 }
 
+export function File(blocks: ArrayLike<Block>, name?: string): File {
+    return { name, blocks: blocks as any };
+}
+
 export interface Block {
     readonly header?: string,
     readonly categories: { readonly [name: string]: Category }
 }
 
+export function Block(categories: { readonly [name: string]: Category }, header?: string): Block {
+    return { header, categories };
+}
+
 export interface Category {
     readonly rowCount: number,
     getField(name: string): Field | undefined
 }
 
+export function Category(rowCount: number, fields: { [name: string]: Field }): Category {
+    return { rowCount, getField(name) { return fields[name]; } };
+}
+
 export namespace Category {
     export const Empty: Category = { rowCount: 0, getField(name: string) { return void 0; } };
 }
@@ -29,7 +41,7 @@ export const enum ValuePresence {
     Unknown = 2
 }
 
-export type FieldArray = number[] | Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array
+export type FieldArray = string[] | number[] | Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array
 
 /**
  * Implementation note:
diff --git a/src/data/schema.ts b/src/data/schema.ts
index 69184e0b5..83f9a9890 100644
--- a/src/data/schema.ts
+++ b/src/data/schema.ts
@@ -23,10 +23,11 @@ import * as Data from './data'
 export type BlockDefinition = { [category: string]: CategoryDefinition }
 export type CategoryDefinition = { '@alias'?: string } & { [field: string]: Field.Schema<any> }
 
-export type Schema<Definition extends BlockDefinition> = Block<{ [C in keyof Definition]: Category<{ [F in keyof Definition[C]]: Field<Definition[C][F]['type']> }> }>
+export type BlockInstance<Definition extends BlockDefinition> = Block<{ [C in keyof Definition]: CategoryInstance<Definition[C]> }>
+export type CategoryInstance<Definition extends CategoryDefinition> = Category<{ [F in keyof Definition]: Field<Definition[F]['type']> }>
 
-export function apply<T extends BlockDefinition>(schema: T, block: Data.Block): Schema<T> {
-    return createBlock(schema, block) as Schema<T>;
+export function apply<Definition extends BlockDefinition>(schema: Definition, block: Data.Block): BlockInstance<Definition> {
+    return createBlock(schema, block) as BlockInstance<Definition>;
 }
 
 export type Block<Categories> = Categories & {
diff --git a/src/data/spec/schema.spec.ts b/src/data/spec/schema.spec.ts
index 8ca0e7c8a..1918453bf 100644
--- a/src/data/spec/schema.spec.ts
+++ b/src/data/spec/schema.spec.ts
@@ -34,17 +34,8 @@ function Field(values: any[]): Data.Field {
     }
 }
 
-class Category implements Data.Category {
-    getField(name: string) { return this.fields[name]; }
-    constructor(public rowCount: number, private fields: any) { }
-}
-
-class Block implements Data.Block {
-    constructor(public categories: { readonly [name: string]: Data.Category }, public header?: string) { }
-}
-
-const testBlock = new Block({
-    'atoms': new Category(2, {
+const testBlock = Data.Block({
+    'atoms': Data.Category(2, {
         x: Field([1, 2]),
         name: Field(['C', 'O'])
     })
diff --git a/src/reader/common/data.ts b/src/reader/common/data.ts
new file mode 100644
index 000000000..e69de29bb
diff --git a/src/reader/gro/parser.ts b/src/reader/gro/parser.ts
new file mode 100644
index 000000000..e69de29bb
diff --git a/src/reader/gro/schema.ts b/src/reader/gro/schema.ts
new file mode 100644
index 000000000..e69de29bb
-- 
GitLab