|
| 1 | +import type { RustDoc, Page, PageMember, ID } from './types'; |
| 2 | +import { PageMemberType, PageType } from './types/pages'; |
| 3 | +import { existsSync, readFileSync, mkdirSync, writeFileSync, readdirSync } from 'node:fs'; |
| 4 | +import { join, dirname } from 'node:path'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | +import { generateContent } from './generator'; |
| 7 | + |
| 8 | +async function parseModules( |
| 9 | + rootPath: string, |
| 10 | + baseUrl: string, |
| 11 | + rustdoc: RustDoc, |
| 12 | + isCrate: boolean = false |
| 13 | +): Promise<Page[]> { |
| 14 | + const pages: Page[] = []; |
| 15 | + if (isCrate) { |
| 16 | + const item = rustdoc.index[rustdoc.root]; |
| 17 | + const path = rustdoc.paths[rustdoc.root]; |
| 18 | + const members: PageMember[] = item.inner.module.items.map((id: ID) => { |
| 19 | + const member: PageMember = { |
| 20 | + type: PageMemberType.struct, |
| 21 | + item: rustdoc.index[id], |
| 22 | + path: rustdoc.paths[id], |
| 23 | + }; |
| 24 | + return member; |
| 25 | + }); |
| 26 | + pages.push({ |
| 27 | + type: PageType.crate, |
| 28 | + path: join(rootPath, path.path.slice(1).join('/'), 'index.md'), |
| 29 | + content: await generateContent(PageType.crate, { |
| 30 | + title: path.path.join('::'), |
| 31 | + description: item.docs ?? '', |
| 32 | + moduleUrl: baseUrl, |
| 33 | + members: members, |
| 34 | + }), |
| 35 | + }); |
| 36 | + } else { |
| 37 | + for (const id in rustdoc.paths) { |
| 38 | + const path = rustdoc.paths[id]; |
| 39 | + if (path.kind !== 'module' || path.crate_id !== 0 || id === rustdoc.root) continue; |
| 40 | + const item = rustdoc.index[id]; |
| 41 | + const members: PageMember[] = item.inner.module.items.map((id: ID) => { |
| 42 | + const member: PageMember = { |
| 43 | + type: PageMemberType.struct, |
| 44 | + item: rustdoc.index[id], |
| 45 | + path: rustdoc.paths[id], |
| 46 | + }; |
| 47 | + return member; |
| 48 | + }); |
| 49 | + pages.push({ |
| 50 | + type: PageType.module, |
| 51 | + path: join(rootPath, path.path.slice(1).join('/'), 'index.md'), |
| 52 | + content: await generateContent(PageType.module, { |
| 53 | + title: path.path.join('::'), |
| 54 | + description: item.docs ?? '', |
| 55 | + moduleUrl: baseUrl, |
| 56 | + members: members, |
| 57 | + }), |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + return pages; |
| 62 | +} |
| 63 | + |
| 64 | +async function parseStructs(rootPath: string, baseUrl: string, rustdoc: RustDoc): Promise<Page[]> { |
| 65 | + const pages: Page[] = []; |
| 66 | + return pages; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Parses a single JSON file |
| 71 | + */ |
| 72 | +export async function parseCrate( |
| 73 | + rustdoc: RustDoc, |
| 74 | + baseUrl: string, |
| 75 | + rootPath: string |
| 76 | +): Promise<Page[]> { |
| 77 | + let pages: Page[] = []; |
| 78 | + const crateItem = rustdoc.index[rustdoc.root]; |
| 79 | + |
| 80 | + for (const type in PageType) { |
| 81 | + switch (type) { |
| 82 | + case 'crate': |
| 83 | + pages = pages.concat(await parseModules(rootPath, baseUrl, rustdoc, true)); |
| 84 | + case 'module': |
| 85 | + pages = pages.concat(await parseModules(rootPath, baseUrl, rustdoc)); |
| 86 | + case 'struct': |
| 87 | + pages = pages.concat(await parseStructs(rootPath, baseUrl, rustdoc)); |
| 88 | + } |
| 89 | + } |
| 90 | + return pages; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Resolves the path to a json file for external crates |
| 95 | + * @param crate |
| 96 | + */ |
| 97 | +export async function crateResolver(crate: string): Promise<RustDoc | null> { |
| 98 | + const targetFolder = '../tauri/target'; |
| 99 | + for (const file of readdirSync(targetFolder)) { |
| 100 | + if (file !== crate + '.json') continue; |
| 101 | + |
| 102 | + const rustdoc: RustDoc = JSON.parse(readFileSync(join(targetFolder, file), 'utf-8')); |
| 103 | + rustdoc.name = crate; |
| 104 | + return rustdoc; |
| 105 | + } |
| 106 | + return null; |
| 107 | +} |
0 commit comments