Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Aliases for Complex Result Types in examples #4347

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions examples/postgres/relations/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub mod schema;
use crate::model::*;
use crate::schema::*;

type DbResult<T> = Result<T, Box<dyn Error + Send + Sync>>;

fn establish_connection() -> PgConnection {
dotenv().ok();

Expand All @@ -18,27 +20,23 @@ fn establish_connection() -> PgConnection {
.unwrap_or_else(|_| panic!("Error connecting to {database_url}"))
}

fn new_author(conn: &mut PgConnection, name: &str) -> Result<Author, Box<dyn Error + Send + Sync>> {
fn new_author(conn: &mut PgConnection, name: &str) -> DbResult<Author> {
let author = diesel::insert_into(authors::table)
.values(authors::name.eq(name))
.returning(Author::as_returning())
.get_result(conn)?;
Ok(author)
}

fn new_book(conn: &mut PgConnection, title: &str) -> Result<Book, Box<dyn Error + Send + Sync>> {
fn new_book(conn: &mut PgConnection, title: &str) -> DbResult<Book> {
let book = diesel::insert_into(books::table)
.values(books::title.eq(title))
.returning(Book::as_returning())
.get_result(conn)?;
Ok(book)
}

fn new_books_author(
conn: &mut PgConnection,
book_id: i32,
author_id: i32,
) -> Result<BookAuthor, Box<dyn Error + Send + Sync>> {
fn new_books_author(conn: &mut PgConnection, book_id: i32, author_id: i32) -> DbResult<BookAuthor> {
let book_author = diesel::insert_into(books_authors::table)
.values((
books_authors::book_id.eq(book_id),
Expand All @@ -54,7 +52,7 @@ fn new_page(
page_number: i32,
content: &str,
book_id: i32,
) -> Result<Page, Box<dyn Error + Send + Sync>> {
) -> DbResult<Page> {
let page = diesel::insert_into(pages::table)
.values((
pages::page_number.eq(page_number),
Expand All @@ -66,7 +64,7 @@ fn new_page(
Ok(page)
}

fn joins(conn: &mut PgConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn joins(conn: &mut PgConnection) -> DbResult<()> {
let page_with_book = pages::table
.inner_join(books::table)
.filter(books::title.eq("Momo"))
Expand All @@ -84,7 +82,7 @@ fn joins(conn: &mut PgConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}

fn one_to_n_relations(conn: &mut PgConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn one_to_n_relations(conn: &mut PgConnection) -> DbResult<()> {
let momo = books::table
.filter(books::title.eq("Momo"))
.select(Book::as_select())
Expand Down Expand Up @@ -117,7 +115,7 @@ fn one_to_n_relations(conn: &mut PgConnection) -> Result<(), Box<dyn Error + Sen
Ok(())
}

fn m_to_n_relations(conn: &mut PgConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn m_to_n_relations(conn: &mut PgConnection) -> DbResult<()> {
let astrid_lindgren = authors::table
.filter(authors::name.eq("Astrid Lindgren"))
.select(Author::as_select())
Expand Down Expand Up @@ -172,7 +170,7 @@ fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}

fn setup_data(conn: &mut PgConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn setup_data(conn: &mut PgConnection) -> DbResult<()> {
// create a book
let momo = new_book(conn, "Momo")?;

Expand Down
24 changes: 10 additions & 14 deletions examples/sqlite/relations/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub mod schema;
use crate::model::*;
use crate::schema::*;

type DbResult<T> = Result<T, Box<dyn Error + Send + Sync>>;

fn establish_connection() -> SqliteConnection {
dotenv().ok();

Expand All @@ -18,21 +20,15 @@ fn establish_connection() -> SqliteConnection {
.unwrap_or_else(|_| panic!("Error connecting to {database_url}"))
}

fn new_author(
conn: &mut SqliteConnection,
name: &str,
) -> Result<Author, Box<dyn Error + Send + Sync>> {
fn new_author(conn: &mut SqliteConnection, name: &str) -> DbResult<Author> {
let author = diesel::insert_into(authors::table)
.values(authors::name.eq(name))
.returning(Author::as_returning())
.get_result(conn)?;
Ok(author)
}

fn new_book(
conn: &mut SqliteConnection,
title: &str,
) -> Result<Book, Box<dyn Error + Send + Sync>> {
fn new_book(conn: &mut SqliteConnection, title: &str) -> DbResult<Book> {
let book = diesel::insert_into(books::table)
.values(books::title.eq(title))
.returning(Book::as_returning())
Expand All @@ -44,7 +40,7 @@ fn new_books_author(
conn: &mut SqliteConnection,
book_id: i32,
author_id: i32,
) -> Result<BookAuthor, Box<dyn Error + Send + Sync>> {
) -> DbResult<BookAuthor> {
let book_author = diesel::insert_into(books_authors::table)
.values((
books_authors::book_id.eq(book_id),
Expand All @@ -60,7 +56,7 @@ fn new_page(
page_number: i32,
content: &str,
book_id: i32,
) -> Result<Page, Box<dyn Error + Send + Sync>> {
) -> DbResult<Page> {
let page = diesel::insert_into(pages::table)
.values((
pages::page_number.eq(page_number),
Expand All @@ -72,7 +68,7 @@ fn new_page(
Ok(page)
}

fn joins(conn: &mut SqliteConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn joins(conn: &mut SqliteConnection) -> DbResult<()> {
let page_with_book = pages::table
.inner_join(books::table)
.filter(books::title.eq("Momo"))
Expand All @@ -90,7 +86,7 @@ fn joins(conn: &mut SqliteConnection) -> Result<(), Box<dyn Error + Send + Sync>
Ok(())
}

fn one_to_n_relations(conn: &mut SqliteConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn one_to_n_relations(conn: &mut SqliteConnection) -> DbResult<()> {
let momo = books::table
.filter(books::title.eq("Momo"))
.select(Book::as_select())
Expand Down Expand Up @@ -123,7 +119,7 @@ fn one_to_n_relations(conn: &mut SqliteConnection) -> Result<(), Box<dyn Error +
Ok(())
}

fn m_to_n_relations(conn: &mut SqliteConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn m_to_n_relations(conn: &mut SqliteConnection) -> DbResult<()> {
let astrid_lindgren = authors::table
.filter(authors::name.eq("Astrid Lindgren"))
.select(Author::as_select())
Expand Down Expand Up @@ -178,7 +174,7 @@ fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}

fn setup_data(conn: &mut SqliteConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn setup_data(conn: &mut SqliteConnection) -> DbResult<()> {
// create a book
let momo = new_book(conn, "Momo")?;

Expand Down
Loading