kafonek / kernel-sidecar-rs

Kernel Sidecar in Rust
MIT License
11 stars 1 forks source link

Reduce redundancy in enums #5

Open kafonek opened 11 months ago

kafonek commented 11 months ago

Lots of redundancy especially around Response. For the most part I want to be able to drop into the inner struct of the enum variant. Maybe look into enum-dispatch

kafonek commented 11 months ago

Running into problems trying to use enum-dispatch like this,

#[enum_dispatch::enum_dispatch(MessageUtils)]
#[derive(Debug)]
pub enum Response {
    Status(Message<Status>),
    KernelInfo(Message<KernelInfoReply>),
    Execute(Message<ExecuteReply>),
    Unmodeled(Message<UnmodeledContent>),
}

pub trait MessageUtils {
    fn parent_msg_id(&self) -> Option<String>;
    fn msg_type(&self) -> String;
}

impl<T> MessageUtils for Message<T> {
    fn parent_msg_id(&self) -> Option<String> {
        match &self.parent_header {
            Some(header) => Some(header.msg_id.to_owned()),
            None => None,
        }
    }

    fn msg_type(&self) -> String {
        self.header.msg_type.to_owned()
    }
}

The following syntax raises errors, no method named parent_msg_id found for enum Response in the current scope / MessageUtil defines an item parent_msg_id, perhaps you need to implement it

let response: Response = zmq_msg.into();
let msg_id = response.parent_msg_id().unwrap();

think this issue is captured in https://gitlab.com/antonok/enum_dispatch/-/issues/67.

kafonek commented 10 months ago

Notebook.cells is a Vec<Cell> where Cell is an enum of Markdown / code / raw, etc. It's a pain to drill down into Cell.id / Cell.source there.