UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 3.45 kB
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */ import{FromBinResult as t}from"../primitive/FromBinResult.js";import{DateTimeLocal as e}from"../datetime/dt.js";import{ACmd as n}from"./ACmd.js";import{ColValue as r}from"./ColValue.js";import{CommandData as s}from"./types/Command.js";import{Plane as o}from"./types/Plane.js";import{fromGlScaleBytes as a,toGlScaleBytes as i}from"../primitive/number/xtUint.js";import{ParseProblem as c}from"../error/index.js";export class ACmdData extends n{constructor(t,e,n,r,s){super(t,e,n,o.Data,s),this.recId=r}toJSON(){const t=super.toJSON();return t.recId=this.recId,t}toBin(t=0){const e=i(this.recId),n=e.length+t,r=super.toBin(n);return r.set(e,r.length-n),r}static fromBinSub(e,n,r,o,a,i,c,m){const u=s.fromByte(n);return u?u.isInsert?CmdDataInsert.fromBinSub(e,n,r,o,a,i,c,m):u.isPut?CmdDataPut.fromBinSub(e,n,r,o,a,i,c,m):u.isPatch?CmdDataPatch.fromBinSub(e,n,r,o,a,i,c,m):CmdDataDelete.fromBinSub(e,n,r,o,a,i,c,m):new t(0,void 0,"ACmdData.fromBinSub missing command")}}class m extends ACmdData{constructor(t,e,n,r,s,...o){super(t,e,n,r,s),this.cols=o}toJSON(){const t=super.toJSON();return t.cols=this.cols,t}toBin(){const t=[];let e=0;for(const n of this.cols){const r=n.toBin();t.push(r),e+=r.length}const n=super.toBin(e);let r=n.length-e;for(const e of t)n.set(e,r),r+=e.length;return n}}export class CmdDataInsert extends m{constructor(t,e,n,r,...o){super(t,e,n,r,s.Insert,...o)}static Now(t,n,r,...s){return new CmdDataInsert(t,e.now(),n,r,...s)}static fromBinSub(e,n,s,o,i,m,u,d){let f=d,l=i;const D=a(m.subarray(f));if(D instanceof c)return new t(0,void 0,`CmdDataInsert.fromBinSub missing recId: ${D.reason}`);const[p,C]=D;f+=C,l-=C;const b=[];for(;l>0;){const e=r.fromBin(m,f);if(!e.success)return new t(0,void 0,`CmdDataInsert.fromBinSub missing col ${b.length}: ${e.reason}`);f+=e.byteLen,l-=e.byteLen,b.push(e.value)}return new t(u+i,new CmdDataInsert(s,e,o,p,...b))}}export class CmdDataPut extends m{constructor(t,e,n,r,...o){super(t,e,n,r,s.Put,...o)}static Now(t,n,r,...s){return new CmdDataPut(t,e.now(),n,r,...s)}static fromBinSub(e,n,s,o,i,m,u,d){let f=d,l=i;const D=a(m.subarray(f));if(D instanceof c)return new t(0,void 0,`CmdDataPut.fromBinSub missing recId: ${D.reason}`);const[p,C]=D;f+=C,l-=C;const b=[];for(;l>0;){const e=r.fromBin(m,f);if(!e.success)return new t(0,void 0,`CmdDataPut.fromBinSub missing col ${b.length}: ${e.reason}`);f+=e.byteLen,l-=e.byteLen,b.push(e.value)}return new t(u+i,new CmdDataPut(s,e,o,p,...b))}}export class CmdDataPatch extends m{constructor(t,e,n,r,...o){super(t,e,n,r,s.Patch,...o)}static Now(t,n,r,...s){return new CmdDataPatch(t,e.now(),n,r,...s)}static fromBinSub(e,n,s,o,i,m,u,d){let f=d,l=i;const D=a(m.subarray(f));if(D instanceof c)return new t(0,void 0,`CmdDataPatch.fromBinSub missing recId: ${D.reason}`);const[p,C]=D;f+=C,l-=C;const b=[];for(;l>0;){const e=r.fromBin(m,f);if(!e.success)return new t(0,void 0,`CmdDataPatch.fromBinSub missing col ${b.length}`);f+=e.byteLen,l-=e.byteLen,b.push(e.value)}return new t(u+i,new CmdDataPatch(s,e,o,p,...b))}}export class CmdDataDelete extends ACmdData{constructor(t,e,n,r){super(t,e,n,r,s.Delete)}static Now(t,n,r){return new CmdDataDelete(t,e.now(),n,r)}static fromBinSub(e,n,r,s,o,i,m,u){let d=u;const f=a(i.subarray(d));if(f instanceof c)return new t(0,void 0,`CmdDataDelete.fromBinSub missing recId: ${f.reason}`);const[l,D]=f;return d+=D,new t(m+o,new CmdDataDelete(r,e,s,l))}}