~lthms/typed-urls

ae8806086509ac3454f3f14b1245b78b89e1014e — Thomas Letan 11 months ago 240343b
Implement Display
3 files changed, 16 insertions(+), 9 deletions(-)

M Cargo.toml
M src/lib.rs
M typed-urls-derive/src/lib.rs
M Cargo.toml => Cargo.toml +1 -1
@@ 1,5 1,5 @@
[workspace]
members = ['typed-urls-derive']
members = ['typed-urls-derive', 'typed-urls-tera']

[package]
name = "typed-urls"

M src/lib.rs => src/lib.rs +4 -1
@@ 1,16 1,19 @@
use std::collections::HashMap;
use std::fmt::Display;
use std::slice::Iter;

use serde_json::Value;
pub use typed_urls_derive::TypedUrl;

pub trait Routing {
    type Url;
    type Url: Display;

    fn enumerate() -> Iter<'static, Self>
    where
        Self : std::marker::Sized;

    fn to_string(&self) -> String;

    fn to_url(
        &self,
        args : &HashMap<String, Value>,

M typed-urls-derive/src/lib.rs => typed-urls-derive/src/lib.rs +11 -7
@@ 115,17 115,22 @@ fn derive_ast_to_string(
        let flds = fields.get(&ident).unwrap();

        match_bodies.push(quote! {
            #name::#ident { #(#flds),* } => format!(#fmt, #(#flds),*)
            #name::#ident { #(#flds),* } => write!(f, #fmt, #(#flds),*)
        });
    }

    quote! { impl #name {
        pub fn to_string(&self) -> String {
            match self {
                #(#match_bodies),*
    quote! {
        use std::fmt;
        use std::fmt::Display;

        impl Display for #name {
            fn fmt(&self, f : &mut fmt::Formatter<'_>) -> fmt::Result {
                match self {
                    #(#match_bodies),*
                }
            }
        }
    }}
    }
}

fn ident_to_litstr(


@@ 191,7 196,6 @@ fn derive_typed_url_boilerplate(ast : &DeriveInput) -> Quote {
        derive_ast_to_string(enum_name, &enum_ast, &fmts, &fields);

    quote! {
        use std::iter::Iterator;
        use typed_urls::Routing;

        #enum_to_string