// Code quality of this file: medium. using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; public enum Kind { Record, Variant, } public static class Generator { public static void Generate(string outputFile, string singleHeader, string header, string footer, string qualifier, ImmutableDictionary>> types) { using (var o = new System.IO.StreamWriter(outputFile)) { Action w = o.WriteLine; w("// This file was generated by Generator.cs"); w(""); w("using System;"); w($"{singleHeader}"); w(""); foreach (var type in types) { var name = type.Key; var kind = type.Value.Item1; var components = type.Value.Item2; switch (kind) { case Kind.Record: w.Record(header, footer, qualifier, name, @components); break; case Kind.Variant: w.Variant(header, footer, qualifier, name, @components); break; } w(""); } } } // Below are shorthands for making the last argument to Generate(). public static ImmutableDictionary>> Types(params Tuple>>[] types) => types.ToImmutableDictionary(t => t.Item1, t => t.Item2); public static Tuple>> Record(string name, params Tuple[] fields) => new Tuple>>( name, new Tuple>( Kind.Record, fields.ToImmutableDictionary(t => t.Item1, t => t.Item2))); public static Tuple>> Variant(string name, params Tuple[] cases) => new Tuple>>( name, new Tuple>( Kind.Variant, cases.ToImmutableDictionary(t => t.Item1, t => t.Item2))); public static Tuple Field(string type, string name) => new Tuple(name, type); public static Tuple Case(string type, string name) => new Tuple(name, type); public static Tuple Case(string name) => new Tuple(name, null); }