UNPKG

gnablib

Version:

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

2 lines 3.48 kB
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */ import{FromBinResult as t}from"../primitive/FromBinResult.js";import{DateTimeLocal as o}from"../datetime/dt.js";import{ACmd as s}from"./ACmd.js";import{ColName as r}from"./ColName.js";import{CreateColDef as e}from"./CreateColDef.js";import{InsertColDef as n}from"./InsertColDef.js";import{CommandCtrl as l}from"./types/Command.js";import{Plane as i}from"./types/Plane.js";export class ACmdCtrl extends s{constructor(t,o,s,r){super(t,o,s,i.Control,r)}static fromBinSub(o,s,r,e,n,i,m,C){const c=l.fromByte(s);return c?c.isCreate?CmdCtrlCreate.fromBinSub(o,s,r,e,n,i,m,C):c.isInsCols?CmdCtrlInsCols.fromBinSub(o,s,r,e,n,i,m,C):c.isRemCols?CmdCtrlRemCols.fromBinSub(o,s,r,e,n,i,m,C):CmdCtrlDrop.fromBinSub(o,s,r,e,n,i,m,C):new t(0,void 0,"ACmdCtrl.fromBinSub missing command")}}export class CmdCtrlCreate extends ACmdCtrl{constructor(t,o,s,...r){super(t,o,s,l.Create),this.cols=r}toJSON(){const t=super.toJSON();return t.cols=this.cols,t}toBin(){const t=[];let o=0;for(const s of this.cols){const r=s.toBin();t.push(r),o+=r.length}const s=super.toBin(o);let r=s.length-o;for(const o of t)s.set(o,r),r+=o.length;return s}static Now(t,s,...r){return new CmdCtrlCreate(t,o.now(),s,...r)}static fromBinSub(o,s,r,n,l,i,m,C){let c=C;const u=[];let f=l;for(;f>0;){const o=e.fromBin(i,c);if(!o.success)return new t(0,void 0,`CmdCtrlCreate.fromBinSub missing col ${u.length}`);c+=o.byteLen,f-=o.byteLen,u.push(o.value)}return new t(m+l,new CmdCtrlCreate(r,o,n,...u))}}export class CmdCtrlInsCols extends ACmdCtrl{constructor(t,o,s,r,...e){super(t,o,s,l.InsCols),this.after=r,this.cols=e}toJSON(){const t=super.toJSON();return null!=this.after&&(t.after=this.after.name),t.cols=this.cols,t}toBin(){var t,o;const s=null===(t=this.after)||void 0===t?void 0:t.toBin(),r=[];let e=0;for(const t of this.cols){const o=t.toBin();r.push(o),e+=o.length}const n=(null!==(o=null==s?void 0:s.length)&&void 0!==o?o:0)+e,l=super.toBin(n);let i=l.length-n;s&&(l.set(s,i),i+=s.length);for(const t of r)l.set(t,i),i+=t.length;return l}static Now(t,s,r,...e){return new CmdCtrlInsCols(t,o.now(),s,r,...e)}static fromBinSub(o,s,e,l,i,m,C,c){let u=c,f=i;const d=[],a=r.fromBin(m,c);if(!a.success)return new t(0,void 0,"CmdCtrlInsCols.fromBinSub missing after-column: "+a.reason);for(u+=a.byteLen,f-=a.byteLen;f>0;){const o=n.fromBin(m,u);if(!o.success)return new t(0,void 0,`CmdCtrlInsCols.fromBinSub missing col ${d.length}: ${o.reason}`);u+=o.byteLen,f-=o.byteLen,d.push(o.value)}return new t(C+i,new CmdCtrlInsCols(e,o,l,a.value,...d))}}export class CmdCtrlRemCols extends ACmdCtrl{constructor(t,o,s,...r){super(t,o,s,l.RemCols),this.cols=r}toJSON(){const t=super.toJSON();return t.cols=this.cols,t}toBin(){const t=[];let o=0;for(const s of this.cols){const r=s.toBin();t.push(r),o+=r.length}const s=super.toBin(o);let r=s.length-o;for(const o of t)s.set(o,r),r+=o.length;return s}static Now(t,s,...r){return new CmdCtrlRemCols(t,o.now(),s,...r)}static fromBinSub(o,s,e,n,l,i,m,C){let c=C;const u=[];let f=l;for(;f>0;){const o=r.fromBin(i,c);if(!o.success)return new t(0,void 0,`CmdCtrlRemCols.fromBinSub missing col ${u.length}: ${o.reason}`);c+=o.byteLen,f-=o.byteLen,u.push(o.value)}return new t(m+l,new CmdCtrlRemCols(e,o,n,...u))}}export class CmdCtrlDrop extends ACmdCtrl{constructor(t,o,s){super(t,o,s,l.Drop)}static Now(t,s){return new CmdCtrlDrop(t,o.now(),s)}static fromBinSub(o,s,r,e,n,l,i,m){return new t(i+n,new CmdCtrlDrop(r,o,e))}}