<commit_before>package com.komanov.serialization.converters

import java.nio.charset.StandardCharsets
import java.time.Instant

import com.komanov.serialization.converters.api.MyConverter
import com.komanov.serialization.domain._
import io.circe.parser._
import io.circe.syntax._

import scala.util.Try

/** https://circe.github.io/circe/ */
object CirceConverter extends MyConverter {

  import CirceImplicits._

  override def toByteArray(site: Site): Array[Byte] = {
    require(encodeSite != null)
    site.asJson(encodeSite).noSpaces.getBytes(StandardCharsets.UTF_8)
  }

  override def fromByteArray(bytes: Array[Byte]): Site =
    decode[Site](new String(bytes, StandardCharsets.UTF_8)).right.get

  override def toByteArray(event: SiteEvent): Array[Byte] =
    event.asJson.noSpaces.getBytes(StandardCharsets.UTF_8)

  override def siteEventFromByteArray(clazz: Class[_], bytes: Array[Byte]): SiteEvent =
    decode[SiteEvent](new String(bytes, StandardCharsets.UTF_8)).right.get

  private object CirceImplicits {

    import io.circe._
    import io.circe.generic.auto._

    implicit val encodePageComponentType: Encoder[PageComponentType] = Encoder.encodeString.contramap[PageComponentType](_.toString)
    implicit val decodePageComponentType: Decoder[PageComponentType] = Decoder.decodeString.emapTry(v => Try(PageComponentType.valueOf(v)))

    implicit val encodeSiteFlag: Encoder[SiteFlag] = Encoder.encodeString.contramap[SiteFlag](_.toString)
    implicit val decodeSiteFlag: Decoder[SiteFlag] = Decoder.decodeString.emapTry(v => Try(SiteFlag.valueOf(v)))

    implicit val encodeSiteType: Encoder[SiteType] = Encoder.encodeString.contramap[SiteType](_.toString)
    implicit val decodeSiteType: Decoder[SiteType] = Decoder.decodeString.emapTry(v => Try(SiteType.valueOf(v)))

    implicit val encodeSite: Encoder[Site] = exportEncoder[Site].instance
    implicit val decodeSite: Decoder[Site] = exportDecoder[Site].instance

    implicit val encodeSiteEvent: Encoder[SiteEvent] = exportEncoder[SiteEvent].instance
    implicit val decodeSiteEvent: Decoder[SiteEvent] = exportDecoder[SiteEvent].instance
  }
}
<commit_msg>Use fold instead of Either.right.get<commit_after>package com.komanov.serialization.converters

import com.komanov.serialization.converters.api.MyConverter
import com.komanov.serialization.domain._
import io.circe.parser._
import io.circe.syntax._

import java.nio.charset.StandardCharsets
import scala.util.Try

/** https://circe.github.io/circe/ */
object CirceConverter extends MyConverter {

  import CirceImplicits._

  override def toByteArray(site: Site): Array[Byte] = {
    require(encodeSite != null)
    site.asJson(encodeSite).noSpaces.getBytes(StandardCharsets.UTF_8)
  }

  override def fromByteArray(bytes: Array[Byte]): Site =
    decode[Site](new String(bytes, StandardCharsets.UTF_8)).fold(throw _, identity)

  override def toByteArray(event: SiteEvent): Array[Byte] =
    event.asJson.noSpaces.getBytes(StandardCharsets.UTF_8)

  override def siteEventFromByteArray(clazz: Class[_], bytes: Array[Byte]): SiteEvent =
    decode[SiteEvent](new String(bytes, StandardCharsets.UTF_8)).fold(throw _, identity)

  private object CirceImplicits {

    import io.circe._
    import io.circe.generic.auto._

    implicit val encodePageComponentType: Encoder[PageComponentType] = Encoder.encodeString.contramap[PageComponentType](_.toString)
    implicit val decodePageComponentType: Decoder[PageComponentType] = Decoder.decodeString.emapTry(v => Try(PageComponentType.valueOf(v)))

    implicit val encodeSiteFlag: Encoder[SiteFlag] = Encoder.encodeString.contramap[SiteFlag](_.toString)
    implicit val decodeSiteFlag: Decoder[SiteFlag] = Decoder.decodeString.emapTry(v => Try(SiteFlag.valueOf(v)))

    implicit val encodeSiteType: Encoder[SiteType] = Encoder.encodeString.contramap[SiteType](_.toString)
    implicit val decodeSiteType: Decoder[SiteType] = Decoder.decodeString.emapTry(v => Try(SiteType.valueOf(v)))

    implicit val encodeSite: Encoder[Site] = exportEncoder[Site].instance
    implicit val decodeSite: Decoder[Site] = exportDecoder[Site].instance

    implicit val encodeSiteEvent: Encoder[SiteEvent] = exportEncoder[SiteEvent].instance
    implicit val decodeSiteEvent: Decoder[SiteEvent] = exportDecoder[SiteEvent].instance
  }
}

{'fraction_non_alphanumeric': 0.10726723937775715, 'fraction_numerical': 0.0018574413745066172, 'mean_word_length': 5.752351097178684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '6201138', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1307, 'n_words': 234}