<commit_before>package hmda.validation.rules.ts.syntactical

import hmda.model.fi.lar.LoanApplicationRegister
import hmda.model.fi.ts.TransmittalSheet
import hmda.validation.context.ValidationContext
import hmda.validation.dsl.PredicateCommon._
import hmda.validation.dsl.PredicateSyntax.PredicateOps
import hmda.validation.dsl.{ Failure, Result, Success }
import hmda.validation.rules.EditCheck

object S025 {

  def inContext(ctx: ValidationContext) = {
    ctx.institution match {
      case Some(inst) => new S025(inst)
      case None => new EmptyEditCheck
    }
  }

  def name = "S025" // this would go away too

  // TODO the necessary things to make an EditCheck[T <: HasControlId] or whatever.
  // after that, then the next two functions could go away entirely.
  def apply(ts: TransmittalSheet, ctx: ValidationContext): Result = {
    compare(ts.respondent.id, ts.agencyCode, ctx)
  }

  private def compare(filingRespId: String, filingAgencyCode: Int, ctx: ValidationContext): Result = {
    ctx.institution match {

      // If institution is not present, edit is always a `Success`
      case None => Success()
      case Some(institution) => institution.respondentId match {

        // If respondentId cannot be derived, edit is always a `Failure`
        case Left(invalid) => new Failure()
        case Right(validRespId) => {
          (filingRespId is equalTo(validRespId.id)) and (filingAgencyCode is equalTo(institution.agency.value))
        }
      }
    }
  }

}

class S025(institution: Institution) extends EditCheck[LoanApplicationRegister] {
  def name = "S025"

  def apply(ts: TransmittalSheet): Result = {
    compare(ts.respondent.id, ts.agencyCode)
  }

  def apply(lar: LoanApplicationRegister): Result = {
    compare(lar.respondentId, lar.agencyCode)
  }

  private def compare(filingRespId: String, filingAgencyCode: Int): Result = {
    institution.respondentId match {
      case Left(invalid) => new Failure()
      case Right(validRespId) => {
        (filingRespId is equalTo(validRespId.id)) and (filingAgencyCode is equalTo(institution.agency.value))
      }
    }
  }
}

class EmptyEditCheck extends EditCheck[LoanApplicationRegister] {
  def name = "empty"
  def apply(lar: LoanApplicationRegister): Result = Success()
  def apply(transmittalSheet: TransmittalSheet): Result = Success()
}
<commit_msg>go ahead and remove some duplicated code<commit_after>package hmda.validation.rules.ts.syntactical

import hmda.model.fi.lar.LoanApplicationRegister
import hmda.model.fi.ts.TransmittalSheet
import hmda.validation.context.ValidationContext
import hmda.validation.dsl.PredicateCommon._
import hmda.validation.dsl.PredicateSyntax.PredicateOps
import hmda.validation.dsl.{ Failure, Result, Success }
import hmda.validation.rules.EditCheck

object S025 {

  def inContext(ctx: ValidationContext) = {
    ctx.institution match {
      case Some(inst) => new S025(inst)
      case None => new EmptyEditCheck
    }
  }

  def name = "S025" // this would go away too

  // TODO the necessary things to make an EditCheck[T <: HasControlId] or whatever.
  // then soon this function could go away entirely.
  def apply(ts: TransmittalSheet, ctx: ValidationContext): Result = {
    new S025(ctx.institution.get).apply(ts)
  }

}

class S025(institution: Institution) extends EditCheck[LoanApplicationRegister] {
  def name = "S025"

  def apply(ts: TransmittalSheet): Result = {
    compare(ts.respondent.id, ts.agencyCode)
  }

  def apply(lar: LoanApplicationRegister): Result = {
    compare(lar.respondentId, lar.agencyCode)
  }

  private def compare(filingRespId: String, filingAgencyCode: Int): Result = {
    institution.respondentId match {
      case Left(invalid) => new Failure()
      case Right(validRespId) => {
        (filingRespId is equalTo(validRespId.id)) and (filingAgencyCode is equalTo(institution.agency.value))
      }
    }
  }
}

class EmptyEditCheck extends EditCheck[LoanApplicationRegister] {
  def name = "empty"
  def apply(lar: LoanApplicationRegister): Result = Success()
  def apply(transmittalSheet: TransmittalSheet): Result = Success()
}

{'fraction_non_alphanumeric': 0.10038891589693728, 'fraction_numerical': 0.008021390374331552, 'mean_word_length': 3.9758162031438937, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '1465744', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1248, 'n_words': 318}