Fix all tests
This commit is contained in:
parent
7381ea019c
commit
606848a61e
@ -3,8 +3,8 @@
|
||||
#![allow(unused_imports)]
|
||||
#![feature(rustc_private)]
|
||||
|
||||
extern crate rustc_serialize;
|
||||
use rustc_serialize::json::Object;
|
||||
extern crate libc;
|
||||
use libc::c_void;
|
||||
|
||||
pub fn main() {
|
||||
println!("Hello world!");
|
||||
|
@ -3,42 +3,80 @@
|
||||
#![allow(unused_must_use)]
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused_imports)]
|
||||
#![feature(rustc_private)]
|
||||
|
||||
extern crate rustc_macros;
|
||||
extern crate rustc_serialize;
|
||||
|
||||
use std::fmt;
|
||||
use std::io::prelude::*;
|
||||
use std::io::Cursor;
|
||||
use std::slice;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
trait Encoder {
|
||||
type Error;
|
||||
}
|
||||
|
||||
trait Encodable<S: Encoder> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error>;
|
||||
}
|
||||
|
||||
struct JsonEncoder<'a>(PhantomData<&'a mut ()>);
|
||||
|
||||
impl Encoder for JsonEncoder<'_> {
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
struct AsJson<'a, T> {
|
||||
inner: &'a T,
|
||||
}
|
||||
|
||||
impl<'a, T: for<'r> Encodable<JsonEncoder<'r>>> fmt::Display for AsJson<'a, T> {
|
||||
/// Encodes a json value into a string
|
||||
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn as_json<T>(t: &T) -> AsJson<'_, T> {
|
||||
AsJson { inner: t }
|
||||
}
|
||||
|
||||
struct OpaqueEncoder(Vec<u8>);
|
||||
|
||||
impl Encoder for OpaqueEncoder {
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
use rustc_macros::Encodable;
|
||||
use rustc_serialize::json;
|
||||
use rustc_serialize::opaque;
|
||||
use rustc_serialize::{Encodable, Encoder};
|
||||
|
||||
#[derive(Encodable)]
|
||||
struct Foo {
|
||||
baz: bool,
|
||||
}
|
||||
|
||||
#[derive(Encodable)]
|
||||
impl<S: Encoder> Encodable<S> for Foo {
|
||||
fn encode(&self, _s: &mut S) -> Result<(), S::Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct Bar {
|
||||
froboz: usize,
|
||||
}
|
||||
|
||||
impl<S: Encoder> Encodable<S> for Bar {
|
||||
fn encode(&self, _s: &mut S) -> Result<(), S::Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
enum WireProtocol {
|
||||
JSON,
|
||||
Opaque,
|
||||
// ...
|
||||
}
|
||||
|
||||
fn encode_json<T: for<'a> Encodable<json::Encoder<'a>>>(val: &T, wr: &mut Cursor<Vec<u8>>) {
|
||||
write!(wr, "{}", json::as_json(val));
|
||||
fn encode_json<T: for<'a> Encodable<JsonEncoder<'a>>>(val: &T, wr: &mut Cursor<Vec<u8>>) {
|
||||
write!(wr, "{}", as_json(val));
|
||||
}
|
||||
fn encode_opaque<T: Encodable<opaque::Encoder>>(val: &T, wr: Vec<u8>) {
|
||||
let mut encoder = opaque::Encoder::new(wr);
|
||||
fn encode_opaque<T: Encodable<OpaqueEncoder>>(val: &T, wr: Vec<u8>) {
|
||||
let mut encoder = OpaqueEncoder(wr);
|
||||
val.encode(&mut encoder);
|
||||
}
|
||||
|
||||
|
@ -3,21 +3,48 @@
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_must_use)]
|
||||
// pretty-expanded FIXME #23616
|
||||
#![feature(rustc_private)]
|
||||
|
||||
extern crate rustc_serialize;
|
||||
|
||||
use rustc_serialize::json;
|
||||
use rustc_serialize::{Encodable, Encoder};
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
struct Foo<T: for<'a> Encodable<json::Encoder<'a>>> {
|
||||
trait Encoder {
|
||||
type Error;
|
||||
}
|
||||
|
||||
trait Encodable<S: Encoder> {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error>;
|
||||
}
|
||||
|
||||
impl<S: Encoder> Encodable<S> for i32 {
|
||||
fn encode(&self, _s: &mut S) -> Result<(), S::Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct JsonEncoder<'a>(PhantomData<&'a mut ()>);
|
||||
|
||||
impl Encoder for JsonEncoder<'_> {
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
fn encode_json<T: for<'r> Encodable<JsonEncoder<'r>>>(
|
||||
object: &T,
|
||||
) -> Result<String, ()> {
|
||||
let s = String::new();
|
||||
{
|
||||
let mut encoder = JsonEncoder(PhantomData);
|
||||
object.encode(&mut encoder)?;
|
||||
}
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
struct Foo<T: for<'a> Encodable<JsonEncoder<'a>>> {
|
||||
v: T,
|
||||
}
|
||||
|
||||
impl<T: for<'a> Encodable<json::Encoder<'a>>> Drop for Foo<T> {
|
||||
impl<T: for<'a> Encodable<JsonEncoder<'a>>> Drop for Foo<T> {
|
||||
fn drop(&mut self) {
|
||||
json::encode(&self.v);
|
||||
encode_json(&self.v);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,27 +2,38 @@
|
||||
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(dead_code)]
|
||||
#![feature(rustc_private)]
|
||||
|
||||
extern crate rustc_serialize;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use rustc_serialize::json::{self, Json};
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::option;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum Json {
|
||||
I64(i64),
|
||||
U64(u64),
|
||||
F64(f64),
|
||||
String(String),
|
||||
Boolean(bool),
|
||||
Array(Array),
|
||||
Object(Object),
|
||||
Null,
|
||||
}
|
||||
|
||||
type Array = Vec<Json>;
|
||||
type Object = BTreeMap<String, Json>;
|
||||
|
||||
enum object {
|
||||
bool_value(bool),
|
||||
int_value(i64),
|
||||
}
|
||||
|
||||
fn lookup(table: json::Object, key: String, default: String) -> String
|
||||
fn lookup(table: Object, key: String, default: String) -> String
|
||||
{
|
||||
match table.get(&key) {
|
||||
option::Option::Some(&Json::String(ref s)) => {
|
||||
s.to_string()
|
||||
}
|
||||
option::Option::Some(value) => {
|
||||
println!("{} was expected to be a string but is a {}", key, value);
|
||||
println!("{} was expected to be a string but is a {:?}", key, value);
|
||||
default
|
||||
}
|
||||
option::Option::None => {
|
||||
@ -31,7 +42,7 @@ fn lookup(table: json::Object, key: String, default: String) -> String
|
||||
}
|
||||
}
|
||||
|
||||
fn add_interface(_store: isize, managed_ip: String, data: json::Json) -> (String, object)
|
||||
fn add_interface(_store: isize, managed_ip: String, data: Json) -> (String, object)
|
||||
{
|
||||
match &data {
|
||||
&Json::Object(ref interface) => {
|
||||
@ -43,13 +54,13 @@ fn add_interface(_store: isize, managed_ip: String, data: json::Json) -> (String
|
||||
(label, object::bool_value(false))
|
||||
}
|
||||
_ => {
|
||||
println!("Expected dict for {} interfaces, found {}", managed_ip, data);
|
||||
println!("Expected dict for {} interfaces, found {:?}", managed_ip, data);
|
||||
("gnos:missing-interface".to_string(), object::bool_value(true))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_interfaces(store: isize, managed_ip: String, device: HashMap<String, json::Json>)
|
||||
fn add_interfaces(store: isize, managed_ip: String, device: HashMap<String, Json>)
|
||||
-> Vec<(String, object)> {
|
||||
match device["interfaces"] {
|
||||
Json::Array(ref interfaces) =>
|
||||
@ -60,7 +71,7 @@ fn add_interfaces(store: isize, managed_ip: String, device: HashMap<String, json
|
||||
}
|
||||
_ =>
|
||||
{
|
||||
println!("Expected list for {} interfaces, found {}", managed_ip,
|
||||
println!("Expected list for {} interfaces, found {:?}", managed_ip,
|
||||
device["interfaces"]);
|
||||
Vec::new()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user