profi248 / pluto

[in development] Easily make encrypted backups to other users, in exchange just for your unused disk space!
GNU General Public License v3.0
3 stars 0 forks source link

topics macro #4

Closed nebneb0703 closed 2 years ago

nebneb0703 commented 2 years ago

Topic structs:

Topic enum:

Example syntax:


pluto_macros::define_topics! {
    Coordinator {
        #[topic = "coordinator/auth"]
        #[message = AuthNodeInit]
        Auth
    },

    Node {
        #[topic = "node/{id}/auth"]
        Auth
    }
}

generates into:


pub enum Topic {
    Coordinator(CoordinatorTopic),
    Node(NodeTopic),
}

pub enum CoordinatorTopic {
    Auth
}

pub enum NodeTopic {
    Auth
}

pub struct NodeAuthTopic;

impl NodeAuthTopic {
    pub topic(&self, id: impl Into<String>) -> String {
        format!("node/{id}/auth")
    }
}

pub struct CoordinatorAuthTopic;

impl CoordinatorAuthTopic {
    pub topic(&self) -> String {
        "coordinator/auth".to_owned()
    }

    pub fn message(&self) -> AuthNodeInit {
        Default::default()
    }
}

Helper macro for nested topics:


let t: NodeAuthTopic = topic!(Node::Auth);
let t: NodeAuthTopic = Topic::Node(NodeTopic::Auth).get();
nebneb0703 commented 2 years ago

move topic attribute as just a literal string because all topics are gonna have a topic string

nebneb0703 commented 2 years ago

i am really fucking stupid, of course get() -> S cannot work.

generate a macro (via the proc macro hehe) to convert topic types, like topic! was already shown to do.

nebneb0703 commented 2 years ago

realised an issue with the current implementation on topics branch, the generated topic! macro doesn't have the :: syntax i wanted. implement a proper context for this, maybe using a vec of literals so we can choose how to join them, or just go the lazy route and create a second context which uses :: as the separator.

nebneb0703 commented 2 years ago

Merged with #6