title: Calling Rust from NodeJS
body: <p>I was trying to build a simple rust rss 'harvester' for my soup.io blog and then post those entries to diaspora with node.js (since there is an npm package for that) </p>
  
  <p>I want to learn how to use rust from node so this is why I'm building this project.</p>
  
  <p>My problem is that I don't know how to call the <code>ffi</code> function with the right types.</p>
  
  <pre><code>var lib = ffi.Library('target/debug/libmain', {
      'get_soup': ['Vec&lt;Post&gt;', ['String']]
  });
  </code></pre>
  
  <p>The <code>'Vec&lt;Post&gt;'</code> doesn't work.
  I get that I have to use <code>ref</code> for that.
  But I don't really know how and what that actually does. </p>
  
  <p>I understand that I have to translate the rust types to javascript?</p>
  
  <p><strong>How can I use <code>Vec&lt;Post&gt;</code> in my ffi function?</strong></p>
  
  <p>my github project for that: <a href="https://github.com/Realtin/suppe" rel="noreferrer">Realtin/suppe</a></p>
  
  <p>and here the relevant code:</p>
  
  <h1>Rust Code:</h1>
  
  <pre><code>extern crate rss;
  extern crate hyper;
  
  use rss::Rss;
  use std::io::prelude::*;
  
  #[derive(Debug)]
  pub struct Post  {
      title: String,
      link: String,
      description: String,
  }
  
  fn main() {
      let user = "realtin".to_string();
      let vec = get_soup(&amp;user);
      println!("{:?}", vec[vec.len()-1]);
  }
  
  #[no_mangle]
  pub extern fn get_soup(user: &amp;str) -&gt;Vec&lt;Post&gt;{
      let url = format!("http://{}.soup.io/rss", user);
      let mut vec = Vec::new();
  
      let client = hyper::Client::new();
      let mut response = client.get(&amp;url).send().unwrap();
  
      let mut suppe = String::new();
      let _= response.read_to_string(&amp;mut suppe);
  
      let rss::Rss(channel) = suppe.parse::&lt;rss::Rss&gt;().unwrap();
  
      for item in channel.items.into_iter().rev() {
        let item_object = Post {
             title: item.title.unwrap(),
             link: item.link.unwrap(),
             description: item.description.unwrap(),
          };
          vec.push(item_object);
      }
      return vec;
  }
  </code></pre>
  
  <h1>NodeJS code:</h1>
  
  <pre><code>var ref = require('ref');
  var StructType = require("ref-struct");
  var ffi = require('ffi');
  
  var Post = StructType({
    title: String,
    link: String,
    description: String,
  });
  // var vecPost = ref.refType(ref.types.Object);
  
  var lib = ffi.Library('target/debug/libmain', {
      'get_soup': ['Vec&lt;Post&gt;', ['String']]
  });
  
  var posts = lib.get_soup("realtin");
  </code></pre>