okf-core-0.14.0.0: src/Okf/Lifecycle.hs
-- | Select stable concepts, following the replacement paths of deprecated
-- concepts through the same bounded inventory used by the graph.
module Okf.Lifecycle (ReplacementIssue (..), currentKnowledge) where
import Data.Either (partitionEithers)
import Data.Map.Strict qualified as Map
import Data.Maybe (catMaybes)
import Data.Set qualified as Set
import Okf.Bundle
import Okf.ConceptId (ConceptId)
import Okf.Document (Status (..))
import Okf.Index (VersionDeclaration, frontmatterReferenceContext)
import Okf.Path (PathResolution (..), resolveFileReference)
import Okf.Prelude
data ReplacementIssue
= MissingReplacement !ConceptId !Text
| ReplacementCycle !ConceptId !ConceptId
deriving stock (Eq, Ord, Show)
-- | The last argument supplies initial matches. Replacements can come from
-- anywhere in the full first concept list, and each stable result appears once.
currentKnowledge :: VersionDeclaration -> BundleInventory -> [Concept] -> [Concept] -> ([Concept], [ReplacementIssue])
currentKnowledge declaration inventory concepts initialMatches =
(Map.elems (Map.fromList [(conceptIdOf result, result) | result <- catMaybes selected]), Set.toAscList (Set.fromList issues))
where
byPath = Map.fromList [(conceptSourcePath concept, concept) | concept <- concepts]
context = frontmatterReferenceContext declaration
(issues, selected) = partitionEithers [follow (conceptIdOf start) Set.empty start | start <- initialMatches]
follow origin visited current
| Set.member currentId visited = Left (ReplacementCycle origin currentId)
| otherwise =
case conceptStatus current of
Stable -> Right (Just current)
Deprecated
| Just raw <- conceptSupersededBy current ->
case resolveFileReference (flip bundleInventoryMember inventory) context (conceptSourcePath current) raw of
ResolvedInBundle target
| Just replacement <- Map.lookup target byPath ->
follow origin (Set.insert currentId visited) replacement
_ -> Left (MissingReplacement currentId raw)
_ -> Right Nothing
where
currentId = conceptIdOf current