why does proc macro not work

This commit is contained in:
2021-08-29 14:59:11 -04:00
parent 9d6901c499
commit 5318c59880
23 changed files with 2288 additions and 582 deletions
-20
View File
@@ -1,20 +0,0 @@
use diesel::prelude::*;
use super::models::{Event, NewEvent};
use super::super::utils::{exit_with_error};
pub fn create_event(conn: &PgConnection, event: NewEvent) -> Event {
use super::schema::events;
diesel::insert_into(events::table)
.values(&event)
.get_result(conn)
.unwrap_or_else(|_| exit_with_error("Error saving new post"))
}
pub fn get_all(conn: &PgConnection) -> Result<Vec<Event>, diesel::result::Error> {
use super::schema::events::dsl::*;
events.load::<Event>(conn)
}
+238 -3
View File
@@ -1,3 +1,238 @@
pub mod events;
pub mod schema;
pub mod models;
#[macro_use]
use cms_macro::api_route;
use rocket::{
http::{RawStr, Status},
request::FromParam,
};
use std::borrow::Cow;
pub struct Lang<'a>(Cow<'a, str>);
fn valid_lang(lang: &str) -> bool {
lang.chars().all(|c| (c >= 'a' && c <= 'z')) && lang.chars().count() == 2
}
impl<'a> FromParam<'a> for Lang<'a> {
type Error = Status;
fn from_param(param: &'a RawStr) -> Result<Lang<'a>, Status> {
match valid_lang(param) {
true => Ok(Lang(Cow::Borrowed(param))),
false => Err(Status::InternalServerError),
}
}
}
pub mod defs {
use chrono::naive::NaiveDate;
use rocket::{http::RawStr, request::FromFormValue};
use std::ops::Deref;
#[derive(Debug)]
pub struct DateForm(NaiveDate);
impl Deref for DateForm {
type Target = NaiveDate;
fn deref(&self) -> &NaiveDate {
&self.0
}
}
impl<'v> FromFormValue<'v> for DateForm {
type Error = ();
fn from_form_value(value: &'v RawStr) -> Result<DateForm, ()> {
let value_uri = match value.url_decode() {
Ok(n) => n,
Err(_) => return Err(()),
};
let naivedate = NaiveDate::parse_from_str(&value_uri[..], "%m/%d/%Y");
match naivedate {
Ok(n) => Ok(DateForm(n)),
Err(_) => Err(()),
}
}
}
}
api_route! {
events {
title: (Text, String, String),
location: (Text, String, String),
text: (Text, String, String),
event_date: (Text, NaiveDate, DateForm),
}
}
/*
pub mod events {
use crate::data::{defs::*, Lang};
use crate::auth::Token;
use ::chrono::naive::*;
use ::diesel::{prelude::*, Insertable, Queryable};
use ::rocket::{http::Status, request::Form, response::Redirect, State};
use ::rocket_contrib::{json::Json, templates::Template};
use ::serde::Serialize;
use ::std::{collections::*, sync::Mutex};
pub mod schema {
table! {
use diesel::sql_types::*;
events (id) {
id -> Integer,
lang -> Text,
title -> Text,
location -> Text,
text -> Text,
event_date -> Date,
}
}
}
use schema::events;
#[derive(Debug, Clone, Queryable, Serialize)]
pub struct Get {
pub id: i32,
pub lang: String,
pub title: String,
pub location: String,
pub text: String,
pub event_date: NaiveDate,
}
#[derive(Debug, AsChangeset, Insertable)]
#[table_name = "events"]
pub struct Create {
pub lang: String,
pub title: String,
pub location: String,
pub text: String,
pub event_date: NaiveDate,
}
#[derive(Debug, FromForm)]
pub struct Post {
pub lang: String,
pub title: String,
pub location: String,
pub text: String,
pub event_date: DateForm,
}
#[derive(Debug, FromForm)]
pub struct Update {
pub id: i32,
pub lang: String,
pub title: String,
pub location: String,
pub text: String,
pub event_date: DateForm,
}
#[derive(Debug, FromForm)]
pub struct Delete {
pub id: i32,
}
impl Post {
fn convert(self) -> Create {
Create {
lang: self.lang,
title: self.title,
location: self.location,
text: self.text,
event_date: *self.event_date,
}
}
}
impl Update {
fn convert(self) -> Create {
Create {
lang: self.lang,
title: self.title,
location: self.location,
text: self.text,
event_date: *self.event_date,
}
}
}
pub fn create(conn: &PgConnection, event: Create) -> Result<Get, diesel::result::Error> {
diesel::insert_into(events::table)
.values(&event)
.get_result(conn)
}
pub fn get(conn: &PgConnection, lg: Lang) -> Result<Vec<Get>, diesel::result::Error> {
use schema::events::dsl::*;
events.filter(lang.eq(lg.0)).load::<Get>(conn)
}
pub fn get_all(conn: &PgConnection) -> Result<Vec<Get>, diesel::result::Error> {
use schema::events::dsl::*;
events.load::<Get>(conn)
}
pub fn update(
conn: &PgConnection,
idn: i32,
event: Create,
) -> Result<Get, diesel::result::Error> {
use schema::events::dsl::*;
diesel::update(events.find(idn))
.set(&event)
.get_result::<Get>(conn)
}
pub fn delete(conn: &PgConnection, idn: i32) -> Result<usize, diesel::result::Error> {
use schema::events::dsl::*;
diesel::delete(events.find(idn)).execute(conn)
}
#[get("/<lang>/events")]
pub fn api(pg: State<Mutex<PgConnection>>, lang: Lang) -> Result<Json<Vec<Get>>, Status> {
Ok(Json(
get(&*(pg.lock().unwrap()), lang).map_err(|_| Status::InternalServerError)?,
))
}
#[post("/events/add", data = "<form>")]
pub fn add(pg: State<Mutex<PgConnection>>, form: Form<Post>) -> Result<Redirect, Status> {
match create(&*(pg.lock().unwrap()), form.into_inner().convert()) {
Ok(_) => Ok(Redirect::to("/ui/events")),
Err(_) => Err(Status::InternalServerError),
}
}
#[post("/events/del", data = "<form>")]
pub fn del(pg: State<Mutex<PgConnection>>, form: Form<Delete>) -> Result<Redirect, Status> {
match delete(&*(pg.lock().unwrap()), form.id) {
Ok(_) => Ok(Redirect::to("/ui/events")),
Err(_) => Err(Status::InternalServerError),
}
}
#[post("/events/upd", data = "<form>")]
pub fn upd(pg: State<Mutex<PgConnection>>, form: Form<Update>) -> Result<Redirect, Status> {
match update(&*(pg.lock().unwrap()), form.id, form.into_inner().convert()) {
Ok(_) => Ok(Redirect::to("/ui/events")),
Err(_) => Err(Status::InternalServerError),
}
}
#[get("/events")]
pub fn ui(_token: Token, pg: State<Mutex<PgConnection>>) -> Result<Template, Status> {
let ctx = get_all(&*(pg.lock().unwrap()))
.map_err(|_| Status::InternalServerError)?
.iter()
.map(|x| (x.id, x.clone()))
.collect::<HashMap<i32, Get>>();
Ok(Template::render("events", &ctx))
}
}
*/
-22
View File
@@ -1,22 +0,0 @@
use super::schema::events;
use diesel::Insertable;
use diesel::Queryable;
use chrono::naive::NaiveDate;
#[derive(Queryable)]
pub struct Event {
pub id: i32,
pub title: String,
pub location: String,
pub text: String,
pub event_date: NaiveDate,
}
#[derive(Insertable)]
#[table_name="events"]
pub struct NewEvent<'a> {
pub title: &'a str,
pub location: &'a str,
pub text: &'a str,
pub event_date: &'a NaiveDate
}
-12
View File
@@ -1,12 +0,0 @@
table! {
use diesel::sql_types::*;
events (id) {
id -> Integer,
title -> Text,
text -> Text,
location -> Text,
event_date -> Date,
}
}