Skip to content

Instantly share code, notes, and snippets.

@5nyper
Last active August 29, 2015 14:11
Show Gist options
  • Save 5nyper/100aa5b06a437e0d7262 to your computer and use it in GitHub Desktop.
Save 5nyper/100aa5b06a437e0d7262 to your computer and use it in GitHub Desktop.
Struct properties as variables
Im trying to get the struct to be renamed to what the user inputs, Notice I declared the `per` variables
input: `Class1 Java`
input: 'done'
Expected Output: '{
"Java": " ",
"per2": " ",
}
Actual Output: '{
"per1": " ",
"per2": " ",
}
And so on, Any help? Thanks in advance!
What other way can I accomplish this? if you have another way, please provide an example!
fn user() {
println!("--------------------------------------------\nEnter Class name first, then enter stuff for it. I.E: `Math Do homework`");
let mut stdin = io::stdin();
let (mut class1, mut class2, mut class3, mut class4, mut class5,
mut class6, mut class7, mut class8) =
(" ".to_string(), " ".to_string(), " ".to_string(), " ".to_string(),
" ".to_string(), " ".to_string(), " ".to_string(), " ".to_string());
let (mut per1, mut per2, mut per3, mut per4, mut per5,
mut per6, mut per7, mut per8) =
(" ".to_string(), " ".to_string(), " ".to_string(), " ".to_string(),
" ".to_string(), " ".to_string(), " ".to_string(), " ".to_string());
let mut reader = stdin.lock();
#[deriving(Encodable)]
struct Agenda {
per1: String,
per2: String,
per3: String,
per4: String,
per5: String,
per6: String,
per7: String,
per8: String,
}
let mut user =
Agenda{per1: class1.clone(),
per2: class2.clone(),
per3: class3.clone(),
per4: class4.clone(),
per5: class5.clone(),
per6: class6.clone(),
per7: class7.clone(),
per8: class8.clone(),};
for line in reader.lines() {
let mut buffer: Vec<u8> = Vec::new();
{
let mut encoder = PrettyEncoder::new(&mut buffer);
user.encode(&mut encoder).ok().expect("JSON encode error");
}
let encoded = String::from_utf8(buffer).unwrap().to_string();
let srt = line.unwrap();
let v: Vec<_> = srt.splitn(1, ' ').collect();
match v[0].trim() {
"Class1" => { user.per1 = v[1].trim().to_string(); }
"Class2" => { user.per2 = v[1].trim().to_string(); }
"Class3" => { user.per3 = v[1].trim().to_string(); }
"Class4" => { user.per4 = v[1].trim().to_string(); }
"Class5" => { user.per5 = v[1].trim().to_string(); }
"Class6" => { user.per6 = v[1].trim().to_string(); }
"Class7" => { user.per7 = v[1].trim().to_string(); }
"Class8" | "chemistry" | "chem" => {
per8 = v[1].trim().to_string();
}
"exit" => panic!("Exiting program!"),
"done" => {save(encoded);}
_ => println!("Input Invalid! from the outer braces"),
}
}
}
// Might be slightly different, maybe not Ive added then reverted alot.
extern crate serialize;
use std::io;
use std::io::fs::PathExtensions;
use std::io::{File};
use std::str::from_utf8;
use serialize::{Decodable, Encodable, json};
use serialize::json::PrettyEncoder;
use serialize::json::{Json};
fn main() {
println!("*******/// Agenda \\\\\\*******(v.1.0.0)");
println!("Developed by Karim Ellaisy, Written in Rust\n\n");
println!("Enter either `New` to add classes!");
let (mut class1, mut class2, mut class3, mut class4, mut class5,
mut class6, mut class7, mut class8) =
(" ".to_string(), " ".to_string(), " ".to_string(), " ".to_string(),
" ".to_string(), " ".to_string(), " ".to_string(), " ".to_string());
let mut stdin = io::stdin();
let mut reader = stdin.lock();
let mut name = "".to_string();
for line in reader.lines() {
let srt = line.unwrap();
let v: Vec<_> = srt.splitn(1, ' ').collect();
match v[0].trim() {
"New" => { savenew(encoded); }
// "enter" | "Enter" => { enter() }
"exit" => panic!("Exiting program!"),
_ => println!("Input Invalid!"),
}
}
}
fn savenew(x: String) {
let (mut per1, mut per2, mut per3, mut per4, mut per5,
mut per6, mut per7, mut per8) =
(" ".to_string(), " ".to_string(), " ".to_string(), " ".to_string(),
" ".to_string(), " ".to_string(), " ".to_string(), " ".to_string());
let mut stdin = io::stdin();
let mut name = "".to_string();
let mut reader = stdin.lock();
for line in reader.lines() {
let srt = line.unwrap();
let v: Vec<_> = srt.splitn(1, ' ').collect();
match v[0].trim() {
"Name" => { name = v[1].trim().to_string(); }
"Class1" => { per1 = v[1].trim().to_string(); }
"Class2" => { per2 = v[1].trim().to_string(); }
"Class3" => { per3 = v[1].trim().to_string(); }
"Class4" => { per4 = v[1].trim().to_string(); }
"Class5" => { per5 = v[1].trim().to_string(); }
"Class6" => { per6 = v[1].trim().to_string(); }
"Class7" => { per7 = v[1].trim().to_string(); }
"Class8" | "chemistry" | "chem" => {
per8 = v[1].trim().to_string();
}
"exit" => {panic!("Exiting program")}
"done" => {
let path = Path::new("agenda[karim].json");
// create the file, whether it exists or not
let mut file = File::create(&path);
file.write(y.as_bytes()).ok().expect("err writing");
// open the file in read-only mode
let mut file = File::open(&path);
file.read_to_end().ok().expect("err reading");
{
let mut file = File::open(&path);
let contents: String =
file.read_to_string().ok().expect("err reading");
println!("Agenda: {}" , contents);
}
}
_ => println!("Input Invalid! from the outer braces"),
}
}
}
fn main2() {
println!("*******/// Agenda \\\\\\*******(v.1.0.0)");
println!("Developed by Karim Ellaisy, Written in Rust\n\n");
println!("Enter either `Enter` to enter agenda, or `View` to view current agenda.");
let mut stdin = io::stdin();
let mut r = &mut File::open(&Path::new("agenda[karim].json"));
let json = json::from_reader(r);
let map =
match json {
Ok(Json::Object(map)) => map,
_ => { println!("ERROR"); return; }
};
let (mut class1, mut class2, mut class3, mut class4, mut class5,
mut class6, mut class7, mut class8) =
(map[*"AP_Java"].to_string(), map[*"Latin_III"].to_string(),
map[*"Algebra_II"].to_string(), map[*"Honors_English"].to_string(),
map[*"PRM"].to_string(), map[*"Honors_History"].to_string(),
map[*"Health"].to_string(), map[*"Chemistry"].to_string());
let mut reader = stdin.lock();
for line in reader.lines() {
let mut buffer: Vec<u8> = Vec::new();
{
let mut encoder = PrettyEncoder::new(&mut buffer);
karim.encode(&mut encoder).ok().expect("JSON encode error");
}
let encoded1 = String::from_utf8(buffer).unwrap().to_string();
let srt = line.unwrap();
let v: Vec<_> = srt.splitn(1, ' ').collect();
match v[0].trim() {
// "New" => { save(encoded1); }
// "enter" | "Enter" => { enter() }
"exit" => panic!("Exiting program!"),
_ => println!("Input Invalid!"),
}
}
}
/*let mut boi = let mut stdin = io::stdin();
let mut reader = stdin.lock();
let mut name = "".to_string();
for line in reader.lines() {
let srt = line.unwrap();
let v: Vec<_> = srt.splitn(1, ' ').collect();
match v[0].trim() {
"Java" | "java" => { per7 = v[1].trim().to_string(); }
"Latin" | "latin" => {
per6 = v[1].trim().to_string();
}
"Math" | "math" => { per5 = v[1].trim().to_string(); }
"english" | "English" => {
per3 = v[1].trim().to_string();
}
"PRM" | "prm" => { per4 = v[1].trim().to_string(); }
"history" => { per2 = v[1].trim().to_string(); }
"Health" | "health" => { per1 = v[1].trim().to_string(); }
"Chemistry" | "chemistry" | "chem" => {
per8 = v[1].trim().to_string();
}
"exit" => panic!("Exiting program!"),
// "save" | "Save" => { save(encoded1); }
// "view" | "View" => { view(); }
_ => println!("Input Invalid! from the sldfgel"),
}
}; */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment