|
| 1 | +{-# LANGUAGE OverloadedLists, TypeSynonymInstances #-} |
| 2 | +module Nirum.Targets.Rust.ModuleTree ( RustModule ( RustModule |
| 3 | + , filePath |
| 4 | + , modPath |
| 5 | + , children |
| 6 | + ) |
| 7 | + , buildRustModuleList |
| 8 | + ) where |
| 9 | + |
| 10 | +import Data.List |
| 11 | +import qualified Data.Text as T |
| 12 | +import Data.Tree |
| 13 | + |
| 14 | +import GHC.Exts (IsList (toList)) |
| 15 | + |
| 16 | +import System.FilePath (joinPath) |
| 17 | + |
| 18 | +import Nirum.Constructs.Identifier |
| 19 | +import Nirum.Constructs.ModulePath (ModulePath) |
| 20 | +import Nirum.Targets.Rust.Keyword |
| 21 | + |
| 22 | +data UnpackedModule = UnpackedModule { unpackedModulePath :: [Identifier] |
| 23 | + , originalModulePath :: ModulePath |
| 24 | + } |
| 25 | +data ModuleNode = ModuleNode { moduleName :: Identifier |
| 26 | + , moduleNodePath :: Maybe ModulePath |
| 27 | + } |
| 28 | + |
| 29 | +instance Eq UnpackedModule where |
| 30 | + a == b = (unpackedModulePath a) == (unpackedModulePath b) |
| 31 | +instance Ord UnpackedModule where |
| 32 | + a <= b = (unpackedModulePath a) <= (unpackedModulePath b) |
| 33 | + |
| 34 | +type ModuleTree = Tree ModuleNode |
| 35 | + |
| 36 | +data RustModule = RustModule { filePath :: FilePath |
| 37 | + , modPath :: Maybe ModulePath |
| 38 | + , children :: [Identifier] |
| 39 | + } |
| 40 | + |
| 41 | +-- type a = Identifier |
| 42 | +-- type b = (Identifier, [UnpackedModule]) |
| 43 | +-- moduleUnfolder :: b -> (a, [b]) |
| 44 | +moduleUnfolder :: (ModuleNode, [UnpackedModule]) |
| 45 | + -> (ModuleNode, [(ModuleNode, [UnpackedModule])]) |
| 46 | +moduleUnfolder (ident, mps) = |
| 47 | + (ident, groupByParent mps) |
| 48 | + where |
| 49 | + isParentEqual :: UnpackedModule -> UnpackedModule -> Bool |
| 50 | + isParentEqual UnpackedModule { unpackedModulePath = (a:_) } |
| 51 | + UnpackedModule { unpackedModulePath = (b:_) } = |
| 52 | + a == b |
| 53 | + isParentEqual _ _ = False |
| 54 | + extractCommonParent :: [UnpackedModule] -> (ModuleNode, [UnpackedModule]) |
| 55 | + extractCommonParent mps' = |
| 56 | + ( ModuleNode { moduleName = commonParent |
| 57 | + , moduleNodePath = maybeModulePath |
| 58 | + } |
| 59 | + , [ UnpackedModule { unpackedModulePath = x:xs |
| 60 | + , originalModulePath = omn |
| 61 | + } |
| 62 | + | UnpackedModule { unpackedModulePath = x:xs |
| 63 | + , originalModulePath = omn |
| 64 | + } <- mps' |
| 65 | + ] |
| 66 | + ) |
| 67 | + where |
| 68 | + commonParent :: Identifier |
| 69 | + commonParent = head $ unpackedModulePath $ head mps' |
| 70 | + maybeModulePath :: Maybe ModulePath |
| 71 | + maybeModulePath = |
| 72 | + fmap originalModulePath $ |
| 73 | + find (((==) 1) . length . unpackedModulePath) mps' |
| 74 | + groupByParent :: [UnpackedModule] -> [(ModuleNode, [UnpackedModule])] |
| 75 | + groupByParent = (map extractCommonParent) . (groupBy isParentEqual) . sort |
| 76 | + |
| 77 | +buildModuleTree :: [ModulePath] -> ModuleTree |
| 78 | +buildModuleTree mps = |
| 79 | + unfoldTree moduleUnfolder seed |
| 80 | + where |
| 81 | + srcModule :: ModuleNode |
| 82 | + srcModule = ModuleNode { moduleName = "src" |
| 83 | + , moduleNodePath = Nothing |
| 84 | + } |
| 85 | + seed :: (ModuleNode, [UnpackedModule]) |
| 86 | + seed = ( srcModule |
| 87 | + , [ UnpackedModule { unpackedModulePath = toList mp |
| 88 | + , originalModulePath = mp |
| 89 | + } |
| 90 | + | mp <- mps |
| 91 | + ] |
| 92 | + ) |
| 93 | + |
| 94 | +toRustModuleList :: [String] -> ModuleTree -> [RustModule] |
| 95 | +toRustModuleList baseDir Node { rootLabel = ModuleNode { moduleName = modName |
| 96 | + , moduleNodePath = modPath' |
| 97 | + } |
| 98 | + , subForest = children' |
| 99 | + } = |
| 100 | + RustModule { filePath = joinPath identPath |
| 101 | + , modPath = modPath' |
| 102 | + , children = map (moduleName . rootLabel) children' |
| 103 | + } : |
| 104 | + (concat $ map (toRustModuleList identPath) children') |
| 105 | + where |
| 106 | + libOrMod :: String |
| 107 | + libOrMod = case baseDir of |
| 108 | + [] -> "lib.rs" |
| 109 | + _ -> "mod.rs" |
| 110 | + identPath :: [String] |
| 111 | + identPath = |
| 112 | + baseDir ++ |
| 113 | + [T.unpack $ toRustIdentifier toSnakeCaseText modName] ++ |
| 114 | + [libOrMod] |
| 115 | + |
| 116 | +buildRustModuleList :: [ModulePath] -> [RustModule] |
| 117 | +buildRustModuleList = (toRustModuleList []) . buildModuleTree |
0 commit comments