diff --git a/daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/DamlLfEncoder.scala b/daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/DamlLfEncoder.scala index bcd12af5d326..012aec130b12 100644 --- a/daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/DamlLfEncoder.scala +++ b/daml-lf/encoder/src/main/scala/com/digitalasset/daml/lf/archive/testing/DamlLfEncoder.scala @@ -9,7 +9,7 @@ import java.nio.file.Paths import com.daml.lf.archive.{Dar, DarWriter} import com.daml.lf.data.Ref import com.daml.lf.data.Ref.PackageId -import com.daml.lf.language.{Ast, Interface, LanguageMajorVersion, LanguageVersion} +import com.daml.lf.language.{Ast, Interface, LanguageVersion} import com.daml.lf.testing.parser.{ParserParameters, parseModules} import com.daml.lf.validation.Validation import com.daml.SdkVersion @@ -113,7 +113,7 @@ private[daml] object DamlLfEncoder extends App { } else args(i) match { case "--target" if i + 1 < nAgrs => - go(appArgs.copy(languageVersion = parseVersion(args(i + 1))), i + 2) + go(appArgs.copy(languageVersion = LanguageVersion.assertFromString(args(i + 1))), i + 2) case "--output" if i + 1 < nAgrs => go(appArgs.copy(outputFile = args(i + 1)), i + 2) case _ if i + 1 >= nAgrs => @@ -127,15 +127,6 @@ private[daml] object DamlLfEncoder extends App { go() } - private def parseVersion(version: String) = - version.split("""\.""").toSeq match { - case Seq("1", minor) - if LanguageMajorVersion.V1.supportsMinorVersion(minor) || minor == "dev" => - LanguageVersion(LanguageMajorVersion.V1, LanguageVersion.Minor(minor)) - case _ => - error(s"version '$version' not supported") - } - main() } diff --git a/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/LanguageVersion.scala b/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/LanguageVersion.scala index 41fcaeb802e4..4c2b7d6ea467 100644 --- a/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/LanguageVersion.scala +++ b/daml-lf/language/src/main/scala/com/digitalasset/daml/lf/language/LanguageVersion.scala @@ -1,9 +1,8 @@ // Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -package com.daml.lf.language - -import com.daml.lf.VersionRange +package com.daml.lf +package language final case class LanguageVersion(major: LanguageMajorVersion, minor: LanguageMinorVersion) { def pretty: String = s"${major.pretty}.${minor.toProtoIdentifier}" @@ -17,6 +16,13 @@ object LanguageVersion { type Minor = LanguageMinorVersion val Minor = LanguageMinorVersion + private[this] def stringToVersions = All.iterator.map(v => v.pretty -> v).toMap + + def fromString(s: String): Either[String, LanguageVersion] = + stringToVersions.get(s).toRight(s + " is not supported") + + def assertFromString(s: String): LanguageVersion = data.assertRight(fromString(s)) + implicit val Ordering: scala.Ordering[LanguageVersion] = { case (LanguageVersion(Major.V1, leftMinor), LanguageVersion(Major.V1, rightMinor)) => Major.V1.minorVersionOrdering.compare(leftMinor, rightMinor)