cap_sdk_core/router.rs
1//! Contains logic for interacting with cap's main router.
2//!
3//! For more information on the purpose of the main router, see the documentation on
4//! [`Router`].
5
6use ic_kit::candid::CandidType;
7use ic_kit::{ic::call, Principal, RejectionCode};
8use serde::{Deserialize, Serialize};
9
10/// A router.
11///
12/// The main router is an extended version of [`Index`] which allows registration of a new
13/// contract to cap. It also contains global index canister registry which allows querying
14/// for the root bucket of a contract.
15///
16/// A router bucket implements the same interface as [`Index`], but with 1 additional method.
17///
18/// Use [`Router`]'s [`Into<Index>`] implementation to use a [`Router`] as an [`Index`].
19#[derive(Debug, Clone, Copy, Serialize, Deserialize, CandidType)]
20pub struct Router(pub Principal);
21
22impl Router {
23 pub fn new(principal: Principal) -> Self {
24 Self(principal)
25 }
26
27 pub async fn install_code(&self, canister: Principal) -> Result<(), (RejectionCode, String)> {
28 call(self.0, "install_bucket_code", (canister,)).await?;
29
30 Ok(())
31 }
32}