Line data Source code
1 : import 'package:enum_assist/src/enum_field.dart'; 2 : import 'package:enum_assist/src/util/string_helpers.dart'; 3 : import 'package:meta/meta.dart'; 4 : 5 : /// {@template enum_assist.template_core} 6 : /// helper class to format generated code 7 : /// {@endtemplate} 8 : abstract class FieldTemplate<T> { 9 : /// {@template enum_assist.template_core} 10 : /// helper class to format generated code 11 : /// {@endtemplate} 12 0 : const FieldTemplate(this.enumName, this.field); 13 : 14 : /// the name of the enum 15 : final String enumName; 16 : 17 : /// the field to format 18 : /// 19 : /// Will always be null in [TemplateCore] 20 : final T field; 21 : } 22 : 23 : /// {@template enum_assist.template_core} 24 : /// helper class to format generated code 25 : /// {@endtemplate} 26 : abstract class TemplateCore<T extends FieldTemplate<R>, R> { 27 : /// {@macro enum_assist.template_core} 28 0 : TemplateCore(this.enumName, this.fields); 29 : 30 : /// the name of the enum 31 : @protected 32 : @nonVirtual 33 : final String enumName; 34 : 35 : /// All fields for enum 36 : @protected 37 : @nonVirtual 38 : final Iterable<R> fields; 39 : 40 : @protected 41 : @nonVirtual 42 : final _buffer = StringBuffer(); 43 : 44 : /// {@macro tab_indentation} 45 0 : @protected 46 : @nonVirtual 47 0 : String get tb => tab(''); 48 : 49 : /// {@macro tab_indentation} 50 0 : @protected 51 : @nonVirtual 52 0 : String tabn(String s, [int n = 1]) => tab(s, n); 53 : 54 : /// maps out all fields to [T] 55 : /// 56 : /// returns joined string 57 0 : @protected 58 : @nonVirtual 59 : String map(String Function(T) i) { 60 0 : return formatFields.map((e) => i(e)).join('\n'); 61 : } 62 : 63 : /// Starts the template with the provided buffer. 64 : StringBuffer writeTemplate(StringBuffer buffer); 65 : 66 0 : @override 67 : @nonVirtual 68 : String toString() { 69 0 : return writeTemplate(_buffer).toString(); 70 : } 71 : 72 : /// fields to be used in the template 73 : /// 74 : /// prepped with quick templates for code generation 75 0 : @protected 76 : @nonVirtual 77 0 : Iterable<T> get formatFields => fields.map(convert); 78 : 79 : /// converts a field to [T] 80 : T convert(R e); 81 : } 82 : 83 : /// {@macro enum_assist.template_core} 84 : abstract class TemplateCoreSimple<T extends FieldTemplate<String>> 85 : extends TemplateCore<T, String> { 86 : /// {@macro enum_assist.template_core} 87 0 : TemplateCoreSimple(String enumName, Iterable<String> fields) 88 0 : : super(enumName, fields); 89 : } 90 : 91 : /// {@macro enum_assist.template_core} 92 : abstract class TemplateCoreDetailed<T extends FieldTemplate<EnumField>> 93 : extends TemplateCore<T, EnumField> { 94 : /// {@macro enum_assist.template_core} 95 0 : TemplateCoreDetailed(String enumName, Iterable<EnumField> fields) 96 0 : : super(enumName, fields); 97 : }