use axum::{
extract::{Path, State},
http::StatusCode,
Json,
};
use std::sync::Arc;
use crate::{
models::{ErrorResponse, SynthDefInfo},
AppState,
};
pub async fn list_synthdefs(State(state): State<Arc<AppState>>) -> Json<Vec<SynthDefInfo>> {
let synthdefs = state
.with_state(|s| {
s.synthdefs
.iter()
.map(|name| SynthDefInfo {
name: name.clone(),
params: Vec::new(), source: None, })
.collect::<Vec<_>>()
})
.await;
Json(synthdefs)
}
pub async fn get_synthdef(
State(state): State<Arc<AppState>>,
Path(name): Path<String>,
) -> Result<Json<SynthDefInfo>, (StatusCode, Json<ErrorResponse>)> {
let exists = state.with_state(|s| s.synthdefs.contains(&name)).await;
if exists {
Ok(Json(SynthDefInfo {
name,
params: Vec::new(), source: None, }))
} else {
Err((
StatusCode::NOT_FOUND,
Json(ErrorResponse::not_found(&format!(
"SynthDef '{}' not found",
name
))),
))
}
}