specify a Map<K,V> then we have to construct this from what? as Map::from([k,v,....]). type Map<K,V> = { [k in K]: V} o = JSON.parse(s) o.a.b.c = new Map.from(o.a.b.c) so we need the path... for each from the root like o.a[5].c.contents.blah etc. also for Set etc.
Grab random rust code with derive(Serialize) and see if my code works.
Problem with enums rendering as a string.
Maybe make a const enum as set of const strings e.g.
type Enum = "a" | "b" | "c"
If we want to honour #[serde(flatten)]
then this will only be possible
with TypeScriptify
. we can laydown a
trait TypeScriptifyTrait {
fn type_script_ify() -> String;
fn fields() -> Vec<String>;
}
Then we can flatten by finding the fields for a type with #T::<???>::fields()
Configure code generation via option_env!
. Yesss! (look at env_logger).
... baby steps.
function verify_#ident(obj: any): obj is #ident {
for let t in ["a", "b", ...] {
let v = obj[t];
if (v === undefined) return false; // change optional to a | null
// TODO more checks
}
return true;
}
for simple types (number, string) we can do if typeof v == "number"
etc.
might need to recurse into another verify_X function
for arrays #tp[] Array.isArray(v) && all(for x in v verify_#tp(x))
probably want two types a "shallow" verification (see above) and a deep one.
Maybe just say no... who is going to use generic types for data transfer?
Still there is the possibility that a type will use a monomorphisation of a generic struct.
Maybe a variants attribute:
#[typescript(variants(T="str + Vec<f64>"))]
struct S<T> { value: T}
With typescript as
export type S<T>
| { value: string }
| { value: number[]}
| { value: T}
// OR maybe just as before
export type S<T> { value: T}
export type S_s = { value: string }
export type S_vn = { value: number[] }
export const isa_S = (a:any): a is S => isa_S_s(a) || isa_S_vn(a);
export const isa_S_s (a:any) a is S_s => {/*....*/}
export const isa_S_vn (a:any) a is S_vn => {/*....*/}
// etc...
Type erasure would allow one to instaniate let v = S<number> = { value: 32 }
This would allow structs using this generic:
struct T { value: S<Vec<f64>> }
to generate automatically a isa_S_vn
verifier
export type T { value: S<number[]> }
export const isa_T = (a:any) a is T => { isa_S_vn(a.value)}
This will probably be last to have a verifier....
#[typescript(variants(T="str + Vec<f64>"))]
struct S2<T> {value: S<T> }
export type S2<T> =
| { value : S<string> }
| { value : S<number[]>}
| { value: S<T>}
maybe some better name mangling <Vec<Vec<f64>>>
go to vvn
Could get
arbitrarily complex...
Interesting that this seems to pass tsc
type IntMap = { intmap: { [key: number]: number } };
let v : IntMap = { intmap: { 2: 6, "6": 5, 4:5 } };
but { 2: 6, "6a": 5, 4:5 }
fails.
check Dates/times durations
So typescript has guard for global
for fields we have #[ts(type=,..., guard, rust=::ss::bb::Derive)] or use as
say for ::im::Vector #[ts(as="Vec")] or "alias"
write a config file
path=[a,b,c] generics=[K,V] expr="#K[]"
The for simple trans we have given TSType match path=[a,b,c] then parse(TSTYpe, expr) => string[]
for verifier parse(obj, TSType, expr) => {.....}
See notes for target https://users.rust-lang.org/t/target-dependent-default-features-in-cargo/11367/3 and here rust-lang/cargo#1197