Skip to content

Commit

Permalink
fix: Types and nomenclature
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Sep 14, 2023
1 parent c41594d commit afd7cf5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
18 changes: 10 additions & 8 deletions packages/mermaid/src/diagrams/packet/db.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Block, PacketDB, Word } from './types.js';
import type { Block, PacketDB, Row } from './types.js';
import { log } from '../../logger.js';
import type { PacketDiagramConfig } from '../../config.type.js';
import DEFAULT_CONFIG from '../../defaultConfig.js';
import { getConfig as commonGetConfig } from '../../config.js';

interface PacketData {
packet: Word[];
packet: Row[];
}

const defaultPacketData: PacketData = {
Expand All @@ -22,21 +22,23 @@ export const getConfig = (): Required<PacketDiagramConfig> => {
});
};

export const getPacket = (): Word[] => data.packet;
export const getPacket = (): Row[] => data.packet;

export const getNextFittingBlock = (
block: Block,
row: number,
bitsPerRow: number
): [Block, Block | undefined] => {
block.end = block.end ?? block.start;
): [Required<Block>, Block | undefined] => {
if (block.end === undefined) {
block.end = block.start;
}

if (block.start > block.end) {
throw new Error(`Block start ${block.start} is greater than block end ${block.end}.`);
}

if (block.end + 1 <= row * bitsPerRow) {
return [block, undefined];
return [block as Required<Block>, undefined];
}

return [
Expand All @@ -55,12 +57,12 @@ export const getNextFittingBlock = (

export const populate = ({ blocks }: { blocks: Block[] }) => {
let lastByte = -1;
let word: Block[] = [];
let word: Row = [];
data.packet = [];
let row = 1;
const { bitsPerRow } = getConfig();
for (let { start, end, label } of blocks) {
if (end < start) {
if (end && end < start) {
throw new Error(`Packet block ${start} - ${end} is invalid. End must be greater than start.`);
}
if (start != lastByte + 1) {
Expand Down
10 changes: 5 additions & 5 deletions packages/mermaid/src/diagrams/packet/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { configureSvgSize } from '../../setupGraphViewbox.js';
import type { DrawDefinition, Group, SVG } from '../../diagram-api/types.js';
import { selectSvgElement } from '../../rendering-util/selectSvgElement.js';
import type { PacketDB, Word } from './types.js';
import type { PacketDB, Row } from './types.js';
import type { PacketDiagramConfig } from '../../config.type.js';
import type { Diagram } from '../../Diagram.js';

Expand All @@ -25,13 +25,13 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {

const drawWord = (
svg: SVG,
word: Word,
row: number,
row: Row,
rowNumber: number,
{ rowHeight, paddingX, paddingY, bitWidth, bitsPerRow }: Required<PacketDiagramConfig>
) => {
const group: Group = svg.append('g');
const wordY = row * (rowHeight + paddingY) + paddingY;
for (const block of word) {
const wordY = rowNumber * (rowHeight + paddingY) + paddingY;
for (const block of row) {
const blockX = (block.start % bitsPerRow) * bitWidth + 1;
const width = (block.end - block.start + 1) * bitWidth - paddingX;
// Block rectangle
Expand Down
4 changes: 2 additions & 2 deletions packages/mermaid/src/diagrams/packet/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { DiagramDB } from '../../diagram-api/types.js';

export type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
export type Block = Pick<ArrayElement<Packet['blocks']>, 'start' | 'end' | 'label'>;
export type Word = Block[];
export type Row = Required<Block>[];

export interface PacketDB extends DiagramDB {
getPacket: () => Word[];
getPacket: () => Row[];
}

0 comments on commit afd7cf5

Please sign in to comment.