fix: Types and nomenclature

This commit is contained in:
Sidharth Vinod 2023-09-14 22:46:43 +05:30
parent c41594d220
commit afd7cf51cf
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
3 changed files with 17 additions and 15 deletions

View File

@ -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 { log } from '../../logger.js';
import type { PacketDiagramConfig } from '../../config.type.js'; import type { PacketDiagramConfig } from '../../config.type.js';
import DEFAULT_CONFIG from '../../defaultConfig.js'; import DEFAULT_CONFIG from '../../defaultConfig.js';
import { getConfig as commonGetConfig } from '../../config.js'; import { getConfig as commonGetConfig } from '../../config.js';
interface PacketData { interface PacketData {
packet: Word[]; packet: Row[];
} }
const defaultPacketData: PacketData = { const defaultPacketData: PacketData = {
@ -22,21 +22,23 @@ export const getConfig = (): Required<PacketDiagramConfig> => {
}); });
}; };
export const getPacket = (): Word[] => data.packet; export const getPacket = (): Row[] => data.packet;
export const getNextFittingBlock = ( export const getNextFittingBlock = (
block: Block, block: Block,
row: number, row: number,
bitsPerRow: number bitsPerRow: number
): [Block, Block | undefined] => { ): [Required<Block>, Block | undefined] => {
block.end = block.end ?? block.start; if (block.end === undefined) {
block.end = block.start;
}
if (block.start > block.end) { if (block.start > block.end) {
throw new Error(`Block start ${block.start} is greater than block end ${block.end}.`); throw new Error(`Block start ${block.start} is greater than block end ${block.end}.`);
} }
if (block.end + 1 <= row * bitsPerRow) { if (block.end + 1 <= row * bitsPerRow) {
return [block, undefined]; return [block as Required<Block>, undefined];
} }
return [ return [
@ -55,12 +57,12 @@ export const getNextFittingBlock = (
export const populate = ({ blocks }: { blocks: Block[] }) => { export const populate = ({ blocks }: { blocks: Block[] }) => {
let lastByte = -1; let lastByte = -1;
let word: Block[] = []; let word: Row = [];
data.packet = []; data.packet = [];
let row = 1; let row = 1;
const { bitsPerRow } = getConfig(); const { bitsPerRow } = getConfig();
for (let { start, end, label } of blocks) { 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.`); throw new Error(`Packet block ${start} - ${end} is invalid. End must be greater than start.`);
} }
if (start != lastByte + 1) { if (start != lastByte + 1) {

View File

@ -1,7 +1,7 @@
import { configureSvgSize } from '../../setupGraphViewbox.js'; import { configureSvgSize } from '../../setupGraphViewbox.js';
import type { DrawDefinition, Group, SVG } from '../../diagram-api/types.js'; import type { DrawDefinition, Group, SVG } from '../../diagram-api/types.js';
import { selectSvgElement } from '../../rendering-util/selectSvgElement.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 { PacketDiagramConfig } from '../../config.type.js';
import type { Diagram } from '../../Diagram.js'; import type { Diagram } from '../../Diagram.js';
@ -25,13 +25,13 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => {
const drawWord = ( const drawWord = (
svg: SVG, svg: SVG,
word: Word, row: Row,
row: number, rowNumber: number,
{ rowHeight, paddingX, paddingY, bitWidth, bitsPerRow }: Required<PacketDiagramConfig> { rowHeight, paddingX, paddingY, bitWidth, bitsPerRow }: Required<PacketDiagramConfig>
) => { ) => {
const group: Group = svg.append('g'); const group: Group = svg.append('g');
const wordY = row * (rowHeight + paddingY) + paddingY; const wordY = rowNumber * (rowHeight + paddingY) + paddingY;
for (const block of word) { for (const block of row) {
const blockX = (block.start % bitsPerRow) * bitWidth + 1; const blockX = (block.start % bitsPerRow) * bitWidth + 1;
const width = (block.end - block.start + 1) * bitWidth - paddingX; const width = (block.end - block.start + 1) * bitWidth - paddingX;
// Block rectangle // Block rectangle

View File

@ -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 ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
export type Block = Pick<ArrayElement<Packet['blocks']>, 'start' | 'end' | 'label'>; export type Block = Pick<ArrayElement<Packet['blocks']>, 'start' | 'end' | 'label'>;
export type Word = Block[]; export type Row = Required<Block>[];
export interface PacketDB extends DiagramDB { export interface PacketDB extends DiagramDB {
getPacket: () => Word[]; getPacket: () => Row[];
} }