-- | Converts crossreference data of a file to Kythe schema.
-- 'basevn' should have the corpus and language prefilled.
toKythe :: Raw.VName -> T.Text -> XRef -> ConduitT () Raw.Entry Identity ()
toKythe basevn content XRef{..} = do
    let NameAndEntries pkgvn pkgEntries =
            makePackageFacts basevn (mtPkgModule xrefModule)
        NameAndEntries filevn fileEntries =
            makeFileFacts basevn
                          (analysedOriginalPath xrefFile)
                          encodedContent
        env = ConversionEnv pkgvn table basevn
        -- Files are children of the package they belong to.
        -- This makes more sense for golang, but can't hurt here either.
        pkgFileEntry = edge filevn ChildOfE pkgvn
    sourceList (pkgFileEntry : pkgEntries ++ fileEntries)
    flip runReaderT env $ do
        stream (makeAnchor (mtSpan xrefModule) DefinesBindingE pkgvn
                           Nothing Nothing)
        mapM_ (stream . makeDeclFacts) xrefDecls
        mapM_ (stream . makeDocDeclFacts) xrefDocDecls
        mapM_ (stream . makeModuleDocDeclFacts) xrefModuleDocDecls
        mapM_ (stream . makeUsageFacts) xrefCrossRefs
        mapM_ (stream . makeRelationFacts) xrefRelations
        mapM_ (stream . makeImportFacts) xrefImports
  where
    table = Offset.createOffsetTable (TL.fromStrict content)
    encodedContent = T.encodeUtf8 content

-- | Changes a conversion returning a list of entries into one that streams
-- those entries. To be applied at reasonable points in the computation tree,
-- where already a decent but not too large amount of entries have accumulated
toKythe :: Raw.VName -> T.Text -> XRef -> ConduitT () Raw.Entry Identity ()
toKythe basevn content XRef{..} = do
    let NameAndEntries pkgvn pkgEntries =
            makePackageFacts basevn (mtPkgModule xrefModule)
        NameAndEntries filevn fileEntries =
            makeFileFacts basevn
                          (analysedOriginalPath xrefFile)
                          encodedContent
        env = ConversionEnv pkgvn table basevn
        -- Files are children of the package they belong to.
        -- This makes more sense for golang, but can't hurt here either.
        pkgFileEntry = edge filevn ChildOfE pkgvn
    sourceList (pkgFileEntry : pkgEntries ++ fileEntries)
    flip runReaderT env $ do
        stream (makeAnchor (mtSpan xrefModule) DefinesBindingE pkgvn
                           Nothing Nothing)
        mapM_ (stream . makeDeclFacts) xrefDecls
        mapM_ (stream . makeDocDeclFacts) xrefDocDecls
        mapM_ (stream . makeModuleDocDeclFacts) xrefModuleDocDecls
        mapM_ (stream . makeUsageFacts) xrefCrossRefs
        mapM_ (stream . makeRelationFacts) xrefRelations
        mapM_ (stream . makeImportFacts) xrefImports
  where
    table = Offset.createOffsetTable (TL.fromStrict content)
    encodedContent = T.encodeUtf8 content

-- | Changes a conversion returning a list of entries into one that streams
-- those entries. To be applied at reasonable points in the computation tree,
-- where already a decent but not too large amount of entries have accumulated
toKythe basevn content XRef{..} = do
    let NameAndEntries pkgvn pkgEntries =
            makePackageFacts basevn (mtPkgModule xrefModule)
        NameAndEntries filevn fileEntries =
            makeFileFacts basevn
                          (analysedOriginalPath xrefFile)
                          encodedContent
        env = ConversionEnv pkgvn table basevn
        -- Files are children of the package they belong to.
        -- This makes more sense for golang, but can't hurt here either.
        pkgFileEntry = edge filevn ChildOfE pkgvn
    sourceList (pkgFileEntry : pkgEntries ++ fileEntries)
    flip runReaderT env $ do
        stream (makeAnchor (mtSpan xrefModule) DefinesBindingE pkgvn
                           Nothing Nothing)
        mapM_ (stream . makeDeclFacts) xrefDecls
        mapM_ (stream . makeDocDeclFacts) xrefDocDecls
        mapM_ (stream . makeModuleDocDeclFacts) xrefModuleDocDecls
        mapM_ (stream . makeUsageFacts) xrefCrossRefs
        mapM_ (stream . makeRelationFacts) xrefRelations
        mapM_ (stream . makeImportFacts) xrefImports
  where
    table = Offset.createOffsetTable (TL.fromStrict content)
    encodedContent = T.encodeUtf8 content

-- | Changes a conversion returning a list of entries into one that streams
-- those entries. To be applied at reasonable points in the computation tree,
-- where already a decent but not too large amount of entries have accumulated