Skip to content

Commit

Permalink
feat: 初步支持解析工作流字段信息
Browse files Browse the repository at this point in the history
  • Loading branch information
Cnotech committed Dec 23, 2023
1 parent beb99e4 commit 14be237
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
8 changes: 8 additions & 0 deletions scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { genStructsWiki } from "./struct";
import { genContextWiki } from "./context";
import { genPermissionsWiki } from "./permission";
import { genStepsWiki } from "./step";
import { genWorkflowWiki } from "./workflow";

genStructsWiki(
{
Expand Down Expand Up @@ -94,3 +95,10 @@ genStepsWiki(
},
"4-steps",
);
genWorkflowWiki({
file: "@/types/workflow.rs",
top: {
title: "工作流",
description: "在步骤上附加的公共工作流字段定义。",
},
});
24 changes: 9 additions & 15 deletions scripts/struct/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { structRenderer } from "./markdown";
import { writeWiki } from "../writer";
import { splitBlock } from "../block";
import { type Top } from "../type";
import { parseTypeDeclaration } from "../utils";

// 读取 Rust 中的某个 struct,分析出所有字段信息
function parseStruct(fileInfo: FileInfo): FieldInfo[] {
Expand All @@ -21,22 +22,15 @@ function parseStruct(fileInfo: FileInfo): FieldInfo[] {

return splittedBlock.map(({ wiki, declaration, demo }) => {
// 解析字段名和类型
const m = declaration.match(/(\w+):\s?([\w<>()]+)/);
if (m) {
const [, name, rawType] = m;
const parsedDecl = parseTypeDeclaration(declaration);
if (parsedDecl) {
const { identifier, optional, name } = parsedDecl;
const enumValues = enumValuesMap[name];
const type: FieldInfo["type"] =
rawType.startsWith("Option<") && rawType.endsWith(">")
? {
identifier: rawType.slice(7, -1),
optional: true,
enum: enumValues,
}
: {
identifier: rawType,
optional: false,
enum: enumValues,
};
const type: FieldInfo["type"] = {
identifier,
optional,
enum: enumValues,
};
if (enumValues) {
if (type.identifier !== "String") {
throw new Error(
Expand Down
26 changes: 26 additions & 0 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,29 @@ export function parseFilePath(rawPath: string) {
}
return rawPath;
}

const DECL_REGEX = /(\w+):\s?([\w<>()]+)/;
// 输入类型申明行,返回解析结果
export function parseTypeDeclaration(decl: string):
| {
identifier: string;
optional: boolean;
name: string;
}
| undefined {
const m = decl.match(DECL_REGEX);
if (!m) return undefined;
const [, rawName, rawType] = m;
const name = rawName.startsWith("c_") ? rawName.slice(2) : rawName;
return rawType.startsWith("Option<") && rawType.endsWith(">")
? {
identifier: rawType.slice(7, -1),
optional: true,
name,
}
: {
identifier: rawType,
optional: false,
name,
};
}
14 changes: 14 additions & 0 deletions scripts/workflow/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type Top } from "../type";
import { splitBlock } from "../block";
import { parseTypeDeclaration } from "../utils";

export function genWorkflowWiki({ file, top }: { file: string; top: Top }) {
const blocks = splitBlock({
file,
startsWith: `pub struct WorkflowHeader`,
}).map((raw) => ({
...raw,
type: parseTypeDeclaration(raw.declaration),
}));
console.log(blocks);
}
7 changes: 6 additions & 1 deletion src/types/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct WorkflowHeader {
pub name: Option<String>, // 解析时如果没有提供 name 则人为赋 key 的 sentence case
/// 步骤名称,缺省使用步骤键的 sentence case
pub name: Option<String>,
/// 步骤类型
//@ 必须是[步骤](/nep/definition/4-steps.html)定义中的一种值
pub step: String,
/// 步骤执行条件
//@ 是合法的条件
pub c_if: Option<String>,
}

Expand Down

0 comments on commit 14be237

Please sign in to comment.