<commit_before>package de.leanovate.swaggercheck.formats

import java.net.{URI, URL}
import java.time.Instant
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import java.util.UUID

import de.leanovate.swaggercheck.{Generators, VerifyResult}
import org.scalacheck.Gen

import scala.util.Try

object StringFormats {

  object URLString extends Format[String] {
    override def generate: Gen[String] = Generators.url

    override def verify(path: String, value: String): VerifyResult =
      if (Try(new URL(value)).isSuccess)
        VerifyResult.success
      else
        VerifyResult.error(s"'$value' is not an url: $path")
  }

  object URIString extends Format[String] {
    override def generate: Gen[String] = Generators.uri

    override def verify(path: String, value: String): VerifyResult =
      if (Try(new URI(value)).isSuccess)
        VerifyResult.success
      else
        VerifyResult.error(s"'$value' is not an uri: $path")
  }

  object UUIDString extends Format[String] {
    override def generate: Gen[String] =
      Gen.uuid.map(_.toString)

    override def verify(path: String, value: String): VerifyResult =
      if (Try(UUID.fromString(value)).isSuccess)
        VerifyResult.success
      else
        VerifyResult.error(s"'$value' is not an uri: $path")
  }

  object EmailString extends Format[String] {
    val emailPattern = """(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])""".r

    override def generate: Gen[String] = Generators.email

    override def verify(path: String, value: String): VerifyResult =
      if (emailPattern.pattern.matcher(value).matches()) {
        VerifyResult.success
      } else {
        VerifyResult.error(s"'$value' is not an email: $path")
      }  }

  object DateString extends Format[String] {
    override def generate: Gen[String] = {
      Gen.choose[Long](Long.MinValue, Long.MaxValue).map {
        diff: Long =>
          val instant = Instant.now().plus(diff, ChronoUnit.NANOS)
          DateTimeFormatter.ISO_DATE.format(instant)
      }<commit_msg>Fix typo<commit_after>package de.leanovate.swaggercheck.formats

import java.net.{URI, URL}
import java.time.Instant
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import java.util.UUID

import de.leanovate.swaggercheck.{Generators, VerifyResult}
import org.scalacheck.Gen

import scala.util.Try

object StringFormats {

  object URLString extends Format[String] {
    override def generate: Gen[String] = Generators.url

    override def verify(path: String, value: String): VerifyResult =
      if (Try(new URL(value)).isSuccess)
        VerifyResult.success
      else
        VerifyResult.error(s"'$value' is not an url: $path")
  }

  object URIString extends Format[String] {
    override def generate: Gen[String] = Generators.uri

    override def verify(path: String, value: String): VerifyResult =
      if (Try(new URI(value)).isSuccess)
        VerifyResult.success
      else
        VerifyResult.error(s"'$value' is not an uri: $path")
  }

  object UUIDString extends Format[String] {
    override def generate: Gen[String] =
      Gen.uuid.map(_.toString)

    override def verify(path: String, value: String): VerifyResult =
      if (Try(UUID.fromString(value)).isSuccess)
        VerifyResult.success
      else
        VerifyResult.error(s"'$value' is not an uuid: $path")
  }

  object EmailString extends Format[String] {
    val emailPattern = """(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])""".r

    override def generate: Gen[String] = Generators.email

    override def verify(path: String, value: String): VerifyResult =
      if (emailPattern.pattern.matcher(value).matches()) {
        VerifyResult.success
      } else {
        VerifyResult.error(s"'$value' is not an email: $path")
      }  }

  object DateString extends Format[String] {
    override def generate: Gen[String] = {
      Gen.choose[Long](Long.MinValue, Long.MaxValue).map {
        diff: Long =>
          val instant = Instant.now().plus(diff, ChronoUnit.NANOS)
          DateTimeFormatter.ISO_DATE.format(instant)
      }
{'fraction_non_alphanumeric': 0.1951619516195162, 'fraction_numerical': 0.03936039360393604, 'mean_word_length': 3.3640429338103757, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3228851', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1884, 'n_words': 297}