1#![doc(html_root_url = "https://docs.rs/ic-oss-types/latest")]
2#![allow(clippy::needless_doctest_main)]
3
4use candid::{Nat, Principal};
5use ciborium::into_writer;
6use num_traits::cast::ToPrimitive;
7use serde::Serialize;
8use sha3::Digest;
9use std::collections::{BTreeMap, BTreeSet};
10
11pub mod bucket;
12pub mod cluster;
13pub mod cose;
14pub mod file;
15pub mod folder;
16pub mod object_store;
17pub mod permission;
18
19pub static ANONYMOUS: Principal = Principal::anonymous();
20pub const MILLISECONDS: u64 = 1_000_000u64;
21
22pub type MapValue =
24 BTreeMap<String, icrc_ledger_types::icrc::generic_metadata_value::MetadataValue>;
25
26pub fn format_error<T>(err: T) -> String
27where
28 T: std::fmt::Debug,
29{
30 format!("{:?}", err)
31}
32
33pub fn crc32(data: &[u8]) -> u32 {
34 let mut h = crc32fast::Hasher::new();
35 h.update(data);
36 h.finalize()
37}
38
39pub fn nat_to_u64(nat: &Nat) -> u64 {
40 nat.0.to_u64().unwrap_or(0)
41}
42
43pub fn to_cbor_bytes(obj: &impl Serialize) -> Vec<u8> {
45 let mut buf: Vec<u8> = Vec::new();
46 into_writer(obj, &mut buf).expect("failed to encode in CBOR format");
47 buf
48}
49
50pub fn sha3_256(data: &[u8]) -> [u8; 32] {
51 let mut hasher = sha3::Sha3_256::new();
52 hasher.update(data);
53 hasher.finalize().into()
54}
55
56pub fn validate_principals(principals: &BTreeSet<Principal>) -> Result<(), String> {
57 if principals.is_empty() {
58 return Err("principals cannot be empty".to_string());
59 }
60 if principals.contains(&ANONYMOUS) {
61 return Err("anonymous user is not allowed".to_string());
62 }
63 Ok(())
64}