carbon-wavebreak-decoder 2.0.0

Wavebreak Program Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::PermissionSigner,
    carbon_core::{
        account::AccountMetadata,
        postgres::{
            metadata::AccountRowMetadata,
            primitives::{Pubkey, U8},
        },
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PermissionConfigRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub consumer_program: Pubkey,
    pub allowed_signers: sqlx::types::Json<Vec<PermissionSigner>>,
    pub padding: Vec<U8>,
}

impl PermissionConfigRow {
    pub fn from_parts(
        source: crate::accounts::permission_config::PermissionConfig,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            consumer_program: source.consumer_program.into(),
            allowed_signers: sqlx::types::Json(source.allowed_signers.to_vec()),
            padding: source
                .padding
                .into_iter()
                .map(|element| element.into())
                .collect(),
        }
    }
}

impl TryFrom<PermissionConfigRow> for crate::accounts::permission_config::PermissionConfig {
    type Error = carbon_core::error::Error;
    fn try_from(source: PermissionConfigRow) -> Result<Self, Self::Error> {
        Ok(Self {
            consumer_program: *source.consumer_program,
            allowed_signers: source
                .allowed_signers
                .0
                .into_iter()
                .collect::<Vec<_>>()
                .try_into()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert value from postgres primitive".to_string(),
                    )
                })?,
            padding: source
                .padding
                .into_iter()
                .map(|element| {
                    element.try_into().map_err(|_| {
                        carbon_core::error::Error::Custom(
                            "Failed to convert value from postgres primitive".to_string(),
                        )
                    })
                })
                .collect::<Result<Vec<_>, carbon_core::error::Error>>()?
                .try_into()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert array element to primitive".to_string(),
                    )
                })?,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::permission_config::PermissionConfig
{
    fn table() -> &'static str {
        "permission_config_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "consumer_program",
            "allowed_signers",
            "padding",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PermissionConfigRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO permission_config_account (
                "consumer_program",
                "allowed_signers",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5
            )"#,
        )
        .bind(self.consumer_program)
        .bind(&self.allowed_signers)
        .bind(&self.padding)
        .bind(self.account_metadata.pubkey)
        .bind(&self.account_metadata.slot)
        .execute(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(())
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Upsert for PermissionConfigRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO permission_config_account (
                "consumer_program",
                "allowed_signers",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "consumer_program" = EXCLUDED."consumer_program",
                "allowed_signers" = EXCLUDED."allowed_signers",
                "padding" = EXCLUDED."padding",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.consumer_program)
        .bind(&self.allowed_signers)
        .bind(&self.padding)
        .bind(self.account_metadata.pubkey)
        .bind(&self.account_metadata.slot)
        .execute(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(())
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Delete for PermissionConfigRow {
    type Key = carbon_core::postgres::primitives::Pubkey;

    async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"DELETE FROM permission_config_account WHERE
                __pubkey = $1
            "#,
        )
        .bind(key)
        .execute(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(())
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Lookup for PermissionConfigRow {
    type Key = carbon_core::postgres::primitives::Pubkey;

    async fn lookup(
        key: Self::Key,
        pool: &sqlx::PgPool,
    ) -> carbon_core::error::CarbonResult<Option<Self>> {
        let row = sqlx::query_as(
            r#"SELECT * FROM permission_config_account WHERE
                __pubkey = $1
            "#,
        )
        .bind(key)
        .fetch_optional(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(row)
    }
}

pub struct PermissionConfigMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PermissionConfigMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS permission_config_account (
                -- Account data
                "consumer_program" BYTEA NOT NULL,
                "allowed_signers" JSONB NOT NULL,
                "padding" INT2[] NOT NULL,
                -- Account metadata
                __pubkey BYTEA NOT NULL,
                __slot NUMERIC(20),
                PRIMARY KEY (__pubkey)
            )"#,
        )
        .execute(connection)
        .await?;
        Ok(())
    }

    async fn down(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(r#"DROP TABLE IF EXISTS permission_config_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}