
Options.pudu
Pudu159 lines6.7 KB
1/** @Environment.Options.Module — what a load reads and how */2module PuduLangEnvironment.Options34import PuduLangEnvironment.Constants.Messages as Messages5import PuduLangEnvironment.Constants.Names as Names6import PuduLangEnvironment.Domain.Encoding as Encoding7import PuduLangEnvironment.Source as Source89/** @Environment.Options.Options — every choice a load makes */10export type Options = {11 strict: Bool,12 files: Array[Str],13 sources: Array[Source.Source],14 encoding: Encoding.Encoding,15 trimValues: Bool,16 overwrite: Bool,17 probe: Option[Int],18 exportSyntax: Bool,19 inlineComments: Bool,20 expansion: Bool,21 cascade: Bool,22 environmentName: Option[Str],23 directory: Str24}25262728export fn defaults() -> Options {29 Options {30 strict: false,31 files: [Names.DEFAULT_FILE],32 sources: [],33 encoding: Encoding.Utf8,34 trimValues: false,35 overwrite: true,36 probe: None,37 exportSyntax: false,38 inlineComments: true,39 expansion: false,40 cascade: false,41 environmentName: None,42 directory: "."43 }44}454647export fn validate(options: &Options) -> Array[Str] {48 let givenFiles = options.files != [Names.DEFAULT_FILE]49 let givenSources = !options.sources.isEmpty()50 let probing = options.probe != None51 var problems: Array[Str] = []52 if options.files.isEmpty() { problems = problems.push(Messages.NO_FILES) }53 if givenFiles && probing { problems = problems.push(Messages.FILES_WITH_PROBE) }54 if givenFiles && options.cascade { problems = problems.push(Messages.FILES_WITH_CASCADE) }55 if givenSources && givenFiles { problems = problems.push(Messages.SOURCES_WITH_FILES) }56 if givenSources && probing { problems = problems.push(Messages.SOURCES_WITH_PROBE) }57 if givenSources && options.cascade { problems = problems.push(Messages.SOURCES_WITH_CASCADE) }58 if let Some(levels) = options.probe {59 if levels < 0 { problems = problems.push(Messages.NEGATIVE_PROBE) }60 }61 problems62}6364/** @Environment.Options.Configuring — options changed one choice at a time */65export trait Configuring {66 fn withStrict(self: &Self) -> Self67 fn withoutStrict(self: &Self) -> Self68 fn withFiles(self: &Self, paths: &Array[Str]) -> Self69 fn withSources(self: &Self, given: &Array[Source.Source]) -> Self70 fn withEncoding(self: &Self, encoding: Encoding.Encoding) -> Self71 fn withTrimValues(self: &Self) -> Self72 fn withoutTrimValues(self: &Self) -> Self73 fn withOverwrite(self: &Self) -> Self74 fn withoutOverwrite(self: &Self) -> Self75 fn withProbe(self: &Self) -> Self76 fn withProbeLevels(self: &Self, levels: Int) -> Self77 fn withoutProbe(self: &Self) -> Self78 fn withExportSyntax(self: &Self) -> Self79 fn withoutExportSyntax(self: &Self) -> Self80 fn withInlineComments(self: &Self) -> Self81 fn withoutInlineComments(self: &Self) -> Self82 fn withExpansion(self: &Self) -> Self83 fn withoutExpansion(self: &Self) -> Self84 fn withCascade(self: &Self) -> Self85 fn withEnvironment(self: &Self, name: Str) -> Self86 fn withoutCascade(self: &Self) -> Self87 fn inDirectory(self: &Self, directory: Str) -> Self88}8990impl Configuring for Options {91 92 fn withStrict(self: &Self) -> Self { Options{..*self, strict: true} }9394 95 fn withoutStrict(self: &Self) -> Self { Options{..*self, strict: false} }9697 98 fn withFiles(self: &Self, paths: &Array[Str]) -> Self {99 Options{..*self, files: if paths.isEmpty() { [Names.DEFAULT_FILE] } else { *paths }}100 }101102 103 fn withSources(self: &Self, given: &Array[Source.Source]) -> Self { Options{..*self, sources: *given} }104105 106 fn withEncoding(self: &Self, encoding: Encoding.Encoding) -> Self { Options{..*self, encoding: encoding} }107108 109 fn withTrimValues(self: &Self) -> Self { Options{..*self, trimValues: true} }110111 112 fn withoutTrimValues(self: &Self) -> Self { Options{..*self, trimValues: false} }113114 115 fn withOverwrite(self: &Self) -> Self { Options{..*self, overwrite: true} }116117 118 fn withoutOverwrite(self: &Self) -> Self { Options{..*self, overwrite: false} }119120 121 fn withProbe(self: &Self) -> Self { Options{..*self, probe: Some(Names.DEFAULT_PROBE_LEVELS)} }122123 124 fn withProbeLevels(self: &Self, levels: Int) -> Self { Options{..*self, probe: Some(levels)} }125126 127 fn withoutProbe(self: &Self) -> Self { Options{..*self, probe: None} }128129 130 fn withExportSyntax(self: &Self) -> Self { Options{..*self, exportSyntax: true} }131132 133 fn withoutExportSyntax(self: &Self) -> Self { Options{..*self, exportSyntax: false} }134135 136 fn withInlineComments(self: &Self) -> Self { Options{..*self, inlineComments: true} }137138 139 fn withoutInlineComments(self: &Self) -> Self { Options{..*self, inlineComments: false} }140141 142 fn withExpansion(self: &Self) -> Self { Options{..*self, expansion: true} }143144 145 fn withoutExpansion(self: &Self) -> Self { Options{..*self, expansion: false} }146147 148 fn withCascade(self: &Self) -> Self { Options{..*self, cascade: true} }149150 151 fn withEnvironment(self: &Self, name: Str) -> Self { Options{..*self, cascade: true, environmentName: Some(name)} }152153 154 fn withoutCascade(self: &Self) -> Self { Options{..*self, cascade: false} }155156 157 fn inDirectory(self: &Self, directory: Str) -> Self { Options{..*self, directory: directory} }158}159