Skip to content

Commit ed460c1

Browse files
Kroissetirr-c
authored andcommitted
Build filepath more natural
1 parent dd1e0bc commit ed460c1

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/Nirum/Targets/Rust.hs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,9 @@ compilePackage' package =
8686
resolveWithModulePath :: ModulePath -> Maybe (BoundModule Rust)
8787
resolveWithModulePath mp = resolveBoundModule mp package
8888
modules' :: [RustModule]
89-
modules' = libModule : [ RustModule { filePath = toFilePath $ toList mp
90-
, modPath = mp
91-
, children = childModules expanded' mp
92-
}
93-
| mp <- toList expanded' ]
89+
modules' = libModule : map (toRustModule expanded') (toList expanded')
9490
expanded' = hierarchies $ S.fromList $ MS.keys $ modules package
95-
libModule = RustModule { filePath = toFilePath []
91+
libModule = RustModule { filePath = toFilePath True ["lib"]
9692
, modPath = ["lib"]
9793
, children = S.map root expanded'
9894
}
@@ -102,11 +98,21 @@ childModules modPaths base = fromMaybe mempty $ getOption $ foldMap f modPaths
10298
where
10399
f = Option . fmap (S.singleton . root) . stripPrefix base
104100

105-
toFilePath :: [I.Identifier] -> FilePath
106-
toFilePath [] = joinPath ["src", "lib.rs"]
107-
toFilePath p = joinPath (["src"] ++ convert p ++ ["mod.rs"])
101+
toFilePath :: Bool -> ModulePath -> FilePath
102+
toFilePath isLeaf p = joinPath ("src" : converted) ++ ".rs"
108103
where
109-
convert = map (T.unpack . toRustIdentifier I.toSnakeCaseText)
104+
convert = map (toRustIdentifier I.toSnakeCaseText) . toList
105+
converted = map T.unpack $ convert p ++ (if isLeaf then [] else ["mod"])
106+
107+
toRustModule :: Foldable f => f ModulePath -> ModulePath -> RustModule
108+
toRustModule modPaths mp = RustModule { filePath = filePath'
109+
, modPath = mp
110+
, children = children'
111+
}
112+
where
113+
children' = childModules modPaths mp
114+
isLeaf = S.null children'
115+
filePath' = toFilePath isLeaf mp
110116

111117
instance Target Rust where
112118
type CompileResult Rust = Code

0 commit comments

Comments
 (0)