// Generates the proxy object used to deal with client-side objects
Generates the proxy object used to deal with client-side objects
Generates the proxy object used to deal with client - side objects
fn generate_proxy<'a, W: Write>(
        &self,
        service_name: &str,
        callback: &Callback,
        sink: &'a mut W,
    ) -> Result<()> {
        writeln!(sink, "#[derive(Clone)]")?;
        writeln!(sink, "pub struct {}Proxy {{", callback.name)?;
        writeln!(
            sink,
            "helper: SessionSupport, object_id: ObjectRef, service_id: TrackerId,"
        )?;
        // For each method, keep track of the return value receiver in a map request_id -> receiver.
        for method in callback.methods.values() {
            let success = rust_type_for_param(&method.returns.success);
            let error = rust_type_for_param(&method.returns.error);
            writeln!(
                sink,
                "pub(crate) {}_requests: Shared<HashMap<u64, Sender<Result<{}, {}>>>>,",
                method.name, success, error
            )?;
        }
        writeln!(sink, "}}\n")?;
        writeln!(sink, "impl {}Proxy {{", callback.name)?;
        writeln!(sink, "pub fn new(object_id: ObjectRef, service_id: TrackerId, helper: &SessionSupport) -> Self {{")?;
        writeln!(
            sink,
            "Self {{ object_id, service_id, helper: helper.clone(),"
        )?;
        // For each method, keep track of the return value receiver in a map request_id -> receiver.
        for method in callback.methods.values() {
            writeln!(
                sink,
                "{}_requests: Shared::adopt(HashMap::new()),",
                method.name
            )?;
        }
        writeln!(sink, "}}\n")?;
        writeln!(sink, "}}")?;
        // Generate methods.
        for method in callback.methods.values() {
            let camel_name = method.name.to_camel_case();
            write!(sink, "pub fn {}(&mut self, ", method.name)?;
            for param in &method.params {
                let stype = rust_type_for_param(&param.typ);
                write!(sink, "{}: {},", param.name, stype)?;
            }
            // Get the return type to build the Sender/Receiver type.
            let success = rust_type_for_param(&method.returns.success);
            let error = rust_type_for_param(&method.returns.error);
            writeln!(sink, ") -> Receiver<Result<{},{}>> {{", success, error)?;
            sink.write_all(
                b"
            let (sender, receiver) = channel();
            let request = self.helper.id_factory().lock().next_id();
            let message = BaseMessage {
                service: self.service_id,
                object: self.object_id.into(),
                kind: BaseMessageKind::Request(request),
                content: vec![],
            };\n",
            )?;
            // Keep track of the sender for this request's response.
            writeln!(
                sink,
                "{{ let mut lock = self.{}_requests.lock(); lock.insert(request, sender); }}",
                method.name
            )?;
            // Create the payload.
            writeln!(
                sink,
                "let value = {}ToClient::{}{}",
                service_name, callback.name, camel_name
            )?;
            if !method.params.is_empty() {
                writeln!(sink, "(")?;
                for param in &method.params {
                    writeln!(sink, "{},", param.name)?;
                }
                writeln!(sink, ")")?;
            }
            writeln!(sink, ";")?;
            writeln!(sink, "self.helper.serialize_message(&message, &value);")?;
            writeln!(sink, "receiver")?;
            writeln!(sink, "}}\n")?;
        }
        writeln!(sink, "}}\n")?;
        Ok(())
    }
fn generate_proxy < ' a , W : Write > ( & self , service_name : & str , callback : & Callback , sink : & ' a mut W , ) -> Result < ( ) > { writeln ! ( sink " " ) ? ; writeln ! ( sink " " callback name ) ? ; writeln ! ( sink " " ) ? ; // For each method, keep track of the return value receiver in a map request_id -> receiver. for method in callback . methods . values ( ) { let success = rust_type_for_param ( & method . returns . success ) ; let error = rust_type_for_param ( & method . returns . error ) ; writeln ! ( sink " " method name success error ) ? ; } writeln ! ( sink " \n " ) ? ; writeln ! ( sink " " callback name ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; // For each method, keep track of the return value receiver in a map request_id -> receiver. for method in callback . methods . values ( ) { writeln ! ( sink " " method name ) ? ; } writeln ! ( sink " \n " ) ? ; writeln ! ( sink " " ) ? ; // Generate methods. for method in callback . methods . values ( ) { let camel_name = method . name . to_camel_case ( ) ; write ! ( sink " " method name ) ? ; for param in & method . params { let stype = rust_type_for_param ( & param . typ ) ; write ! ( sink " " param name stype ) ? ; } // Get the return type to build the Sender/Receiver type. let success = rust_type_for_param ( & method . returns . success ) ; let error = rust_type_for_param ( & method . returns . error ) ; writeln ! ( sink " " success error ) ? ; sink . write_all ( b" \n " , ) ? ; // Keep track of the sender for this request's response. writeln ! ( sink " " method name ) ? ; // Create the payload. writeln ! ( sink " " service_name callback name camel_name ) ? ; if ! method . params . is_empty ( ) { writeln ! ( sink " " ) ? ; for param in & method . params { writeln ! ( sink " " param name ) ? ; } writeln ! ( sink " " ) ? ; } writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " " ) ? ; writeln ! ( sink " \n " ) ? ; } writeln ! ( sink " \n " ) ? ; Ok ( ( ) ) }
Generates the proxy object used to deal with client-side objects
Generates the proxy object used to deal with client - side objects
&self
service_name
&str
callback
&Callback
sink
&'a mut W