use std::future::Future;
use orbit_orm::{Database, Error};
use serde::Serialize;
use crate::field::{Column, Field};
use crate::form::{FieldErrors, FormInput};
use crate::policy::ActingUser;
use crate::query::{ListQuery, Page};
use crate::widget::{LatestRecords, StatWidget};
pub const fn table_name_is(table: &str, expected: &str) -> bool {
let (a, b) = (table.as_bytes(), expected.as_bytes());
if a.len() != b.len() {
return false;
}
let mut i = 0;
while i < a.len() {
if a[i] != b[i] {
return false;
}
i += 1;
}
true
}
pub trait IgniteResource: Send + Sync + 'static {
type Record: Serialize + Send;
type Input: Send;
const SLUG: &'static str;
const TITLE: &'static str;
const SINGULAR: &'static str = "Record";
const PER_PAGE: i64 = 20;
const NAVIGATION_LABEL: Option<&'static str> = None;
const NAVIGATION_GROUP: Option<&'static str> = None;
const NAVIGATION_SORT: Option<i32> = None;
const NAVIGATION_ICON: Option<&'static str> = None;
fn widgets(db: &Database) -> impl Future<Output = Result<Vec<StatWidget>, Error>> + Send {
let _ = db;
async { Ok(Vec::new()) }
}
fn latest_records() -> Option<LatestRecords> {
None
}
fn latest(
db: &Database,
limit: i64,
) -> impl Future<Output = Result<Vec<Self::Record>, Error>> + Send {
let _ = (db, limit);
async { Ok(Vec::new()) }
}
fn fields() -> &'static [Field];
fn columns() -> &'static [Column];
fn options(
db: &Database,
field: &str,
) -> impl Future<Output = Result<Vec<(String, String)>, Error>> + Send {
let _ = (db, field);
async { Ok(Vec::new()) }
}
fn list(
db: &Database,
query: &ListQuery,
) -> impl Future<Output = Result<Page<Self::Record>, Error>> + Send;
fn find(
db: &Database,
id: i64,
) -> impl Future<Output = Result<Option<Self::Record>, Error>> + Send;
fn create(
db: &Database,
input: Self::Input,
) -> impl Future<Output = Result<Self::Record, Error>> + Send;
fn update(
db: &Database,
id: i64,
input: Self::Input,
) -> impl Future<Output = Result<Self::Record, Error>> + Send;
fn delete(db: &Database, id: i64) -> impl Future<Output = Result<u64, Error>> + Send;
fn validate(form: &FormInput) -> Result<Self::Input, FieldErrors>;
fn validate_update(form: &FormInput) -> Result<Self::Input, FieldErrors> {
Self::validate(form)
}
fn list_for(
db: &Database,
user: &ActingUser,
query: &ListQuery,
) -> impl Future<Output = Result<Page<Self::Record>, Error>> + Send {
let _ = user;
Self::list(db, query)
}
fn find_for(
db: &Database,
user: &ActingUser,
id: i64,
) -> impl Future<Output = Result<Option<Self::Record>, Error>> + Send {
let _ = user;
Self::find(db, id)
}
fn create_for(
db: &Database,
user: &ActingUser,
input: Self::Input,
) -> impl Future<Output = Result<Self::Record, Error>> + Send {
let _ = user;
Self::create(db, input)
}
fn update_for(
db: &Database,
user: &ActingUser,
id: i64,
input: Self::Input,
) -> impl Future<Output = Result<Self::Record, Error>> + Send {
let _ = user;
Self::update(db, id, input)
}
fn delete_for(
db: &Database,
user: &ActingUser,
id: i64,
) -> impl Future<Output = Result<u64, Error>> + Send {
let _ = user;
Self::delete(db, id)
}
fn options_for(
db: &Database,
user: &ActingUser,
field: &str,
) -> impl Future<Output = Result<Vec<(String, String)>, Error>> + Send {
let _ = user;
Self::options(db, field)
}
}