Skip to content

Instantly share code, notes, and snippets.

@highlyunavailable
Last active April 24, 2016 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save highlyunavailable/f8424d2881e2d7b2d510d114a57ed9c3 to your computer and use it in GitHub Desktop.
Save highlyunavailable/f8424d2881e2d7b2d510d114a57ed9c3 to your computer and use it in GitHub Desktop.
extern crate toml;
use decode::Decoder;
use message::Message;
use std::error::Error;
pub struct JsonDecoder {
_dummy: i32,
}
impl Decoder for JsonDecoder {
fn decode(&self, data: Vec<u8>) -> Result<Message, Box<Error>> {
println!{"decoder"}
Ok(Message::new())
}
}
impl JsonDecoder {
pub fn new(c: toml::Value) -> Result<Box<Decoder>, Box<Error>> {
Ok(Box::new(JsonDecoder { _dummy: 0i32 }))
}
}
extern crate toml;
use message::Message;
use std::error::Error;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
pub mod json;
pub trait Decoder: Send + Sync {
fn decode(&self, data: Vec<u8>) -> Result<Message, Box<Error>>;
}
pub type BindFunc = fn(toml::Value) -> Result<Box<Decoder>, Box<Error>>;
struct Registry {
decoders: RwLock<HashMap<String, Arc<Box<Decoder>>>>,
defaults: RwLock<HashMap<String, Arc<Box<Decoder>>>>,
binders: RwLock<HashMap<String, BindFunc>>,
}
impl Registry {
fn new() -> Registry {
Registry {
binders: RwLock::new(HashMap::new()),
decoders: RwLock::new(HashMap::new()),
defaults: RwLock::new(HashMap::new()),
}
}
}
lazy_static!{ static ref r: Registry = Registry::new(); }
pub fn register(name: String, b: BindFunc) {
let mut map = r.binders.write().unwrap();
let _ = map.insert(name, b);
}
pub fn register_default(name: String, d: Box<Decoder>) {
let mut map = r.defaults.write().unwrap();
let _ = map.insert(name, Arc::new(d));
}
pub fn get_binder(id: String) -> Option<BindFunc> {
let map = r.binders.read().unwrap();
match map.get(&id) {
Some(&b) => Some(b),
None => None,
}
}
pub fn default(id: String) -> Option<Arc<Box<Decoder>>> {
let map = r.defaults.read().unwrap();
match map.get(&id) {
Some(b) => Some(b.clone()),
None => None,
}
}
#[macro_use]
extern crate lazy_static;
extern crate toml;
mod message;
mod decode;
fn main() {
println!("Hello, world!");
decode::register("json".to_string(), decode::json::JsonDecoder::new);
let d = decode::get_binder("json".to_string()).unwrap();
let decoder = d(toml::Value::String("whee".to_string())).unwrap();
decode::register_default("json".to_string(), decoder);
let dd = decode::default("json".to_string()).unwrap();
&dd.decode(vec![]);
}
extern crate time;
use std::collections::HashMap;
// Mostly taken whole-cloth from http://amznlabs.github.io/ion-docs/spec.html
// clean up portions later.
pub enum MessageValue {
Null,
Bool(bool),
Int(i64),
Float(f64),
Decimal(f64), /* http://speleotrove.com/decimal/decarith.html - shouldn't really be a float, should be a base 10 float. */
Timestamp(time::Tm),
String(String),
Symbol(String),
Blob(Vec<u8>),
Clob(String),
Struct(HashMap<String, MessageValue>),
List(Vec<MessageValue>),
SExp(Vec<MessageValue>),
}
pub struct Message(HashMap<String, MessageValue>);
impl Message {
pub fn new() -> Message {
return Message(HashMap::new());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment