1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
extern crate rustc_serialize; use rustc_serialize::json; use rustc_serialize::Encodable; use rustc_serialize::Encoder; #[derive(RustcEncodable)] struct BuildDetailAttributes { name: &'static str, version: &'static str, homepage: &'static str, support: &'static str, description: &'static str } struct BuildBody { _type: String, id: i16, attributes: BuildDetailAttributes } impl Encodable for BuildBody { fn encode<S: Encoder>(&self, encoder: &mut S) -> Result<(), S::Error> { match * self { BuildBody { _type: ref p_type, id: ref p_id, attributes: ref p_attributes } => encoder.emit_struct("BuildDetail", 3usize, |enc| -> _ { try!(enc.emit_struct_field( "type", 0usize, |enc| p_type.encode(enc))); try!(enc.emit_struct_field( "id", 1usize, |enc| p_id.encode(enc))); return enc.emit_struct_field("attributes", 2usize, |enc| -> _ { (* p_attributes).encode(enc) }); }), } } } #[derive(RustcEncodable)] struct Build { data: Vec<BuildBody> } fn get_build_attributes() -> BuildDetailAttributes { return BuildDetailAttributes { name: env!("CARGO_PKG_NAME"), version: env!("CARGO_PKG_VERSION"), homepage: env!("CARGO_PKG_HOMEPAGE"), support: env!("CARGO_PKG_AUTHORS"), description: env!("CARGO_PKG_DESCRIPTION") }; } fn get_build_body(attributes : BuildDetailAttributes) -> BuildBody { return BuildBody { _type: "build".to_string(), id: 1, attributes: attributes }; } fn get_build(body : BuildBody ) -> Build { return Build { data: vec![body] }; } pub fn build_as_json() -> String { let attributes = get_build_attributes(); let body = get_build_body(attributes); let json = get_build(body); let payload = json::encode(&json).unwrap(); return payload; }