Move cache for final project evaluation into separate module

This commit is contained in:
Jeroen Engels 2023-04-14 18:41:04 +02:00
parent 02e73bf24c
commit 865b3e177c
3 changed files with 57 additions and 53 deletions

View File

@ -1,46 +0,0 @@
module Review.Cache exposing
( FinalProjectEvaluationCache
, createFinalProjectEvaluationCache
, errorsForFinalProjectEvaluationCache
, matchFinalProjectEvaluationCache
, setErrorsForFinalProjectEvaluationCache
)
import Review.Cache.ContextHash exposing (ComparableContextHash, ContextHash)
{-| Variant for final operations like the final evaluation or the extract
-}
type FinalProjectEvaluationCache output context
= EntryNoOutputContext
{ inputContextHashes : ComparableContextHash context
, output : output
}
createFinalProjectEvaluationCache : ComparableContextHash context -> output -> FinalProjectEvaluationCache output context
createFinalProjectEvaluationCache inputContextHashes output =
EntryNoOutputContext
{ inputContextHashes = inputContextHashes
, output = output
}
matchFinalProjectEvaluationCache : ComparableContextHash context -> FinalProjectEvaluationCache error context -> Bool
matchFinalProjectEvaluationCache context (EntryNoOutputContext entry) =
context == entry.inputContextHashes
errorsForFinalProjectEvaluationCache : FinalProjectEvaluationCache output context -> output
errorsForFinalProjectEvaluationCache (EntryNoOutputContext entry) =
entry.output
setErrorsForFinalProjectEvaluationCache : output -> Maybe (FinalProjectEvaluationCache output context) -> Maybe (FinalProjectEvaluationCache output context)
setErrorsForFinalProjectEvaluationCache newOutput maybeEntry =
case maybeEntry of
Just (EntryNoOutputContext entry) ->
Just (EntryNoOutputContext { inputContextHashes = entry.inputContextHashes, output = newOutput })
Nothing ->
Nothing

View File

@ -0,0 +1,50 @@
module Review.Cache.FinalProjectEvaluationCache exposing
( Entry, create
, match
, errors, setErrors
)
{-| Cache for the result of the final project evaluation analysis.
@docs Entry, create
@docs match
@docs errors, setErrors
-}
import Review.Cache.ContextHash exposing (ComparableContextHash, ContextHash)
type Entry output context
= Entry
{ inputContextHashes : ComparableContextHash context
, output : output
}
create : ComparableContextHash context -> output -> Entry output context
create inputContextHashes output =
Entry
{ inputContextHashes = inputContextHashes
, output = output
}
match : ComparableContextHash context -> Entry error context -> Bool
match context (Entry entry) =
context == entry.inputContextHashes
errors : Entry output context -> output
errors (Entry entry) =
entry.output
setErrors : output -> Maybe (Entry output context) -> Maybe (Entry output context)
setErrors newOutput maybeEntry =
case maybeEntry of
Just (Entry entry) ->
Just (Entry { inputContextHashes = entry.inputContextHashes, output = newOutput })
Nothing ->
Nothing

View File

@ -307,9 +307,9 @@ import Elm.Syntax.Node as Node exposing (Node(..))
import Elm.Syntax.Pattern exposing (Pattern)
import Elm.Syntax.Range as Range exposing (Range)
import Json.Encode as Encode
import Review.Cache as Cache
import Review.Cache.ContentHash exposing (ContentHash)
import Review.Cache.ContextHash as ContextHash exposing (ComparableContextHash, ContextHash)
import Review.Cache.FinalProjectEvaluationCache as FinalProjectEvaluationCache
import Review.Cache.Module as ModuleCache
import Review.Cache.ProjectFile as ProjectFileCache
import Review.ElmProjectEncoder
@ -4164,7 +4164,7 @@ type alias ProjectFileCache projectContext =
type alias FinalProjectEvaluationCache projectContext =
Cache.FinalProjectEvaluationCache (List (Error {})) projectContext
FinalProjectEvaluationCache.Entry (List (Error {})) projectContext
type alias ExtractCache projectContext =
@ -4295,7 +4295,7 @@ errorsFromCache cache =
, ProjectFileCache.errorsForMaybe cache.elmJson
, ProjectFileCache.errorsForMaybe cache.readme
, ProjectFileCache.errorsForMaybe cache.dependencies
, Maybe.map Cache.errorsForFinalProjectEvaluationCache cache.finalEvaluationErrors |> Maybe.withDefault []
, Maybe.map FinalProjectEvaluationCache.errors cache.finalEvaluationErrors |> Maybe.withDefault []
]
@ -5458,7 +5458,7 @@ createRuleProjectVisitor schema initialProject ruleData initialCache =
, setErrorsForElmJson = \newErrors -> raiseCache { cache | elmJson = ProjectFileCache.setErrors newErrors cache.elmJson }
, setErrorsForReadme = \newErrors -> raiseCache { cache | readme = ProjectFileCache.setErrors newErrors cache.readme }
, setErrorsForDependencies = \newErrors -> raiseCache { cache | dependencies = ProjectFileCache.setErrors newErrors cache.dependencies }
, setErrorsForFinalEvaluation = \newErrors -> raiseCache { cache | finalEvaluationErrors = Cache.setErrorsForFinalProjectEvaluationCache newErrors cache.finalEvaluationErrors }
, setErrorsForFinalEvaluation = \newErrors -> raiseCache { cache | finalEvaluationErrors = FinalProjectEvaluationCache.setErrors newErrors cache.finalEvaluationErrors }
, backToRule =
\() ->
Rule
@ -5656,11 +5656,11 @@ createFinalProjectEvaluationVisitor schema { exceptions } raise cache =
cachePredicate : FinalProjectEvaluationCache projectContext -> Bool
cachePredicate entry =
Cache.matchFinalProjectEvaluationCache inputContextHashes entry
FinalProjectEvaluationCache.match inputContextHashes entry
in
case reuseProjectRuleCache cachePredicate .finalEvaluationErrors cache of
Just entry ->
( Cache.errorsForFinalProjectEvaluationCache entry, raise cache )
( FinalProjectEvaluationCache.errors entry, raise cache )
Nothing ->
let
@ -5672,7 +5672,7 @@ createFinalProjectEvaluationVisitor schema { exceptions } raise cache =
|> filterExceptionsAndSetName exceptions schema.name
in
( errors
, raise { cache | finalEvaluationErrors = Just (Cache.createFinalProjectEvaluationCache inputContextHashes errors) }
, raise { cache | finalEvaluationErrors = Just (FinalProjectEvaluationCache.create inputContextHashes errors) }
)
)