gsora

Untitled

Mar 23rd, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.20 KB | None | 0 0
  1. use std::io::Write;
  2.  
  3. use bincode::{config, Decode, Encode};
  4. use pddb::{Pddb, PddbKey};
  5.  
  6. const DICT_NAME: &'static str = "irc";
  7. const USER_NETWORKS_KEY: &'static str = "user_networks";
  8. const NETWORK_STR_MAX_CHARS: usize = 512;
  9.  
  10. #[derive(Encode, Decode, PartialEq, Default, Debug)]
  11. pub(crate) struct Network {
  12.     pub channel: std::string::String,
  13.     pub server: std::string::String,
  14.     pub nickname: std::string::String,
  15. }
  16.  
  17. #[derive(Encode, Decode, PartialEq, Debug, Default)]
  18. pub(crate) struct Networks {
  19.     pub networks: Vec<Network>,
  20. }
  21.  
  22. fn store_user_network() {
  23.     let mut pddb = pddb::Pddb::new();
  24.  
  25.     let mut existing_user_networks: PddbKey = pddb
  26.         .get(
  27.             DICT_NAME,
  28.             USER_NETWORKS_KEY,
  29.             None,
  30.             true,
  31.             true,
  32.             None,
  33.             Some(|| {}),
  34.         )
  35.         .expect("cannot get existing user networks key");
  36.  
  37.     let mut networks = Networks::default();
  38.     let nslice = bincode::encode_to_vec(networks, bincode::config::standard().with_big_endian())
  39.         .expect("cannot encode networks");
  40.  
  41.     existing_user_networks
  42.         .write(&nslice)
  43.         .expect("cannot write networks to pddb");
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment