Struct Client

Source
pub struct Client {
    pub client: Client,
    pub username: String,
    pub logged: bool,
}
Expand description

Client you will do almost everything with

Fields§

§client: Client

The reqwest client

§username: String

username

§logged: bool

if client has token, this should be true and false otherwise

Implementations§

Source§

impl Client

Client functions return:

  • Err(1) if request is not Ok, usually when server is unreachable
  • Err(2) when json cannot be parsed, usually unreachable or due to incorrect parameters
  • Err(3) if the json contains success=0
Source

pub fn new(username: String) -> Self

create a new client with given username.

always use this to create a client

for requests you don’t need to be logged in to do, username will not be used

Examples found in repository?
examples/fast_people.rs (line 5)
3fn main() {
4    //make a client
5    let client = Client::new(String::from("Gemmady"));
6    let task_list = client.get_task_list(0, 1024, "", None, None).unwrap();
7    let mut hm = std::collections::HashMap::<String, u32>::new();
8    for i in task_list.tasks {
9        let best = client.get_stats(&i.name).unwrap().best;
10        if best.len() > 0 {
11            let t = hm.entry(best[0].username.clone()).or_insert(0);
12            *t += 1;
13        }
14    }
15    let mut v: Vec<(u32, String)> = hm.iter().map(|x| (*x.1, x.0.clone())).collect();
16    v.sort();
17    for i in v.iter().rev() {
18        println!("{} {}", i.1, i.0);
19    }
20}
More examples
Hide additional examples
examples/resubmit_all.rs (line 6)
3fn main(){
4    let username = String::from("user");
5    let password = "password";
6    let mut client = Client::new(username.clone());
7    client.login(password).unwrap();
8    let user = client.get_user(&username).unwrap();
9    for sc in user.scores.unwrap() {
10        if sc.score == 100.0 {
11            println!("{} has score 100", sc.title);
12            let sub_list = client.get_submission_list(&sc.name).unwrap();
13            let best_sub = sub_list.get_fastest_high(&client).unwrap();
14            let files = &best_sub.files;
15            if files.len() == 1 { // if it is not an output-only
16                let mut submitted: bool = false;
17                print!("Resubmitting {}", sc.name.clone());
18                while !submitted { // because cmsocial has a limit to submission rate
19                    print!(".");
20                    if client
21                        .submit_normal(
22                            &sc.name,
23                            &client.get_file(&files[0]).unwrap(),
24                            files[0].name.split(".").collect::<Vec<&str>>().last().unwrap(),
25                        )
26                        .is_ok()
27                    {
28                        submitted = true;
29                    }
30                }
31                println!("");
32            }
33        }
34    }
35}
Source

pub fn login(&mut self, password: &str) -> Result<bool, u8>

login with self.username and password

Returns Ok(true) if the client was already logged and Ok(false) if it was not and succeeds in logging

example is drop-down menu on the top-right corner

Examples found in repository?
examples/resubmit_all.rs (line 7)
3fn main(){
4    let username = String::from("user");
5    let password = "password";
6    let mut client = Client::new(username.clone());
7    client.login(password).unwrap();
8    let user = client.get_user(&username).unwrap();
9    for sc in user.scores.unwrap() {
10        if sc.score == 100.0 {
11            println!("{} has score 100", sc.title);
12            let sub_list = client.get_submission_list(&sc.name).unwrap();
13            let best_sub = sub_list.get_fastest_high(&client).unwrap();
14            let files = &best_sub.files;
15            if files.len() == 1 { // if it is not an output-only
16                let mut submitted: bool = false;
17                print!("Resubmitting {}", sc.name.clone());
18                while !submitted { // because cmsocial has a limit to submission rate
19                    print!(".");
20                    if client
21                        .submit_normal(
22                            &sc.name,
23                            &client.get_file(&files[0]).unwrap(),
24                            files[0].name.split(".").collect::<Vec<&str>>().last().unwrap(),
25                        )
26                        .is_ok()
27                    {
28                        submitted = true;
29                    }
30                }
31                println!("");
32            }
33        }
34    }
35}
Source

pub fn recover(&self, email: &str, code: &str) -> Result<RecoverResponse, u8>

recover lost password, use empty code to get the email

example cms page

Source

pub fn user_update( &self, email: &str, password: &str, old_password: &str, ) -> Result<(), u8>

update password/email, empty string for fields you dont want to update

example cms page

Source

pub fn check_username(&self, username: &str) -> Result<CheckResponse, u8>

check if username is valid, note: Ok does not mean username is valid

example is Username input

Source

pub fn check_email(&self, email: &str) -> Result<CheckResponse, u8>

check if email is valid, note: Ok does not mean email is valid

example is E-mail address input

Source

pub fn check_password(&self, password: &str) -> bool

check if password is valid, note: this is done locally

unlike other functions, this returs true if password is acceptable and false otherwise

example is Password input

Source

pub fn user_exists(&self, username: &str) -> Result<bool, u8>

check if there is an user with username = username

example is Username input

Source

pub fn get_user_list(&self, first: usize, last: usize) -> Result<UserList, u8>

get list of users in reverse order of score in [first,last)

example cms page

Source

pub fn get_user(&self, username: &str) -> Result<User, u8>

get the details of a specific user

example cms page

Examples found in repository?
examples/resubmit_all.rs (line 8)
3fn main(){
4    let username = String::from("user");
5    let password = "password";
6    let mut client = Client::new(username.clone());
7    client.login(password).unwrap();
8    let user = client.get_user(&username).unwrap();
9    for sc in user.scores.unwrap() {
10        if sc.score == 100.0 {
11            println!("{} has score 100", sc.title);
12            let sub_list = client.get_submission_list(&sc.name).unwrap();
13            let best_sub = sub_list.get_fastest_high(&client).unwrap();
14            let files = &best_sub.files;
15            if files.len() == 1 { // if it is not an output-only
16                let mut submitted: bool = false;
17                print!("Resubmitting {}", sc.name.clone());
18                while !submitted { // because cmsocial has a limit to submission rate
19                    print!(".");
20                    if client
21                        .submit_normal(
22                            &sc.name,
23                            &client.get_file(&files[0]).unwrap(),
24                            files[0].name.split(".").collect::<Vec<&str>>().last().unwrap(),
25                        )
26                        .is_ok()
27                    {
28                        submitted = true;
29                    }
30                }
31                println!("");
32            }
33        }
34    }
35}
Source

pub fn get_task_list( &self, first: usize, last: usize, order: &str, tag: Option<&str>, search: Option<&str>, ) -> Result<TaskList, u8>

get list of tasks in [first,last) in the given order with the given tag that matches search

possible orders are: newest, easiest, hardest

if an invalid order is given, it is assumed to be newest

example cms page

Examples found in repository?
examples/fast_people.rs (line 6)
3fn main() {
4    //make a client
5    let client = Client::new(String::from("Gemmady"));
6    let task_list = client.get_task_list(0, 1024, "", None, None).unwrap();
7    let mut hm = std::collections::HashMap::<String, u32>::new();
8    for i in task_list.tasks {
9        let best = client.get_stats(&i.name).unwrap().best;
10        if best.len() > 0 {
11            let t = hm.entry(best[0].username.clone()).or_insert(0);
12            *t += 1;
13        }
14    }
15    let mut v: Vec<(u32, String)> = hm.iter().map(|x| (*x.1, x.0.clone())).collect();
16    v.sort();
17    for i in v.iter().rev() {
18        println!("{} {}", i.1, i.0);
19    }
20}
Source

pub fn get_task(&self, name: &str) -> Result<DetailedTask, u8>

get the details of a specific task

example cms page

Source

pub fn get_stats(&self, name: &str) -> Result<Stats, u8>

get the statistics for a specific task

example cms page

Examples found in repository?
examples/fast_people.rs (line 9)
3fn main() {
4    //make a client
5    let client = Client::new(String::from("Gemmady"));
6    let task_list = client.get_task_list(0, 1024, "", None, None).unwrap();
7    let mut hm = std::collections::HashMap::<String, u32>::new();
8    for i in task_list.tasks {
9        let best = client.get_stats(&i.name).unwrap().best;
10        if best.len() > 0 {
11            let t = hm.entry(best[0].username.clone()).or_insert(0);
12            *t += 1;
13        }
14    }
15    let mut v: Vec<(u32, String)> = hm.iter().map(|x| (*x.1, x.0.clone())).collect();
16    v.sort();
17    for i in v.iter().rev() {
18        println!("{} {}", i.1, i.0);
19    }
20}
Source

pub fn get_submission_list(&self, task_name: &str) -> Result<SubmissionList, u8>

get your submissions for a task

example cms page

Examples found in repository?
examples/resubmit_all.rs (line 12)
3fn main(){
4    let username = String::from("user");
5    let password = "password";
6    let mut client = Client::new(username.clone());
7    client.login(password).unwrap();
8    let user = client.get_user(&username).unwrap();
9    for sc in user.scores.unwrap() {
10        if sc.score == 100.0 {
11            println!("{} has score 100", sc.title);
12            let sub_list = client.get_submission_list(&sc.name).unwrap();
13            let best_sub = sub_list.get_fastest_high(&client).unwrap();
14            let files = &best_sub.files;
15            if files.len() == 1 { // if it is not an output-only
16                let mut submitted: bool = false;
17                print!("Resubmitting {}", sc.name.clone());
18                while !submitted { // because cmsocial has a limit to submission rate
19                    print!(".");
20                    if client
21                        .submit_normal(
22                            &sc.name,
23                            &client.get_file(&files[0]).unwrap(),
24                            files[0].name.split(".").collect::<Vec<&str>>().last().unwrap(),
25                        )
26                        .is_ok()
27                    {
28                        submitted = true;
29                    }
30                }
31                println!("");
32            }
33        }
34    }
35}
Source

pub fn get_submission(&self, id: usize) -> Result<DetailedSubmission, u8>

get details for specific submission

example is clicking on submission id

Source

pub fn submit_normal( &self, task_name: &str, text: &str, lang: &str, ) -> Result<DetailedSubmission, u8>

submit a not output-only task

lang should be the extension of the file (c, cpp, pas)

example is clicking on submit button

Examples found in repository?
examples/resubmit_all.rs (lines 21-25)
3fn main(){
4    let username = String::from("user");
5    let password = "password";
6    let mut client = Client::new(username.clone());
7    client.login(password).unwrap();
8    let user = client.get_user(&username).unwrap();
9    for sc in user.scores.unwrap() {
10        if sc.score == 100.0 {
11            println!("{} has score 100", sc.title);
12            let sub_list = client.get_submission_list(&sc.name).unwrap();
13            let best_sub = sub_list.get_fastest_high(&client).unwrap();
14            let files = &best_sub.files;
15            if files.len() == 1 { // if it is not an output-only
16                let mut submitted: bool = false;
17                print!("Resubmitting {}", sc.name.clone());
18                while !submitted { // because cmsocial has a limit to submission rate
19                    print!(".");
20                    if client
21                        .submit_normal(
22                            &sc.name,
23                            &client.get_file(&files[0]).unwrap(),
24                            files[0].name.split(".").collect::<Vec<&str>>().last().unwrap(),
25                        )
26                        .is_ok()
27                    {
28                        submitted = true;
29                    }
30                }
31                println!("");
32            }
33        }
34    }
35}
Source

pub fn get_test_list(&self) -> Result<TestList, u8>

get the list of available tests

example cms page

Source

pub fn get_test(&self, test_name: &str) -> Result<Test, u8>

get the details and text of a specific test

example cms page

Source

pub fn get_region_list(&self) -> Result<RegionList, u8>

Misc get a list of the regions

example cms page

Source

pub fn get_technique_list(&self) -> Result<TechniqueList, u8>

get list of technique tags

example cms page

Source

pub fn get_file(&self, file: &File) -> Result<String, u8>

Examples found in repository?
examples/resubmit_all.rs (line 23)
3fn main(){
4    let username = String::from("user");
5    let password = "password";
6    let mut client = Client::new(username.clone());
7    client.login(password).unwrap();
8    let user = client.get_user(&username).unwrap();
9    for sc in user.scores.unwrap() {
10        if sc.score == 100.0 {
11            println!("{} has score 100", sc.title);
12            let sub_list = client.get_submission_list(&sc.name).unwrap();
13            let best_sub = sub_list.get_fastest_high(&client).unwrap();
14            let files = &best_sub.files;
15            if files.len() == 1 { // if it is not an output-only
16                let mut submitted: bool = false;
17                print!("Resubmitting {}", sc.name.clone());
18                while !submitted { // because cmsocial has a limit to submission rate
19                    print!(".");
20                    if client
21                        .submit_normal(
22                            &sc.name,
23                            &client.get_file(&files[0]).unwrap(),
24                            files[0].name.split(".").collect::<Vec<&str>>().last().unwrap(),
25                        )
26                        .is_ok()
27                    {
28                        submitted = true;
29                    }
30                }
31                println!("");
32            }
33        }
34    }
35}

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Err = <U as TryFrom<T>>::Err

Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Err>

Source§

impl<T> ErasedDestructor for T
where T: 'static,