mirror of
https://github.com/plexusorg/IBConverter.git
synced 2024-12-21 23:35:06 +00:00
First rust program!
This commit is contained in:
commit
50fcf33af4
5 changed files with 156 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
/target
|
||||
/.idea
|
||||
/*.yml
|
73
Cargo.lock
generated
Normal file
73
Cargo.lock
generated
Normal file
|
@ -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",
|
||||
]
|
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
|
@ -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"
|
12
rust-learning.iml
Normal file
12
rust-learning.iml
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="RUST_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
57
src/main.rs
Normal file
57
src/main.rs
Normal file
|
@ -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<usize, BTreeMap<String, Vec<String>>> = 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<String, Vec<String>> = BTreeMap::new();
|
||||
let mut names: Vec<String> = 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<String> = 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<String> = 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;
|
||||
}
|
Loading…
Reference in a new issue