From 50fcf33af4c26156c69a2f26a42e18a1483edd6e Mon Sep 17 00:00:00 2001 From: Taah Date: Fri, 13 May 2022 23:01:55 -0700 Subject: [PATCH] First rust program! --- .gitignore | 3 ++ Cargo.lock | 73 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 11 +++++++ rust-learning.iml | 12 ++++++++ src/main.rs | 57 ++++++++++++++++++++++++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 rust-learning.iml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ff15b8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +/.idea +/*.yml \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..63785e3 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,73 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" + +[[package]] +name = "indexmap" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f647032dfaa1f8b6dc29bd3edb7bbef4861b8b8007ebb118d6db284fd59f6ee" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "linked-hash-map" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" + +[[package]] +name = "rust-learning" +version = "0.1.0" +dependencies = [ + "serde", + "serde_yaml", + "yaml-rust", +] + +[[package]] +name = "ryu" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" + +[[package]] +name = "serde" +version = "1.0.137" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" + +[[package]] +name = "serde_yaml" +version = "0.8.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "707d15895415db6628332b737c838b88c598522e4dc70647e59b72312924aebc" +dependencies = [ + "indexmap", + "ryu", + "serde", + "yaml-rust", +] + +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..74f17dd --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rust-learning" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +yaml-rust = "0.4" +serde = "1.0" +serde_yaml = "0.8" diff --git a/rust-learning.iml b/rust-learning.iml new file mode 100644 index 0000000..2fecef3 --- /dev/null +++ b/rust-learning.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d6e79e7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,57 @@ +use std::{fs, io}; +use std::borrow::Borrow; +use std::collections::{BTreeMap, BTreeSet, HashMap}; +use std::fmt::format; +use std::ops::Index; + +use yaml_rust::{Yaml, YamlEmitter, YamlLoader}; +use yaml_rust::yaml::Array; + +fn main() { + let fileData = fs::read_to_string("indefinitebans.yml").expect("Unable to read this file! Perhaps it's corrupt or the file was not found."); + let yaml = &YamlLoader::load_from_str(&fileData).expect("This file couldn't be loaded as a YAML")[0]; + let parentHash = yaml.as_hash().expect("Unable to load everything into a hash"); + let mut map: BTreeMap>> = BTreeMap::new(); + let mut index = 0; + for i in 0..parentHash.keys().len() + { + let k = parentHash.iter().skip(i).next().unwrap().0; + println!("Key: {:?}", k); + let mut data: BTreeMap> = BTreeMap::new(); + let mut names: Vec = Vec::new(); + if k.as_str().is_some() { + names.push(k.as_str().unwrap().to_string()); + data.insert("users".to_string(), names); + } else { + let str = String::from(k.to_owned().as_i64().clone().unwrap().to_string()); + names.push(str); + data.insert("users".to_string(), names); + } + let hash = parentHash[k].as_hash().expect("Couldn't load key as hash"); + if hash.contains_key(&Yaml::String("uuid".to_string())) { + let mut uuids: Vec = Vec::new(); + uuids.push(hash.get(&Yaml::String("uuid".to_string())).unwrap().to_owned().into_string().unwrap()); + data.insert("uuids".to_string(), uuids); + } + if hash.contains_key(&Yaml::String("ips".to_string())) { + if hash.get(&Yaml::String("ips".to_string())).unwrap().is_array() { + let mut ips: Vec = Vec::new(); + for x in hash.get(&Yaml::String("ips".to_string())).unwrap().as_vec().unwrap() { + ips.push(x.to_owned().into_string().unwrap()); + } + data.insert("ips".to_string(), ips); + } + } + map.insert(i, data); + println!("Inserted {}", i); + } + + let s = serde_yaml::to_string(&map).expect("Unable to convert to string"); + println!("{:?}", s); + println!("{:?}", &s[0..4]); + fs::write("./indefbans.yml", &s[4..]).expect("Unable to write to file"); +} + +fn add_one(num: &mut i32) { + *num += 1; +}