Repository: libsql/libsql
File: libsql-server/src/http/admin/mod.rs (Rust)

Code before:
    /// If true, current namespace acts as a DB used solely for multi-db schema updates.
    #[serde(default)]
    shared_schema: bool,
    /// If some, this is a [NamespaceName] reference to a shared schema DB.
    #[serde(default)]
    shared_schema_name: Option<String>,
}

async fn handle_create_namespace<M: MakeNamespace, C: Connector>(
    State(app_state): State<Arc<AppState<M, C>>>,
    Path(namespace): Path<String>,
    Json(req): Json<CreateNamespaceReq>,
) -> crate::Result<()> {
    if let Some(jwt_key) = req.jwt_key.as_deref() {
        // Check that the jwt key is correct
        parse_jwt_key(jwt_key)?;
    }
    let (shared_schema_name, shared_schema_namespace_name) =
        if let Some(ns) = req.shared_schema_name {
            if req.shared_schema {
                return Err(Error::SharedSchemaCreationError(
                    "shared schema database cannot reference another shared schema".to_string(),
                ));
            }
            let name_string = ns.clone();
            let namespace = NamespaceName::from_string(ns)?;
            if !app_state.namespaces.exists(&namespace) {
                return Err(Error::NamespaceDoesntExist(namespace.to_string()));
            }
            (Some(name_string), Some(namespace))
        } else {
            (None, None)
        };
    let dump = match req.dump_url {
        Some(ref url) => {
            RestoreOption::Dump(dump_stream_from_url(url, app_state.connector.clone()).await?)
        }
        None => RestoreOption::Latest,
    };

    let bottomless_db_id = match req.bottomless_db_id {
        Some(db_id) => NamespaceBottomlessDbId::Namespace(db_id),
        None => NamespaceBottomlessDbId::NotProvided,
    };

    let namespace = NamespaceName::from_string(namespace)?;
    app_state
        .namespaces
        .create(namespace.clone(), dump, bottomless_db_id)
        .await?;


Review comment (suggestion):
`NamespaceName` implements `Deserialize`, so you can change `CreateNamespaceReq.shared_schema_name` to  be a `NamespaceName` instead of a string, and skip conversion here.

Code after:
    /// If true, current namespace acts as a DB used solely for multi-db schema updates.
    #[serde(default)]
    shared_schema: bool,
    /// If some, this is a [NamespaceName] reference to a shared schema DB.
    #[serde(default)]
    shared_schema_name: Option<NamespaceName>,
}

async fn handle_create_namespace<M: MakeNamespace, C: Connector>(
    State(app_state): State<Arc<AppState<M, C>>>,
    Path(namespace): Path<String>,
    Json(req): Json<CreateNamespaceReq>,
) -> crate::Result<()> {
    if let Some(jwt_key) = req.jwt_key.as_deref() {
        // Check that the jwt key is correct
        parse_jwt_key(jwt_key)?;
    }
    let shared_schema_name = if let Some(ns) = req.shared_schema_name {
        if req.shared_schema {
            return Err(Error::SharedSchemaCreationError(
                "shared schema database cannot reference another shared schema".to_string(),
            ));
        }
        // TODO: move this check into meta store
        if !app_state.namespaces.exists(&ns) {
            return Err(Error::NamespaceDoesntExist(ns.to_string()));
        }
        Some(ns)
    } else {
        None
    };
    let dump = match req.dump_url {
        Some(ref url) => {
            RestoreOption::Dump(dump_stream_from_url(url, app_state.connector.clone()).await?)
        }
        None => RestoreOption::Latest,
    };

    let bottomless_db_id = match req.bottomless_db_id {
        Some(db_id) => NamespaceBottomlessDbId::Namespace(db_id),
        None => NamespaceBottomlessDbId::NotProvided,
    };

    let namespace = NamespaceName::from_string(namespace)?;
    app_state
        .namespaces
        .create(namespace.clone(), dump, bottomless_db_id)
        .await?;

    let store = app_state.namespaces.config_store(namespace).await?;
    let mut config = (*store.get()).clone();