carbon-gavel-decoder 1.0.0

Gavel Pool Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
pub mod lp_position_account_row;
pub mod pool_account_row;

pub use self::{lp_position_account_row::*, pool_account_row::*};
use super::GavelAccount;

pub struct GavelAccountsMigration;

impl sqlx_migrator::Migration<sqlx::Postgres> for GavelAccountsMigration {
    fn app(&self) -> &str {
        "gavel"
    }

    fn name(&self) -> &str {
        "gavel_accounts"
    }

    fn operations(&self) -> Vec<Box<dyn sqlx_migrator::Operation<sqlx::Postgres>>> {
        vec![
            Box::new(LpPositionAccountMigrationOperation),
            Box::new(PoolAccountMigrationOperation),
        ]
    }

    fn parents(&self) -> Vec<Box<dyn sqlx_migrator::Migration<sqlx::Postgres>>> {
        vec![]
    }
}

pub struct GavelAccountWithMetadata(pub GavelAccount, pub carbon_core::account::AccountMetadata);

impl From<(GavelAccount, carbon_core::account::AccountMetadata)> for GavelAccountWithMetadata {
    fn from(value: (GavelAccount, carbon_core::account::AccountMetadata)) -> Self {
        GavelAccountWithMetadata(value.0, value.1)
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for GavelAccountWithMetadata {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        let GavelAccountWithMetadata(account, metadata) = self;

        match account {
            GavelAccount::PoolAccount(account) => {
                let row = pool_account_row::PoolAccountRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
            GavelAccount::LpPositionAccount(account) => {
                let row = lp_position_account_row::LpPositionAccountRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.insert(pool).await?;
                Ok(())
            }
        }
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Upsert for GavelAccountWithMetadata {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        let GavelAccountWithMetadata(account, metadata) = self;
        match account {
            GavelAccount::PoolAccount(account) => {
                let row = pool_account_row::PoolAccountRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
            GavelAccount::LpPositionAccount(account) => {
                let row = lp_position_account_row::LpPositionAccountRow::from_parts(
                    *account.clone(),
                    metadata.clone(),
                );
                row.upsert(pool).await?;
                Ok(())
            }
        }
    }
}