mod agent; mod agent_catalog; mod api_key; mod approval; mod artifact_source; mod auth; mod connection; mod import_job; mod master_key; mod observability; mod onboarding; mod operation; mod operation_artifact; mod operation_published; mod pool_config; mod product_event; mod secret; mod upstream; mod workspace; mod yaml_import; use crank_core::{ AgentId, AgentOperationBinding, AgentStatus, AgentVersion, ApprovalRequest, ApprovalRequestId, AuthProfile, HttpMethod, InvitationId, InvitationToken, InvocationLog, InvocationLogId, MembershipRole, OperationId, OperationStatus, PlatformApiKey, PlatformApiKeyId, PlatformApiKeyStatus, ProductEventId, Secret, SecretId, SecretVersion, Target, UsageRollup, User, UserId, UserSessionId, Workspace, WorkspaceId, }; use serde::{Serialize, de::DeserializeOwned}; use serde_json::Value; use sqlx::{PgPool, Postgres, Row, Transaction, postgres::PgRow, types::Json}; use time::OffsetDateTime; pub use pool_config::{PostgresPoolConfig, PostgresPoolConfigError}; use crate::{ error::RegistryError, model::{ AdminBootstrapContractRecord, AdminSecurityAuditRequest, AgentSummary, AgentVersionRecord, AppendProductEventOutcome, AppendProductEventRequest, AppliedImportOperation, ApplyImportJobRequest, ApprovalRequestRecord, AuthUserRecord, ConsumeAdminBootstrapContractRequest, CreateAdminBootstrapContractRequest, CreateAgentDraftVersionRequest, CreateAgentRequest, CreateApprovalRequest, CreateImportJobRequest, CreateInvitationRequest, CreateInvocationLogRequest, CreatePlatformApiKeyRequest, CreateSecretRequest, CreateVersionRequest, CreateWorkspaceRequest, CreateYamlImportJobRequest, DecideApprovalRequest, DescriptorMetadata, ExpireApprovalRequest, FinishApprovalRequest, FinishImportJobRequest, ImportConflictMode, ImportJob, ImportJobApplyResult, ImportJobCleanupReport, ImportJobId, ImportJobStatus, InvitationRecord, InvocationHistoryLoss, InvocationHistoryLossCategory, InvocationHistoryWriteOutcome, InvocationLogRecord, InvocationRetentionOutcome, InvocationRetentionPolicy, InvocationRetentionStatus, ListApprovalRequestsQuery, ListInvocationLogsQuery, ListProductEventsQuery, MasterKeyIdentityCandidate, MasterKeyIdentityRecord, MasterKeyRotationRecord, MasterKeyRotationStatus, MembershipRecord, OnboardingMilestoneResult, OnboardingPresentationMilestone, OperationAgentRef, OperationSampleMetadata, OperationStateExpectation, OperationSummary, OperationUsageSummary, OperationVersionRecord, PlatformApiKeyRecord, ProductEventRecord, PublishAgentRequest, PublishRequest, PublishedAgentCatalog, PublishedAgentTool, RecordOnboardingCompletionRequest, RecordOnboardingMilestoneRequest, RegistryOperation, RotateSecretRequest, SaveAgentBindingsRequest, SaveAgentCatalogConfigRequest, SaveAuthProfileRequest, SaveDescriptorMetadataRequest, SaveSampleMetadataRequest, SaveWorkspaceUpstreamRequest, SecretRecord, SecretVersionRecord, SessionRecord, SkippedImportOperation, UpdateWorkspaceRequest, UsageAgentBreakdown, UsageOperationBreakdown, UsageOutcomeBreakdown, UsageQuery, UsageRollupRecord, UsageSummary, UsageTimelinePoint, WorkspaceMembershipRecord, WorkspaceRecord, WorkspaceUpstream, YamlImportJob, YamlImportJobCompletion, YamlImportJobId, YamlImportJobStatus, }, }; #[derive(Clone, Debug)] pub struct PostgresRegistry { pool: PgPool, } impl PostgresRegistry { async fn list_agent_bindings( &self, agent_id: &AgentId, version: u32, ) -> Result, RegistryError> { let rows = sqlx::query( "select operation_id, operation_version, tool_name, tool_title, tool_description_override, enabled from agent_operation_bindings where agent_id = $1 and agent_version = $2 order by tool_name asc", ) .bind(agent_id.as_str()) .bind(to_db_version(version)) .fetch_all(&self.pool) .await?; rows.iter().map(map_agent_binding).collect() } } async fn insert_version_row( tx: &mut Transaction<'_, Postgres>, snapshot: &RegistryOperation, change_note: Option<&str>, created_by: Option<&str>, ) -> Result<(), RegistryError> { sqlx::query( "insert into operation_versions ( operation_id, version, status, target_json, input_schema_json, output_schema_json, input_mapping_json, output_mapping_json, execution_config_json, tool_description_json, samples_json, generated_draft_json, config_export_json, wizard_state_json, name, display_name, category, protocol, security_level, snapshot_provenance, snapshot_observed_at, published_at, published_by, change_note, created_at, created_by ) values ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, 'native_v4', $20::timestamptz, $21::timestamptz, $22, $23, $24::timestamptz, $25 )", ) .bind(snapshot.id.as_str()) .bind(to_db_version(snapshot.version)) .bind(serialize_enum_text(&snapshot.status, "status")?) .bind(Json(serialize_json_value(&snapshot.target)?)) .bind(Json(serialize_json_value(&snapshot.input_schema)?)) .bind(Json(serialize_json_value(&snapshot.output_schema)?)) .bind(Json(serialize_json_value(&snapshot.input_mapping)?)) .bind(Json(serialize_json_value(&snapshot.output_mapping)?)) .bind(Json(serialize_json_value(&snapshot.execution_config)?)) .bind(Json(serialize_json_value(&snapshot.tool_description)?)) .bind(serialize_option_json_value(&snapshot.samples)?.map(Json)) .bind(serialize_option_json_value(&snapshot.generated_draft)?.map(Json)) .bind(serialize_option_json_value(&snapshot.config_export)?.map(Json)) .bind(serialize_option_json_value(&snapshot.wizard_state)?.map(Json)) .bind(&snapshot.name) .bind(&snapshot.display_name) .bind(&snapshot.category) .bind(serialize_enum_text(&snapshot.protocol, "protocol")?) .bind(serialize_enum_text( &snapshot.security_level, "security_level", )?) .bind(snapshot.updated_at) .bind(snapshot.published_at) .bind(Option::<&str>::None) .bind(change_note) .bind(snapshot.updated_at) .bind(created_by) .execute(&mut **tx) .await?; Ok(()) } async fn insert_operation_rows( tx: &mut Transaction<'_, Postgres>, workspace_id: &WorkspaceId, snapshot: &RegistryOperation, created_by: Option<&str>, ) -> Result<(), RegistryError> { sqlx::query( "insert into operations ( id, workspace_id, name, display_name, category, protocol, security_level, status, current_draft_version, latest_published_version, created_at, updated_at, published_at ) values ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11::timestamptz, $12::timestamptz, $13::timestamptz )", ) .bind(snapshot.id.as_str()) .bind(workspace_id.as_str()) .bind(&snapshot.name) .bind(&snapshot.display_name) .bind(&snapshot.category) .bind(serialize_enum_text(&snapshot.protocol, "protocol")?) .bind(serialize_enum_text( &snapshot.security_level, "security_level", )?) .bind(serialize_enum_text(&snapshot.status, "status")?) .bind(to_db_version(snapshot.version)) .bind( snapshot .published_at .as_ref() .map(|_| to_db_version(snapshot.version)), ) .bind(snapshot.created_at) .bind(snapshot.updated_at) .bind(snapshot.published_at) .execute(&mut **tx) .await?; insert_version_row(tx, snapshot, None, created_by).await } async fn insert_agent_version_row( tx: &mut Transaction<'_, Postgres>, version: &AgentVersion, ) -> Result<(), RegistryError> { sqlx::query( "insert into agent_versions ( agent_id, version, status, instructions_json, tool_selection_policy_json, created_at ) values ( $1, $2, $3, $4, $5, $6::timestamptz )", ) .bind(version.agent_id.as_str()) .bind(to_db_version(version.version)) .bind(serialize_enum_text(&version.status, "status")?) .bind(Json(version.instructions.clone())) .bind(Json(serialize_json_value(&version.tool_selection_policy)?)) .bind(version.created_at) .execute(&mut **tx) .await?; Ok(()) } async fn replace_agent_bindings_rows( tx: &mut Transaction<'_, Postgres>, workspace_id: &WorkspaceId, agent_id: &AgentId, version: u32, bindings: &[AgentOperationBinding], ) -> Result<(), RegistryError> { let operation_ids = bindings .iter() .map(|binding| binding.operation_id.as_str()) .collect::>(); for operation_id in operation_ids { let status = sqlx::query_scalar::<_, String>( "select status from operations where workspace_id = $1 and id = $2 for update", ) .bind(workspace_id.as_str()) .bind(operation_id) .fetch_optional(&mut **tx) .await?; match status.as_deref() { Some("archived") => { return Err(RegistryError::OperationArchived { operation_id: operation_id.to_owned(), }); } Some(_) => {} None => { return Err(RegistryError::OperationNotFound { operation_id: operation_id.to_owned(), }); } } } sqlx::query( "delete from agent_operation_bindings where agent_id = $1 and agent_version = $2", ) .bind(agent_id.as_str()) .bind(to_db_version(version)) .execute(&mut **tx) .await?; for binding in bindings { sqlx::query( "insert into agent_operation_bindings ( agent_id, agent_version, operation_id, operation_version, tool_name, tool_title, tool_description_override, enabled ) values ($1, $2, $3, $4, $5, $6, $7, $8)", ) .bind(agent_id.as_str()) .bind(to_db_version(version)) .bind(binding.operation_id.as_str()) .bind(to_db_version(binding.operation_version)) .bind(&binding.tool_name) .bind(&binding.tool_title) .bind(binding.tool_description_override.as_deref()) .bind(binding.enabled) .execute(&mut **tx) .await?; } Ok(()) } fn build_user( id: String, email: String, display_name: String, status: String, created_at: OffsetDateTime, ) -> Result { Ok(User { id: UserId::new(id), email, display_name, status: deserialize_enum_text(&status, "status")?, created_at, }) } fn map_user_update_error(error: sqlx::Error, user_id: &UserId, email: &str) -> RegistryError { match error { sqlx::Error::RowNotFound => RegistryError::UserNotFound { user_id: user_id.as_str().to_owned(), }, sqlx::Error::Database(database_error) if database_error.code().as_deref() == Some("23505") => { RegistryError::UserEmailAlreadyExists { email: email.to_owned(), } } other => RegistryError::Storage(other), } } fn map_invocation_log_record(row: &PgRow) -> Result { Ok(InvocationLogRecord { log: InvocationLog { id: InvocationLogId::new(row.try_get::("id")?), workspace_id: WorkspaceId::new(row.try_get::("workspace_id")?), agent_id: row .try_get::, _>("agent_id")? .map(AgentId::new), platform_api_key_id: row .try_get::, _>("platform_api_key_id")? .map(PlatformApiKeyId::new), operation_id: OperationId::new(row.try_get::("operation_id")?), operation_version: row .try_get::, _>("operation_version")? .map(|value| { u32::try_from(value).map_err(|_| RegistryError::InvalidNumericValue { field: "operation_version", value: i64::from(value), }) }) .transpose()?, source: deserialize_enum_text(&row.try_get::("source")?, "source")?, level: deserialize_enum_text(&row.try_get::("level")?, "level")?, status: deserialize_enum_text(&row.try_get::("status")?, "status")?, tool_name: row.try_get("tool_name")?, message: row.try_get("message")?, request_id: row.try_get("request_id")?, trace_id: row.try_get("trace_id")?, status_code: match row.try_get::, _>("status_code")? { Some(value) => { Some( u16::try_from(value).map_err(|_| RegistryError::InvalidNumericValue { field: "status_code", value: i64::from(value), })?, ) } None => None, }, duration_ms: to_u64(row.try_get::("duration_ms")?, "duration_ms")?, error_kind: row.try_get("error_kind")?, execution_stage: row .try_get::, _>("execution_stage")? .map(|value| deserialize_enum_text(&value, "execution_stage")) .transpose()?, execution_error_code: row .try_get::, _>("execution_error_code")? .map(|value| deserialize_enum_text(&value, "execution_error_code")) .transpose()?, retryability: row .try_get::, _>("retryability")? .map(|value| deserialize_enum_text(&value, "retryability")) .transpose()?, outcome_certainty: row .try_get::, _>("outcome_certainty")? .map(|value| deserialize_enum_text(&value, "outcome_certainty")) .transpose()?, request_preview: row.try_get::, _>("request_preview_json")?.0, response_preview: row.try_get::, _>("response_preview_json")?.0, created_at: row.try_get("created_at")?, }, operation_name: row.try_get("operation_name")?, operation_display_name: row.try_get("operation_display_name")?, agent_slug: row.try_get("agent_slug")?, agent_display_name: row.try_get("agent_display_name")?, }) } fn map_operation_usage_summary(row: &PgRow) -> Result { build_operation_usage_summary( row.try_get("operation_id")?, row.try_get("calls_today")?, row.try_get("error_rate_pct")?, row.try_get("avg_latency_ms")?, ) } fn target_summary(target: &Target) -> (String, String) { match target { Target::Rest(rest) => ( join_target_url(&rest.base_url, &rest.path_template), match rest.method { HttpMethod::Get => "GET", HttpMethod::Post => "POST", HttpMethod::Put => "PUT", HttpMethod::Patch => "PATCH", HttpMethod::Delete => "DELETE", HttpMethod::Head => "HEAD", HttpMethod::Options => "OPTIONS", } .to_owned(), ), } } fn join_target_url(base: &str, path: &str) -> String { if path.is_empty() { return base.to_owned(); } if path.starts_with("http://") || path.starts_with("https://") { return path.to_owned(); } let trimmed_base = base.trim_end_matches('/'); let trimmed_path = path.trim_start_matches('/'); if trimmed_base.is_empty() { format!("/{trimmed_path}") } else if trimmed_path.is_empty() { trimmed_base.to_owned() } else { format!("{trimmed_base}/{trimmed_path}") } } fn map_agent_binding(row: &PgRow) -> Result { Ok(AgentOperationBinding { operation_id: OperationId::new(row.try_get::("operation_id")?), operation_version: from_db_version(row.try_get("operation_version")?, "operation_version")?, tool_name: row.try_get("tool_name")?, tool_title: row.try_get("tool_title")?, tool_description_override: row.try_get("tool_description_override")?, enabled: row.try_get("enabled")?, }) } #[allow(clippy::too_many_arguments)] fn build_agent_summary( id: String, workspace_id: String, slug: String, display_name: String, description: String, status: String, current_draft_version: i32, latest_published_version: Option, catalog_revision: i64, created_at: OffsetDateTime, updated_at: OffsetDateTime, published_at: Option, ) -> Result { Ok(AgentSummary { id: AgentId::new(id), workspace_id: WorkspaceId::new(workspace_id), slug, display_name, description, status: deserialize_enum_text(&status, "status")?, current_draft_version: from_db_version(current_draft_version, "current_draft_version")?, latest_published_version: latest_published_version .map(|value| from_db_version(value, "latest_published_version")) .transpose()?, catalog_revision, created_at, updated_at, published_at, }) } #[allow(clippy::too_many_arguments)] fn build_operation_summary( id: String, workspace_id: String, name: String, display_name: String, category: String, protocol: String, security_level: String, target_json: Value, status: String, current_draft_version: i32, latest_published_version: Option, can_delete: bool, created_at: OffsetDateTime, updated_at: OffsetDateTime, published_at: Option, ) -> Result { let target: Target = deserialize_json_value(target_json)?; let (target_url, target_action) = target_summary(&target); Ok(OperationSummary { id: OperationId::new(id), workspace_id: WorkspaceId::new(workspace_id), name, display_name, category, protocol: deserialize_enum_text(&protocol, "protocol")?, security_level: deserialize_enum_text(&security_level, "security_level")?, target_url, target_action, status: deserialize_enum_text(&status, "status")?, current_draft_version: from_db_version(current_draft_version, "current_draft_version")?, latest_published_version: latest_published_version .map(|value| from_db_version(value, "latest_published_version")) .transpose()?, can_delete, created_at, updated_at, published_at, }) } fn build_auth_profile( id: String, workspace_id: String, name: String, kind: String, config_json: Value, created_at: OffsetDateTime, updated_at: OffsetDateTime, ) -> Result { Ok(AuthProfile { id: crank_core::AuthProfileId::new(id), workspace_id: WorkspaceId::new(workspace_id), name, kind: deserialize_enum_text(&kind, "kind")?, config: deserialize_json_value(config_json)?, created_at, updated_at, }) } fn build_operation_usage_summary( operation_id: String, calls_today: i64, error_rate_pct: f64, avg_latency_ms: i64, ) -> Result { Ok(OperationUsageSummary { operation_id: OperationId::new(operation_id), calls_today: to_u64(calls_today, "calls_today")?, error_rate_pct, avg_latency_ms: to_u64(avg_latency_ms, "avg_latency_ms")?, }) } fn build_operation_agent_ref( operation_id: String, agent_id: String, agent_slug: String, display_name: String, ) -> Result { Ok(OperationAgentRef { operation_id: OperationId::new(operation_id), agent_id: AgentId::new(agent_id), agent_slug, display_name, }) } fn build_agent_version_record( summary: &AgentSummary, version: i32, status: String, instructions_json: Value, tool_selection_policy_json: Value, created_at: OffsetDateTime, bindings: Vec, ) -> Result { let version = from_db_version(version, "version")?; let status = deserialize_enum_text(&status, "status")?; Ok(AgentVersionRecord { agent_id: summary.id.clone(), workspace_id: summary.workspace_id.clone(), version, status, created_at, bindings, snapshot: AgentVersion { agent_id: summary.id.clone(), version, status, instructions: instructions_json, tool_selection_policy: deserialize_json_value(tool_selection_policy_json)?, created_at, }, }) } #[allow(clippy::too_many_arguments)] fn build_operation_version_record( id: String, workspace_id: String, name: String, display_name: String, category: String, protocol: String, security_level: String, operation_created_at: OffsetDateTime, operation_updated_at: OffsetDateTime, operation_published_at: Option, version: i32, status: String, target_json: Value, input_schema_json: Value, output_schema_json: Value, input_mapping_json: Value, output_mapping_json: Value, execution_config_json: Value, tool_description_json: Value, samples_json: Option, generated_draft_json: Option, config_export_json: Option, wizard_state_json: Option, change_note: Option, created_at: OffsetDateTime, created_by: Option, ) -> Result { let operation_id = OperationId::new(id); let version = from_db_version(version, "version")?; let status = deserialize_enum_text(&status, "status")?; Ok(OperationVersionRecord { operation_id: operation_id.clone(), workspace_id: WorkspaceId::new(workspace_id), version, status, change_note, created_at, created_by, snapshot: RegistryOperation { id: operation_id, name, display_name, category, protocol: deserialize_enum_text(&protocol, "protocol")?, security_level: deserialize_enum_text(&security_level, "security_level")?, status, version, target: deserialize_json_value(target_json)?, input_schema: deserialize_json_value(input_schema_json)?, output_schema: deserialize_json_value(output_schema_json)?, input_mapping: deserialize_json_value(input_mapping_json)?, output_mapping: deserialize_json_value(output_mapping_json)?, execution_config: deserialize_json_value(execution_config_json)?, tool_description: deserialize_json_value(tool_description_json)?, samples: samples_json.map(deserialize_json_value).transpose()?, generated_draft: generated_draft_json .map(deserialize_json_value) .transpose()?, config_export: config_export_json.map(deserialize_json_value).transpose()?, wizard_state: wizard_state_json.map(deserialize_json_value).transpose()?, created_at: operation_created_at, updated_at: operation_updated_at, published_at: operation_published_at, }, }) } #[allow(clippy::too_many_arguments)] fn build_published_agent_tool( workspace_id: String, workspace_slug: String, agent_id: String, agent_slug: String, tool_name: String, tool_title: String, tool_description: String, operation: RegistryOperation, ) -> Result { Ok(PublishedAgentTool { workspace_id: WorkspaceId::new(workspace_id), workspace_slug, agent_id: AgentId::new(agent_id), agent_slug, tool_name, tool_title, tool_description, operation, }) } #[allow(clippy::too_many_arguments)] fn build_sample_metadata( id: String, operation_id: String, version: i32, sample_kind: String, storage_ref: String, content_type: String, file_name: Option, created_at: OffsetDateTime, ) -> Result { Ok(OperationSampleMetadata { id: crank_core::SampleId::new(id), operation_id: OperationId::new(operation_id), version: from_db_version(version, "version")?, sample_kind: deserialize_enum_text(&sample_kind, "sample_kind")?, storage_ref, content_type, file_name, created_at, }) } #[allow(clippy::too_many_arguments)] fn build_descriptor_metadata( id: String, operation_id: Option, version: Option, descriptor_kind: String, storage_ref: String, source_name: Option, package_index: Option, created_at: OffsetDateTime, ) -> Result { Ok(DescriptorMetadata { id: crank_core::DescriptorId::new(id), operation_id: operation_id.map(OperationId::new), version: version .map(|value| from_db_version(value, "version")) .transpose()?, descriptor_kind: deserialize_enum_text(&descriptor_kind, "descriptor_kind")?, storage_ref, source_name, package_index, created_at, }) } fn map_yaml_import_job(row: &PgRow) -> Result { Ok(YamlImportJob { id: YamlImportJobId::new(row.try_get::("id")?), source_sample_id: row .try_get::, _>("source_sample_id")? .map(crank_core::SampleId::new), status: deserialize_enum_text(&row.try_get::("status")?, "status")?, format_version: row.try_get("format_version")?, mode: deserialize_enum_text(&row.try_get::("mode")?, "mode")?, result_operation_id: row .try_get::, _>("result_operation_id")? .map(OperationId::new), result_version: row .try_get::, _>("result_version")? .map(|value| from_db_version(value, "result_version")) .transpose()?, error_text: row.try_get("error_text")?, created_at: row.try_get("created_at")?, finished_at: row.try_get("finished_at")?, }) } fn map_import_job(row: &PgRow) -> Result { Ok(ImportJob { id: ImportJobId::new(row.try_get::("id")?), workspace_id: WorkspaceId::new(row.try_get::("workspace_id")?), kind: deserialize_enum_text(&row.try_get::("kind")?, "kind")?, source_format: row.try_get("source_format")?, source_version: row.try_get("source_version")?, status: deserialize_enum_text(&row.try_get::("status")?, "status")?, preview_payload: row.try_get("preview_payload")?, created_operation_ids: row.try_get("created_operation_ids")?, error_text: row.try_get("error_text")?, created_at: row.try_get("created_at")?, expires_at: row.try_get("expires_at")?, finished_at: row.try_get("finished_at")?, }) } fn map_usage_operation_breakdown(row: &PgRow) -> Result { Ok(UsageOperationBreakdown { operation_id: OperationId::new(row.try_get::("operation_id")?), operation_name: row.try_get("operation_name")?, operation_display_name: row.try_get("operation_display_name")?, protocol: deserialize_enum_text(&row.try_get::("protocol")?, "protocol")?, calls_total: to_u64(row.try_get::("calls_total")?, "calls_total")?, calls_error: to_u64(row.try_get::("calls_error")?, "calls_error")?, p50_ms: to_u64(row.try_get::("p50_ms")?, "p50_ms")?, p95_ms: to_u64(row.try_get::("p95_ms")?, "p95_ms")?, p99_ms: to_u64(row.try_get::("p99_ms")?, "p99_ms")?, }) } fn map_usage_agent_breakdown(row: &PgRow) -> Result { Ok(UsageAgentBreakdown { agent_id: AgentId::new(row.try_get::("agent_id")?), agent_slug: row.try_get("agent_slug")?, agent_display_name: row.try_get("agent_display_name")?, calls_total: to_u64(row.try_get::("calls_total")?, "calls_total")?, calls_error: to_u64(row.try_get::("calls_error")?, "calls_error")?, p50_ms: to_u64(row.try_get::("p50_ms")?, "p50_ms")?, p95_ms: to_u64(row.try_get::("p95_ms")?, "p95_ms")?, p99_ms: to_u64(row.try_get::("p99_ms")?, "p99_ms")?, }) } fn serialize_json_value(value: &T) -> Result { Ok(serde_json::to_value(value)?) } fn serialize_option_json_value( value: &Option, ) -> Result, RegistryError> { value.as_ref().map(serialize_json_value).transpose() } fn deserialize_json_value(value: Value) -> Result { Ok(serde_json::from_value(value)?) } fn serialize_enum_text( value: &T, field: &'static str, ) -> Result { serde_json::to_value(value)? .as_str() .map(ToOwned::to_owned) .ok_or(RegistryError::InvalidEnumRepresentation { field }) } fn deserialize_enum_text( value: &str, field: &'static str, ) -> Result { serde_json::from_value(Value::String(value.to_owned())) .map_err(|_| RegistryError::InvalidEnumRepresentation { field }) } fn to_db_version(value: u32) -> i32 { value as i32 } fn from_db_version(value: i32, field: &'static str) -> Result { u32::try_from(value).map_err(|_| RegistryError::InvalidNumericValue { field, value: i64::from(value), }) } fn to_u64(value: i64, field: &'static str) -> Result { u64::try_from(value).map_err(|_| RegistryError::InvalidNumericValue { field, value }) } fn usage_bucket_sql(bucket: crate::model::UsageBucket) -> &'static str { match bucket { crate::model::UsageBucket::Hour => "hour", crate::model::UsageBucket::Day => "day", crate::model::UsageBucket::Week => "week", crate::model::UsageBucket::Month => "month", } }