diff --git a/examples/data.rs b/examples/data.rs index 6149e3f..7de6285 100644 --- a/examples/data.rs +++ b/examples/data.rs @@ -3,11 +3,11 @@ use pastemyst::data::*; #[tokio::main] async fn main() -> DataResult<()> { // Get language by name. - tokio::task::spawn_blocking(||call_get_language_by_name().unwrap()); + tokio::task::spawn_blocking(|| call_get_language_by_name().unwrap()); call_get_language_by_name_async().await?; // Get language by extension. - tokio::task::spawn_blocking(||call_get_language_by_extension().unwrap()); + tokio::task::spawn_blocking(|| call_get_language_by_extension().unwrap()); call_get_language_by_extension_async().await?; Ok(()) } diff --git a/examples/paste.rs b/examples/paste.rs index 30fbed1..2d081c2 100644 --- a/examples/paste.rs +++ b/examples/paste.rs @@ -1,5 +1,5 @@ -use pastemyst::str; use pastemyst::paste::*; +use pastemyst::str; type Error = Box; type Result = std::result::Result; @@ -7,7 +7,7 @@ type Result = std::result::Result; #[tokio::main] async fn main() -> Result<()> { // Create pastes - tokio::task::spawn_blocking(||call_create_paste().unwrap()); + tokio::task::spawn_blocking(|| call_create_paste().unwrap()); call_create_paste_async().await?; // tokio::task::spawn_blocking( // ||call_create_private_paste( @@ -16,7 +16,7 @@ async fn main() -> Result<()> { // "Your PasteMyst Token. Get it from: https://paste.myst.rs/user/settings").await?; // Get pastes - tokio::task::spawn_blocking(||call_get_paste().unwrap()); + tokio::task::spawn_blocking(|| call_get_paste().unwrap()); call_get_paste_async().await?; // tokio::task::spawn_blocking( // ||call_get_private_paste( @@ -39,7 +39,9 @@ fn call_create_paste() -> Result<(), reqwest::Error> /*PasteResult<()>*/ { _id: str!(""), title: "Another pasty title".to_string(), language: str!(pastemyst::data::language::CLANG), - code: String::from("#include \"stdio.h\"\n\nint main() {\n\tprintf(\"Hello World!\");\n}"), + code: String::from( + "#include \n\nint main() {\n\tprintf(\"Hello World!\");\n}", + ), }, ]; let data: CreateObject = CreateObject { @@ -68,7 +70,9 @@ async fn call_create_paste_async() -> Result<()> { _id: str!(""), title: "Another pasty title".to_string(), language: str!(pastemyst::data::language::CLANG), - code: String::from("#include \"stdio.h\"\n\nint main() {\n\tprintf(\"Hello World!\");\n}"), + code: String::from( + "#include \n\nint main() {\n\tprintf(\"Hello World!\");\n}", + ), }, ]; let data: CreateObject = CreateObject { @@ -98,7 +102,9 @@ fn call_create_private_paste(auth_token: &str) -> PasteResult<()> { _id: str!(""), title: "Another pasty title".to_string(), language: str!(pastemyst::data::language::CLANG), - code: String::from("#include \"stdio.h\"\n\nint main() {\n\tprintf(\"Hello World!\");\n}"), + code: String::from( + "#include \"stdio.h\"\n\nint main() {\n\tprintf(\"Hello World!\");\n}", + ), }, ]; let data: CreateObject = CreateObject { @@ -109,10 +115,7 @@ fn call_create_private_paste(auth_token: &str) -> PasteResult<()> { tags: String::from(""), pasties, }; - let paste = create_private_paste( - data, - auth_token, - )?; + let paste = create_private_paste(data, auth_token)?; println!("https://paste.myst.rs/{}", paste._id); Ok(()) } @@ -131,7 +134,9 @@ async fn call_create_private_paste_async(auth_token: &str) -> PasteResult<()> { _id: str!(""), title: "Another pasty title".to_string(), language: str!(pastemyst::data::language::CLANG), - code: String::from("#include \"stdio.h\"\n\nint main() {\n\tprintf(\"Hello World!\");\n}"), + code: String::from( + "#include \"stdio.h\"\n\nint main() {\n\tprintf(\"Hello World!\");\n}", + ), }, ]; let data: CreateObject = CreateObject { @@ -142,10 +147,7 @@ async fn call_create_private_paste_async(auth_token: &str) -> PasteResult<()> { tags: String::from(""), pasties, }; - let paste = create_private_paste( - data, - auth_token, - )?; + let paste = create_private_paste(data, auth_token)?; println!("https://paste.myst.rs/{}", paste._id); Ok(()) } @@ -173,7 +175,8 @@ async fn call_get_paste_async() -> PasteResult<()> { fn call_get_private_paste() -> PasteResult<()> { let paste: PasteObject = get_private_paste( "pasteID", - "Your PasteMyst Token. Get it from: https://paste.myst.rs/user/settings")?; + "Your PasteMyst Token. Get it from: https://paste.myst.rs/user/settings", + )?; println!("{}", paste.ownerId); Ok(()) } @@ -181,11 +184,7 @@ fn call_get_private_paste() -> PasteResult<()> { /// Gets a private paste from pastemyst asynchronously. #[allow(dead_code)] async fn call_get_private_paste_async(auth_token: &str) -> PasteResult<()> { - let paste: PasteObject = get_private_paste_async( - "pasteID", - auth_token, - ) - .await?; + let paste: PasteObject = get_private_paste_async("pasteID", auth_token).await?; println!("{}", paste.isPrivate); Ok(()) } diff --git a/examples/time.rs b/examples/time.rs index bf17ca3..04c93db 100644 --- a/examples/time.rs +++ b/examples/time.rs @@ -2,7 +2,7 @@ use pastemyst::time::*; #[tokio::main] async fn main() -> TimeResult<()> { - tokio::task::spawn_blocking(||call_expires_into_unix().unwrap()); + tokio::task::spawn_blocking(|| call_expires_into_unix().unwrap()); call_expires_into_unix_async().await?; Ok(()) } diff --git a/examples/user.rs b/examples/user.rs index cb598a2..991b575 100644 --- a/examples/user.rs +++ b/examples/user.rs @@ -3,11 +3,11 @@ use pastemyst::user::*; #[tokio::main] async fn main() -> UserResult<()> { // Get a user. - tokio::task::spawn_blocking(||call_get_user().unwrap()); + tokio::task::spawn_blocking(|| call_get_user().unwrap()); call_get_user_async().await?; // Check if a user exists. - tokio::task::spawn_blocking(||call_user_exists().unwrap()); + tokio::task::spawn_blocking(|| call_user_exists().unwrap()); call_user_exists_async().await?; Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 8361669..c3eac85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,7 +45,7 @@ pub mod time { /// the default value of `reqwest::Error`. Keep note /// that `E` can be overriden. pub type TimeResult = Result; - + const TIME_ENDPOINT: &str = "https://paste.myst.rs/api/v2/time/expiresInToUnixTime"; /// All the possible values of the @@ -120,7 +120,8 @@ pub mod time { /// ``` pub fn expires_into_unix(created_at: u64, expires_in: &str) -> TimeResult { let mut response: TimeObject = TimeObject { result: 0 }; - #[allow(unused_assignments)] let mut valid_time: bool = false; + #[allow(unused_assignments)] + let mut valid_time: bool = false; match expires_in { expires_in::NEVER => valid_time = true, expires_in::ONE_HOUR => valid_time = true, @@ -130,12 +131,17 @@ pub mod time { expires_in::ONE_WEEK => valid_time = true, expires_in::ONE_MONTH => valid_time = true, expires_in::ONE_YEAR => valid_time = true, - _ => valid_time = false + _ => valid_time = false, } if valid_time { response = reqwest::blocking::get(&parse_time(created_at, expires_in))? - .json().unwrap(); - } else { println!("[pastemyst] The given expires timestamp is not valid and 0 will be returned."); } + .json() + .unwrap(); + } else { + println!( + "[pastemyst] The given expires timestamp is not valid and 0 will be returned." + ); + } Ok(response.result) } @@ -173,7 +179,8 @@ pub mod time { /// ``` pub async fn expires_into_unix_async(created_at: u64, expires_in: &str) -> TimeResult { let mut response: TimeObject = TimeObject { result: 0 }; - #[allow(unused_assignments)] let mut valid_time: bool = false; + #[allow(unused_assignments)] + let mut valid_time: bool = false; match expires_in { expires_in::NEVER => valid_time = true, expires_in::ONE_HOUR => valid_time = true, @@ -183,15 +190,21 @@ pub mod time { expires_in::ONE_WEEK => valid_time = true, expires_in::ONE_MONTH => valid_time = true, expires_in::ONE_YEAR => valid_time = true, - _ => valid_time = false + _ => valid_time = false, } if valid_time { response = reqwest::Client::builder() .build()? .get(&parse_time(created_at, expires_in)) - .send().await? - .json().await?; - } else { println!("[pastemyst] The given expires timestamp is not valid and 0 will be returned."); } + .send() + .await? + .json() + .await?; + } else { + println!( + "[pastemyst] The given expires timestamp is not valid and 0 will be returned." + ); + } Ok(response.result) } @@ -204,14 +217,16 @@ pub mod time { /// This is the main reason why this /// struct has been kept private. #[derive(Deserialize)] - struct TimeObject { result: u64 } + struct TimeObject { + result: u64, + } /// Parses the time module's API path fn parse_time(created_at: u64, expires_in: &str) -> String { return format!( "{}?createdAt={}&expiresIn={}", TIME_ENDPOINT, created_at, expires_in - ) + ); } } @@ -254,7 +269,7 @@ pub mod data { /// } /// ``` pub fn get_language_by_name(language_name: &str) -> DataResult { - Ok(reqwest::blocking::get(&parse_url(language_name, "name"))?.json()?) + reqwest::blocking::get(&parse_url(language_name, "name"))?.json() } /// Get information on a specific language *supported by PasteMyst*. @@ -281,8 +296,13 @@ pub mod data { /// Ok(()) /// } /// ``` - pub async fn get_language_by_name_async(language_name: &str) -> DataResult { - Ok(reqwest::get(&parse_url(language_name, "name")).await?.json().await?) + pub async fn get_language_by_name_async( + language_name: &str, + ) -> DataResult { + Ok(reqwest::get(&parse_url(language_name, "name")) + .await? + .json() + .await?) } /// The same thing as getting a language by a name, except that it is by @@ -308,8 +328,10 @@ pub mod data { /// Ok(()) /// } /// ``` - pub fn get_language_by_extension(lang_extension: &str) -> DataResult { - Ok(reqwest::blocking::get(&parse_url(lang_extension, "ext"))?.json()?) + pub fn get_language_by_extension( + lang_extension: &str, + ) -> DataResult { + reqwest::blocking::get(&parse_url(lang_extension, "ext"))?.json() } /// The same thing as getting a language by a name, except that it is by @@ -336,8 +358,13 @@ pub mod data { /// Ok(()) /// } /// ``` - pub async fn get_language_by_extension_async(lang_extension: &str) -> DataResult { - Ok(reqwest::get(&parse_url(lang_extension, "ext")).await?.json().await?) + pub async fn get_language_by_extension_async( + lang_extension: &str, + ) -> DataResult { + Ok(reqwest::get(&parse_url(lang_extension, "ext")) + .await? + .json() + .await?) } #[derive(Deserialize)] @@ -372,10 +399,16 @@ pub mod data { fn parse_url(value: &str, req_type: &str) -> String { let parsed_url: String; - if req_type == "name" { parsed_url = format!("{}language?name={}", DATA_ENDPOINT, &value); } - else if req_type == "ext" { parsed_url = format!("{}languageExt?extension={}", DATA_ENDPOINT, &value); } - else { panic!("[pastemyst] Invalid valid: `req_type` provided. Report the developer about this."); } - return parsed_url; + if req_type == "name" { + parsed_url = format!("{}language?name={}", DATA_ENDPOINT, &value); + } else if req_type == "ext" { + parsed_url = format!("{}languageExt?extension={}", DATA_ENDPOINT, &value); + } else { + panic!( + "[pastemyst] Invalid valid: `req_type` provided. Report the developer about this." + ); + } + parsed_url } /// An enum of PasteMyt language constants. @@ -404,7 +437,7 @@ pub mod data { pub const CRYSTAL: &str = "Crystal"; pub const CSS: &str = "CSS"; pub const CQL: &str = "CQL"; - pub const DLANG: &'static str = "D"; + pub const DLANG: &str = "D"; pub const D: &str = "D"; pub const DART: &str = "Dart"; pub const DIFF: &str = "diff"; @@ -555,7 +588,7 @@ pub mod data { #[allow(dead_code, unused_variables)] pub mod user { use serde::Deserialize; - + /// The type provided by the pastemyst lib. It takes /// a type `T` and evalutates to that type and a /// `Result` like so: `Result` where `E` has @@ -603,10 +636,13 @@ pub mod user { defaultLang: str!(""), publicProfile: false, supporterLength: 0, - contributor: false + contributor: false, }; - if user_exists(username)? == false { - print!("[pastemyst] The user '{}' does not exist and an empty object is returned.\n", username); + if !(user_exists(username)?) { + println!( + "[pastemyst] The user '{}' does not exist and an empty object is returned.", + username + ); } else { result = reqwest::blocking::Client::builder() .build()? @@ -656,16 +692,21 @@ pub mod user { defaultLang: str!(""), publicProfile: false, supporterLength: 0, - contributor: false + contributor: false, }; - if user_exists_async(username).await? == false { - print!("[pastemyst] The user '{}' does not exist and an empty object is returned.\n", username); + if !(user_exists_async(username).await?) { + println!( + "[pastemyst] The user '{}' does not exist and an empty object is returned.", + username + ); } else { result = reqwest::Client::builder() .build()? .get(&parse_user(username)) - .send().await? - .json().await?; + .send() + .await? + .json() + .await?; } Ok(result) } @@ -712,8 +753,11 @@ pub mod user { .get(&parse_user_get(username)) .send()?; let mut user_exists: bool = false; - if result.status().as_u16() == 200 { user_exists = true; } - else if result.status().as_u16() == 404 { user_exists = false; } + if result.status().as_u16() == 200 { + user_exists = true; + } else if result.status().as_u16() == 404 { + user_exists = false; + } Ok(user_exists) } @@ -759,17 +803,25 @@ pub mod user { let result = reqwest::Client::builder() .build()? .get(&parse_user_get(username)) - .send().await?; + .send() + .await?; let mut user_exists: bool = false; - if result.status().as_u16() == 200 { user_exists = true; } - else if result.status().as_u16() == 404 { user_exists = false; } + if result.status().as_u16() == 200 { + user_exists = true; + } else if result.status().as_u16() == 404 { + user_exists = false; + } Ok(user_exists) } /// Parses a user `GET` url endpoint. - fn parse_user(username: &str) -> String { return format!("{}{}", USER_ENDPOINT, username) } + fn parse_user(username: &str) -> String { + return format!("{}{}", USER_ENDPOINT, username); + } /// Parses a user exists url endpoint. - fn parse_user_get(username: &str) -> String { return format!("{}{}/exists", USER_ENDPOINT, username) } + fn parse_user_get(username: &str) -> String { + return format!("{}{}/exists", USER_ENDPOINT, username); + } /// The user object that pastemyst provides. /// It has all the public details of a user. @@ -991,7 +1043,7 @@ pub mod paste { .body(serde_json::to_string(&contents).unwrap()) .send() .unwrap(); - Ok(result.json()?) + result.json() } /// Uses the `CreateObject` struct as a parameter for paste @@ -1049,7 +1101,7 @@ pub mod paste { /// send a paste to [pastemyst](https://paste.myst.rs) /// held under your account which you can configure /// to be private/public or not. You also get the - /// authority to delete that paste. This is a + /// authority to delete that paste. This is a /// synchronous method. /// /// ## Examples @@ -1079,7 +1131,7 @@ pub mod paste { .header(reqwest::header::CONTENT_TYPE, content_type) .body(serde_json::to_string(&contents).unwrap()) .send()?; - Ok(result.json()?) + result.json() } /// Uses the `CreateObject` struct and a `&str` authorization @@ -1094,7 +1146,7 @@ pub mod paste { /// /// ```rust /// use pastemyst::paste::*; - /// + /// /// fn main() -> Result<(), reqwest::Error> /*PasteResult<()>*/ { /// let pasties: Vec = vec![ /// PastyObject { @@ -1158,34 +1210,34 @@ pub mod paste { /// pastes as of this version writing this, /// you can only append pastes when editing /// within the site itself as the user. - /// + /// /// ## Examples /// /// ```rust - /// use pastemyst::str; - /// use pastemyst::paste; + /// let pasties = vec![pastemyst::paste::PastyObject { + /// _id: str!("PastyID"), + /// code: String::from("print('Hello World!')"), + /// language: str!(pastemyst::data::language::PYTHON), + /// title: "Pasty Title".to_string(), + /// }]; + /// let edit_object = pastemyst::paste::EditObject { + /// isPrivate: false, + /// isPublic: false, + /// pasties: pasties, + /// tags: str!("Hello, World"), + /// title: str!("My title") + /// }; + /// let paste_result: PasteObject = paste::edit_paste(edit_object, + /// "PasteID", + /// "Your PasteMyst Token. Get it from: https://paste.myst.rs/user/settings", + /// )?; /// - /// fn main() { - /// let pasties = vec![pastemyst::paste::PastyObject { - /// _id: str!("PastyID"), - /// code: String::from("print('Hello World!')"), - /// language: str!(pastemyst::data::language::PYTHON), - /// title: "Pasty Title".to_string(), - /// }]; - /// let edit_object = pastemyst::paste::EditObject { - /// isPrivate: false, - /// isPublic: false, - /// pasties: pasties, - /// tags: str!("Hello, World"), - /// title: str!("My title") - /// }; - /// let paste_result: PasteObject = paste::edit_paste(edit_object, - /// "PasteID", - /// "Your PasteMyst Token. Get it from: https://paste.myst.rs/user/settings", - /// )?; - /// } /// ``` - pub fn edit_paste(edit_info: EditObject, id: &str, auth_token: &str) -> Result { + pub fn edit_paste( + edit_info: EditObject, + id: &str, + auth_token: &str, + ) -> Result { let content_type = reqwest::header::HeaderValue::from_static("application/json"); let result = reqwest::blocking::Client::builder() .build()? @@ -1194,7 +1246,7 @@ pub mod paste { .header(reqwest::header::CONTENT_TYPE, content_type) .body(serde_json::to_string(&edit_info).unwrap()) .send()?; - Ok(result.json()?) + result.json() } /// Sends a request to pastemyst to edit a @@ -1211,7 +1263,7 @@ pub mod paste { /// pastes as of this version writing this, /// you can only append pastes when editing /// within the site itself as the user. - /// + /// /// ## Examples /// /// ```rust @@ -1239,7 +1291,11 @@ pub mod paste { /// ).await?; /// } /// ``` - pub async fn edit_paste_async(edit_info: EditObject, id: &str, auth_token: &str) -> Result { + pub async fn edit_paste_async( + edit_info: EditObject, + id: &str, + auth_token: &str, + ) -> Result { let content_type = reqwest::header::HeaderValue::from_static("application/json"); let result = reqwest::Client::builder() .build()? @@ -1247,7 +1303,8 @@ pub mod paste { .header("Authorization", auth_token) .header(reqwest::header::CONTENT_TYPE, content_type) .body(serde_json::to_string(&edit_info).unwrap()) - .send().await?; + .send() + .await?; Ok(result.json().await?) } @@ -1326,14 +1383,17 @@ pub mod paste { .build()? .delete(&parse_url(&id)) .header("Authorization", auth_token) - .send().await?; + .send() + .await?; Ok(result.status().as_u16()) } /// Parses the url by combining /// the `PASTE_ENDPOINT` with a /// provided id. - fn parse_url(id: &str) -> String { return PASTE_ENDPOINT.to_owned() + &id } + fn parse_url(id: &str) -> String { + PASTE_ENDPOINT.to_owned() + id + } /// The paste object recieved when /// getting a paste. It contains @@ -1393,7 +1453,7 @@ pub mod paste { /// to Rust's nature, so you must provide them. The /// _id field should always be set to `None` though /// if it's not, it is ignored by PasteMyst's API. - /// + /// /// The design choice of the language field not being /// optional was because auto detect isn't perfect /// and you generally should not rely on it especially