T
TypeDB4w ago
sevki

Derive Macro for rust

Hey folks, I'm Sevki, I'm new here and I've been working on a typedb macro, I've already posted it to the forums https://forum.typedb.com/t/derive-macro-for-rust/701 , like I said, I'm super new so if anyone wants to help me out I would really apprecitate it
Discussion Forum
Derive Macro for rust
Hi folks, I’m Sevki, I’m new here. I have been searching for a more rusty™️ way to generate schemas. something like, #[derive(Relation)] enum Employment { #[role] Employee, #[role] Employer, } // Rust code #[derive(Entity)] #[abs] pub struct Person { #[key] email: String, #[owns] full_name: String, ...
9 Replies
krishnan
krishnan4w ago
That's super interesting and similar to what @Joshua (and @dmitrii? ) have been thinking about. We'll be happy to answer any TypeQL questions you may have in this thread. I've struggled to wrap my head around fitting relations into rust. I'm curious why you've not re-used the struct Person for struct PersonAnswer.
sevki
sevkiOP4w ago
@krishnan That's very kind of you. I'm currently in a very exploratory state. I don't really have a nice way to map PERA concepts to rust idioms. So I'm trying a bunch of things to see what feels more natural.
sevki
sevkiOP2w ago
Hey folks, I've got a poc for this... https://github.com/sevki/typedb-model/blob/main/tests/integration_tests.rs it's not polished or anything of the sort. still very early days and still trying to figure out what the user experience should be.
GitHub
typedb-model/tests/integration_tests.rs at main · sevki/typedb-model
TypeDB model generation from with Rust macros. Contribute to sevki/typedb-model development by creating an account on GitHub.
Joshua
Joshua7d ago
ok taking a look @sevki sorry been super busy!
sevki
sevkiOP7d ago
@Joshua actually please don't. that implementation is absolute shite. and turns out I was making things super hard for my self. I rewrote the entire thing in a couple of lines of datalog. what an idiot I was two days ago. I literally wrote a couple of fmts for the defineables to make these
attribute id;
id value string;
entity user;
user owns id;
attribute id;
id value string;
entity user;
user owns id;
into these
attributes(id).
attribute_type(id, sting) :- attributes(id), value_types(string).
entities(user).
owns(user, id)
attributes(id).
attribute_type(id, sting) :- attributes(id), value_types(string).
entities(user).
owns(user, id)
and it worked surprisingly well. so now i can do write stuff like this
% prelude
value_types(string).
value_types(double).
value_types(date).
value_types(bool).
value_types(float).
% auto gen starts here
attributes(id).
attribute_type(id,string) :- attributes(id), value_types(string).
attributes(name).
attribute_type(name,string) :- attributes(name), value_types(string).
attributes(description).
attribute_type(description,string) :- attributes(description), value_types(string).
attributes(kid).
attribute_type(kid,string) :- attributes(kid), value_types(string).
attributes(created_at).
attribute_type(created_at,date) :- attributes(created_at), value_types(date).abstract(content).
entities(content).
owns(content, id) :- entities(content), attributes(id).
abstract(account).
entities(account).
owns(account, id) :- entities(account), attributes(id).
abstract(pubkey).
entities(pubkey).
owns(pubkey, kid) :- entities(pubkey), attributes(kid).
abstract(credential).
relations(credential).
relation(credential,key).
relation(credential,subject).
entities(user).
sub(user, account).
owns(user, name) :- entities(user), attributes(name).
entities(service).
sub(service, account).
owns(service, description) :- entities(service), attributes(description).
entities(passkey).
sub(passkey, key).
plays(passkey, key) :- entities(passkey), roles(key).
% my program
sub(?X,?Y) :- sub(?X,?Y).

sub(?X,?Z) :- sub(?X,?Y), sub(?Y,?Z).

owns(?X,?Y) :- owns(?X,?Y).

owns(?X,?Z) :- sub(?X, ?Y), owns(?Y,?Z).

abstract_struct_field_types(?X, ?Y, ?Z) :- abstract(?X), owns(?X, ?Y), attribute_type(?Y, ?Z).
struct_field_types(?X, ?Y, ?Z) :- owns(?X, ?Y), attribute_type(?Y, ?Z).
% prelude
value_types(string).
value_types(double).
value_types(date).
value_types(bool).
value_types(float).
% auto gen starts here
attributes(id).
attribute_type(id,string) :- attributes(id), value_types(string).
attributes(name).
attribute_type(name,string) :- attributes(name), value_types(string).
attributes(description).
attribute_type(description,string) :- attributes(description), value_types(string).
attributes(kid).
attribute_type(kid,string) :- attributes(kid), value_types(string).
attributes(created_at).
attribute_type(created_at,date) :- attributes(created_at), value_types(date).abstract(content).
entities(content).
owns(content, id) :- entities(content), attributes(id).
abstract(account).
entities(account).
owns(account, id) :- entities(account), attributes(id).
abstract(pubkey).
entities(pubkey).
owns(pubkey, kid) :- entities(pubkey), attributes(kid).
abstract(credential).
relations(credential).
relation(credential,key).
relation(credential,subject).
entities(user).
sub(user, account).
owns(user, name) :- entities(user), attributes(name).
entities(service).
sub(service, account).
owns(service, description) :- entities(service), attributes(description).
entities(passkey).
sub(passkey, key).
plays(passkey, key) :- entities(passkey), roles(key).
% my program
sub(?X,?Y) :- sub(?X,?Y).

sub(?X,?Z) :- sub(?X,?Y), sub(?Y,?Z).

owns(?X,?Y) :- owns(?X,?Y).

owns(?X,?Z) :- sub(?X, ?Y), owns(?Y,?Z).

abstract_struct_field_types(?X, ?Y, ?Z) :- abstract(?X), owns(?X, ?Y), attribute_type(?Y, ?Z).
struct_field_types(?X, ?Y, ?Z) :- owns(?X, ?Y), attribute_type(?Y, ?Z).
sevki
sevkiOP7d ago
ah nice it even runs on the web https://dub.sh/2dIlb9L
Nemo Rule Engine - Web Browser IDE
Try Nemo online in your Web Browser: Nemo is a datalog-based rule engine for fast and scalable analytic data processing in memory.
Joshua
Joshua4d ago
how does this work with the rus tmacro? super curious to see where you take it @sevki ! I thought i saw a notification you sent about how you did it but i can't find it now @sevki !!
sevki
sevkiOP16h ago
Hey @joshua I hadn't. Almost finished my rewrite and I really like this version better. I'll publish it this week 🙂
Joshua
Joshua16h ago
thanks, im excited @sevki !

Did you find this page helpful?