import type { ImplBlock, KindBadge, LibraryRule, PropertyRow, RuleCard, TraitBlock } from '../contract' import { implRows, plainSay, traitRows } from './rustRows' /** 和函数卡同一个黄,实现块看起来仍是函数那一挂。 */ const IMPL: KindBadge = { label: '实现', color: { dark: '#dcdcaa', light: '#795e26' }, icon: 'square-function' } /** 和声明关键字同一个蓝,同特质这个种类在预览里的颜色。 */ const TRAIT: KindBadge = { label: '特质', color: { dark: '#569cd6', light: '#0000ff' }, icon: 'network' } /** * impl、trait 的方法表:点方法名展开那张函数卡,函数体各句在卡里。 * 方法挂在方法表的子行上(RuleSlot)。不看导入,Rust 文件都启用。 */ export const rustImplRule: LibraryRule = { id: 'rust', label: 'Rust 实现与特质', languageIds: ['rust'], libraries: [], claim: (block, context) => { const say = plainSay(context.doc) if (block.kind === 'impl') return methodsCard(block, 'rust.impl', IMPL, implRows(block, say)) if (block.kind === 'trait') return methodsCard(block, 'rust.trait', TRAIT, traitRows(block, say)) return undefined }, attributeNames: () => ({ attributes: {}, values: { kind: { 'rust.impl': '实现', 'rust.trait': '特质' } }, }), } /** 带方法表的卡片:子块里的每个函数挂在「方法」那一行讲它的子行上,跟 rows 里的顺序一一对上。 */ function methodsCard(block: ImplBlock | TraitBlock, kind: string, badge: KindBadge, rows: PropertyRow[]): RuleCard { const row = rows.findIndex(item => item.label === '方法') const methods = block.children.filter(child => child.kind === 'function') return { kind, badge, rows, ...(block.facts.name && { defines: { startIndex: block.facts.name.startIndex, endIndex: block.facts.name.endIndex } }), absorbs: [], ...(row >= 0 && { slots: methods.map((child, item) => ({ block: child, row, item })) }), } }