aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml3
-rw-r--r--README.md2
-rw-r--r--src/actix.rs6
-rw-r--r--src/lib.rs6
-rw-r--r--src/tera.rs2
-rw-r--r--typed-urls-derive/Cargo.toml9
-rw-r--r--typed-urls-derive/tests/simple.rs (renamed from tests/simple.rs)25
7 files changed, 42 insertions, 11 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 1ea667a..95e10cb 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,13 +5,14 @@ members = ['typed-urls-derive']
name = "typed-urls"
version = "0.1.0"
authors = ["Thomas Letan <lthms@soap.coffee>"]
+description = "Type-safe URLs for Rust web applications"
+repository = "https://labs.soap.coffee/crates/typed-urls.git"
edition = "2018"
license = "MPL-2.0"
[dependencies]
serde = "1.0"
serde_json = "1.0"
-typed-urls-derive = { path = "typed-urls-derive/" }
tera = { version = "1.5", optional = true }
actix-web = { version = "3.0", optional = true }
diff --git a/README.md b/README.md
index e97414a..5099274 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
# `typed-urls`
This crate allows to define the various routes of your webapp using a
-simple Rust structure. See `tests/simple.rs` for an
+simple Rust structure. See `typed-urls-derive/tests/simple.rs` for an
example. Currently support `actix` and `tera`.
`typed-urls` is distributed under the terms of the MPL 2.0 license.
diff --git a/src/actix.rs b/src/actix.rs
index e7afb32..2b7f46a 100644
--- a/src/actix.rs
+++ b/src/actix.rs
@@ -4,18 +4,18 @@
use std::fmt::Display;
-use actix_web::Resource;
use actix_web::web::resource;
+use actix_web::Resource;
use crate::TypedUrl;
-pub trait Endpoint : TypedUrl {
+pub trait Endpoint: TypedUrl {
fn endpoint(route : Self::Route) -> Resource;
}
impl<U> Endpoint for U
where
- U : TypedUrl + Display
+ U : TypedUrl + Display,
{
fn endpoint(r : U::Route) -> Resource {
resource(r.to_string())
diff --git a/src/lib.rs b/src/lib.rs
index 06bf180..790462c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,17 +6,17 @@ use std::collections::HashMap;
use std::fmt::Display;
use serde_json::Value;
-pub use typed_urls_derive::TypedUrl;
-#[cfg(feature = "tera-templates")]
-pub mod tera;
#[cfg(feature = "actix-routes")]
pub mod actix;
+#[cfg(feature = "tera-templates")]
+pub mod tera;
pub trait TypedUrl: Sized {
type Route: Display;
fn routes() -> &'static [Self::Route];
+
fn from_route(
route : &Self::Route,
args : &HashMap<String, Value>,
diff --git a/src/tera.rs b/src/tera.rs
index 18d7b4c..51d7836 100644
--- a/src/tera.rs
+++ b/src/tera.rs
@@ -5,9 +5,9 @@
use std::collections::HashMap;
use std::fmt::{Debug, Display};
+use crate::TypedUrl;
use serde_json::Value;
use tera::{Error, Function, Tera};
-use crate::TypedUrl;
struct TeraFunction<U>(U::Route)
where
diff --git a/typed-urls-derive/Cargo.toml b/typed-urls-derive/Cargo.toml
index 9465c6a..ecfadd3 100644
--- a/typed-urls-derive/Cargo.toml
+++ b/typed-urls-derive/Cargo.toml
@@ -1,6 +1,8 @@
[package]
name = "typed-urls-derive"
version = "0.1.0"
+description = "Derive typed-urls boilerplate with a proc-macro"
+repository = "https://labs.soap.coffee/crates/typed-urls.git"
authors = ["Thomas Letan <lthms@soap.coffee>"]
edition = "2018"
license = "MPL-2.0"
@@ -9,6 +11,11 @@ license = "MPL-2.0"
proc-macro = true
[dependencies]
+typed-urls = { version = "0.1", path = "../" }
proc-macro2 = "1.0"
syn = "1.0"
-quote = "1.0" \ No newline at end of file
+quote = "1.0"
+
+[dev-dependencies]
+serde = "*"
+serde_json = "*" \ No newline at end of file
diff --git a/tests/simple.rs b/typed-urls-derive/tests/simple.rs
index 8892535..8204d2e 100644
--- a/tests/simple.rs
+++ b/typed-urls-derive/tests/simple.rs
@@ -2,9 +2,15 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+#[macro_use]
+extern crate typed_urls_derive;
+#[macro_use]
+extern crate serde_json;
+
use typed_urls::TypedUrl;
+use std::collections::HashMap;
-#[derive(TypedUrl)]
+#[derive(TypedUrl, Debug)]
pub enum Url {
#[routefmt = "/worlds"]
Worlds,
@@ -24,3 +30,20 @@ pub enum Url {
z : i32,
},
}
+
+#[test]
+fn to_string() {
+ assert_eq!(Url::Worlds.to_string(), "/worlds");
+ assert_eq!(Url::World { world_key : "olympe".to_owned() }.to_string(), "/worlds/olympe");
+}
+
+
+#[test]
+fn from_route() {
+ let arg = HashMap::new();
+ assert_eq!(Url::from_route(&Route::Worlds, &arg).unwrap().to_string(), "/worlds");
+
+ let mut arg = HashMap::new();
+ arg.insert("world_key".to_owned(), json!("olympe"));
+ assert_eq!(Url::from_route(&Route::World, &arg).unwrap().to_string(), "/worlds/olympe");
+}