使用旧的Rust 2015

0.0.1 2015年1月1日

#101#currency

23KB
507

hal-rs

一个用于生成Hal响应的纯Rust库。

Build Status

构建

运行测试

$ cargo test

示例

请参阅hal-rs-demo以查看实际的工作示例。

一个示例程序,展示如何手动创建Hal响应或通过在您的结构体上实现ToHal

extern crate hal;
extern crate serialize;

use hal::{Link, Resource, ToHal, ToHalData};
use serialize::json::ToJson;

struct Order {
    total: f64,
    currency: String,
    status: String
}

impl ToHal for Order {
    fn to_hal(self) -> Resource {
        Resource::with_self("https://www.example.com/orders/1")
            .add_state("total", self.total)
            .add_state("currency", self.currency)
            .add_state("status", self.status)
    }
}

fn main() {
    let hal = Resource::with_self("/orders")
        .add_curie("ea", "http://example.com/docs/rels/{rel}")
        .add_link("next", Link::new("/orders?page=2"))
        .add_link("ea:find", Link::new("/orders{?id}").templated(true))
        .add_link("ea:admin", Link::new("/admins/2").title("Fred"))
        .add_link("ea:admin", Link::new("/admins/5").title("Kate"))
        .add_state("currentlyProcessing", 14)
        .add_state("shippedToday", 14)
        .add_resource("ea:order",
            Resource::with_self("/orders/123")
                .add_link("ea:basket", Link::new("/baskets/98712"))
                .add_link("ea:customer", Link::new("/customers/7809"))
                .add_state("total", (30.00 as f64))
                .add_state("currency", "USD")
                .add_state("status", "shipped")
        )
        .add_resource("ea:order",
            Resource::with_self("/orders/124")
                .add_link("ea:basket", Link::new("/baskets/97213"))
                .add_link("ea:customer", Link::new("/customers/12369"))
                .add_state("total", (20.00 as f64))
                .add_state("currency", "USD")
                .add_state("status", "processing")
        );

    println!("Creating Hal using a DSL: {}", hal.to_json().to_pretty_str());

    let order = Order { total: 20.00 as f64, 
                        currency: String::from_str("USD"), 
                        status: String::from_str("processing") };

    println!("Creating Hal using to_hal(): {}", order.to_hal().to_json().to_pretty_str());
}

无运行时依赖