Repository: servo/servo
File: components/script/dom/extendablemessageevent.rs (Rust)

Code before:
use stylo_atoms::Atom;

use crate::dom::bindings::codegen::Bindings::ExtendableEventBinding::ExtendableEvent_Binding::ExtendableEventMethods;
use crate::dom::bindings::codegen::Bindings::ExtendableMessageEventBinding;
use crate::dom::bindings::codegen::Bindings::ExtendableMessageEventBinding::ExtendableMessageEventMethods;
use crate::dom::bindings::codegen::UnionTypes::ClientOrServiceWorkerOrMessagePort;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::frozenarray::CachedFrozenArray;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::trace::RootedTraceableBox;
use crate::dom::client::Client;
use crate::dom::event::Event;
use crate::dom::eventtarget::EventTarget;
use crate::dom::extendableevent::ExtendableEvent;
use crate::dom::globalscope::GlobalScope;
use crate::dom::messageport::MessagePort;
use crate::dom::serviceworker::ServiceWorker;
use crate::dom::serviceworkerglobalscope::ServiceWorkerGlobalScope;
use crate::script_runtime::{CanGc, JSContext};

#[derive(Clone, JSTraceable, MallocSizeOf)]
pub(crate) enum MessageSource {
    Client(DomRoot<Client>),
    ServiceWorker(DomRoot<ServiceWorker>),
    MessagePort(DomRoot<MessagePort>),
}

impl From<ClientOrServiceWorkerOrMessagePort> for MessageSource {
    fn from(value: ClientOrServiceWorkerOrMessagePort) -> Self {
        match value {
            ClientOrServiceWorkerOrMessagePort::Client(client) => MessageSource::Client(client),
            ClientOrServiceWorkerOrMessagePort::ServiceWorker(sw) => {
                MessageSource::ServiceWorker(sw)
            },
            ClientOrServiceWorkerOrMessagePort::MessagePort(port) => {
                MessageSource::MessagePort(port)
            },
        }
    }
}

impl From<&ClientOrServiceWorkerOrMessagePort> for MessageSource {
    fn from(value: &ClientOrServiceWorkerOrMessagePort) -> Self {
        match value {
            ClientOrServiceWorkerOrMessagePort::Client(client) => {
                MessageSource::Client(DomRoot::from_ref(client))
            },
            ClientOrServiceWorkerOrMessagePort::ServiceWorker(sw) => {

Review comment (suggestion):
Nit: add link to spec as well:

```suggestion
/// <https://w3c.github.io/ServiceWorker/#dom-extendablemessageevent-source>
pub(crate) enum MessageSource {
```

Code after:
use stylo_atoms::Atom;

use crate::dom::bindings::codegen::Bindings::ExtendableEventBinding::ExtendableEvent_Binding::ExtendableEventMethods;
use crate::dom::bindings::codegen::Bindings::ExtendableMessageEventBinding;
use crate::dom::bindings::codegen::Bindings::ExtendableMessageEventBinding::ExtendableMessageEventMethods;
use crate::dom::bindings::codegen::UnionTypes::ClientOrServiceWorkerOrMessagePort;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::frozenarray::CachedFrozenArray;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::trace::RootedTraceableBox;
use crate::dom::client::Client;
use crate::dom::event::Event;
use crate::dom::eventtarget::EventTarget;
use crate::dom::extendableevent::ExtendableEvent;
use crate::dom::globalscope::GlobalScope;
use crate::dom::messageport::MessagePort;
use crate::dom::serviceworker::ServiceWorker;
use crate::dom::serviceworkerglobalscope::ServiceWorkerGlobalScope;
use crate::script_runtime::{CanGc, JSContext};

/// <https://w3c.github.io/ServiceWorker/#dom-extendablemessageevent-source>
#[derive(Clone, JSTraceable, MallocSizeOf)]
pub(crate) enum MessageSource {
    Client(DomRoot<Client>),
    ServiceWorker(DomRoot<ServiceWorker>),
    MessagePort(DomRoot<MessagePort>),
}

impl From<ClientOrServiceWorkerOrMessagePort> for MessageSource {
    fn from(value: ClientOrServiceWorkerOrMessagePort) -> Self {
        match value {
            ClientOrServiceWorkerOrMessagePort::Client(client) => MessageSource::Client(client),
            ClientOrServiceWorkerOrMessagePort::ServiceWorker(sw) => {
                MessageSource::ServiceWorker(sw)
            },
            ClientOrServiceWorkerOrMessagePort::MessagePort(port) => {
                MessageSource::MessagePort(port)
            },
        }
    }
}

impl From<&ClientOrServiceWorkerOrMessagePort> for MessageSource {
    fn from(value: &ClientOrServiceWorkerOrMessagePort) -> Self {
        match value {
            ClientOrServiceWorkerOrMessagePort::Client(client) => {
                MessageSource::Client(DomRoot::from_ref(client))
            },