convert all to sync, remove now broken doc comments, some renames

This commit is contained in:
2021-08-21 15:19:22 +02:00
parent c967e59ffb
commit 568b1ff07a
59 changed files with 504 additions and 4737 deletions
+2 -2
View File
@@ -1,10 +1,10 @@
use std::io::{self, BufRead, Write};
use crate::{errors::Result, registration::Registered, Mastodon};
use crate::{errors::Result, registration::Registered, Client};
/// Finishes the authentication process for the given `Registered` object,
/// using the command-line
pub async fn authenticate(registration: Registered) -> Result<Mastodon> {
pub async fn authenticate(registration: Registered) -> Result<Client> {
let url = registration.authorize_url()?;
let stdout = io::stdout();
-78
View File
@@ -1,78 +0,0 @@
use crate::{data::Data, Result};
/// Attempts to deserialize a Data struct from the environment
pub fn from_env() -> Result<Data> {
Ok(envy::from_env()?)
}
/// Attempts to deserialize a Data struct from the environment. All keys are
/// prefixed with the given prefix
pub fn from_env_prefixed(prefix: &str) -> Result<Data> {
Ok(envy::prefixed(prefix).from_env()?)
}
#[cfg(test)]
mod tests {
use super::*;
use std::{
env,
ops::FnOnce,
panic::{catch_unwind, UnwindSafe},
};
fn withenv<F: FnOnce() -> R + UnwindSafe, R>(prefix: Option<&'static str>, test: F) -> R {
env::set_var(makekey(prefix, "BASE"), "https://example.com");
env::set_var(makekey(prefix, "CLIENT_ID"), "adbc01234");
env::set_var(makekey(prefix, "CLIENT_SECRET"), "0987dcba");
env::set_var(makekey(prefix, "REDIRECT"), "urn:ietf:wg:oauth:2.0:oob");
env::set_var(makekey(prefix, "TOKEN"), "fedc5678");
let result = catch_unwind(test);
env::remove_var(makekey(prefix, "BASE"));
env::remove_var(makekey(prefix, "CLIENT_ID"));
env::remove_var(makekey(prefix, "CLIENT_SECRET"));
env::remove_var(makekey(prefix, "REDIRECT"));
env::remove_var(makekey(prefix, "TOKEN"));
fn makekey(prefix: Option<&'static str>, key: &str) -> String {
if let Some(prefix) = prefix {
format!("{}{}", prefix, key)
} else {
key.to_string()
}
}
result.expect("failed")
}
#[test]
fn test_from_env_no_prefix() {
let desered = withenv(None, || from_env()).expect("Couldn't deser");
assert_eq!(
desered,
Data {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
redirect: "urn:ietf:wg:oauth:2.0:oob".into(),
token: "fedc5678".into(),
}
);
}
#[test]
fn test_from_env_prefixed() {
let desered = withenv(Some("APP_"), || from_env_prefixed("APP_")).expect("Couldn't deser");
assert_eq!(
desered,
Data {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
redirect: "urn:ietf:wg:oauth:2.0:oob".into(),
token: "fedc5678".into(),
}
);
}
}
+19 -19
View File
@@ -4,46 +4,46 @@ use std::{
path::Path,
};
use crate::{data::Data, Result};
use crate::{data::ClientData, Result};
/// Attempts to deserialize a Data struct from a string
pub fn from_str(s: &str) -> Result<Data> {
pub fn from_str(s: &str) -> Result<ClientData> {
Ok(serde_json::from_str(s)?)
}
/// Attempts to deserialize a Data struct from a slice of bytes
pub fn from_slice(s: &[u8]) -> Result<Data> {
pub fn from_slice(s: &[u8]) -> Result<ClientData> {
Ok(serde_json::from_slice(s)?)
}
/// Attempts to deserialize a Data struct from something that implements
/// the std::io::Read trait
pub fn from_reader<R: Read>(mut r: R) -> Result<Data> {
pub fn from_reader<R: Read>(mut r: R) -> Result<ClientData> {
let mut buffer = Vec::new();
r.read_to_end(&mut buffer)?;
from_slice(&buffer)
}
/// Attempts to deserialize a Data struct from a file
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Data> {
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<ClientData> {
let path = path.as_ref();
let file = File::open(path)?;
Ok(from_reader(file)?)
}
/// Attempts to serialize a Data struct to a String
pub fn to_string(data: &Data) -> Result<String> {
pub fn to_string(data: &ClientData) -> Result<String> {
Ok(serde_json::to_string_pretty(data)?)
}
/// Attempts to serialize a Data struct to a Vec of bytes
pub fn to_vec(data: &Data) -> Result<Vec<u8>> {
pub fn to_vec(data: &ClientData) -> Result<Vec<u8>> {
Ok(serde_json::to_vec(data)?)
}
/// Attempts to serialize a Data struct to something that implements the
/// std::io::Write trait
pub fn to_writer<W: Write>(data: &Data, writer: W) -> Result<()> {
pub fn to_writer<W: Write>(data: &ClientData, writer: W) -> Result<()> {
let mut buf_writer = BufWriter::new(writer);
let vec = to_vec(data)?;
buf_writer.write_all(&vec)?;
@@ -55,7 +55,7 @@ pub fn to_writer<W: Write>(data: &Data, writer: W) -> Result<()> {
/// When opening the file, this will set the `.write(true)` and
/// `.truncate(true)` options, use the next method for more
/// fine-grained control
pub fn to_file<P: AsRef<Path>>(data: &Data, path: P) -> Result<()> {
pub fn to_file<P: AsRef<Path>>(data: &ClientData, path: P) -> Result<()> {
let mut options = OpenOptions::new();
options.create(true).write(true).truncate(true);
to_file_with_options(data, path, options)?;
@@ -63,7 +63,7 @@ pub fn to_file<P: AsRef<Path>>(data: &Data, path: P) -> Result<()> {
}
/// Attempts to serialize a Data struct to a file
pub fn to_file_with_options<P: AsRef<Path>>(data: &Data, path: P, options: OpenOptions) -> Result<()> {
pub fn to_file_with_options<P: AsRef<Path>>(data: &ClientData, path: P, options: OpenOptions) -> Result<()> {
let path = path.as_ref();
let file = options.open(path)?;
to_writer(data, file)?;
@@ -93,7 +93,7 @@ mod tests {
let desered = from_str(DOC).expect("Couldn't deserialize Data");
assert_eq!(
desered,
Data {
ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -108,7 +108,7 @@ mod tests {
let desered = from_slice(&doc).expect("Couldn't deserialize Data");
assert_eq!(
desered,
Data {
ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -124,7 +124,7 @@ mod tests {
let desered = from_reader(doc).expect("Couldn't deserialize Data");
assert_eq!(
desered,
Data {
ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -140,7 +140,7 @@ mod tests {
let desered = from_file(datafile.path()).expect("Couldn't deserialize Data");
assert_eq!(
desered,
Data {
ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -151,7 +151,7 @@ mod tests {
}
#[test]
fn test_to_string() {
let data = Data {
let data = ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -164,7 +164,7 @@ mod tests {
}
#[test]
fn test_to_vec() {
let data = Data {
let data = ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -177,7 +177,7 @@ mod tests {
}
#[test]
fn test_to_writer() {
let data = Data {
let data = ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -192,7 +192,7 @@ mod tests {
}
#[test]
fn test_to_file() {
let data = Data {
let data = ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -207,7 +207,7 @@ mod tests {
}
#[test]
fn test_to_file_with_options() {
let data = Data {
let data = ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
+23 -14
View File
@@ -1,4 +1,3 @@
#[cfg(feature = "toml")]
/// Helpers for serializing to/deserializing from toml
///
/// In order to use this module, set the "toml" feature in your Cargo.toml:
@@ -10,7 +9,6 @@
/// ```
pub mod toml;
#[cfg(feature = "json")]
/// Helpers for serializing to/deserializing from json
///
/// In order to use this module, set the "json" feature in your Cargo.toml:
@@ -22,17 +20,28 @@ pub mod toml;
/// ```
pub mod json;
#[cfg(feature = "env")]
/// Helpers for deserializing a `Data` struct from the environment
///
/// In order to use this module, set the "env" feature in your Cargo.toml:
///
/// ```toml,ignore
/// [dependencies.elefren]
/// version = "0.22"
/// features = ["env"]
/// ```
pub mod env;
/// Helpers for working with the command line
pub mod cli;
/// # Low-level API for extending
// Convert the HTTP response body from JSON. Pass up deserialization errors
// transparently.
pub async fn deserialise_response<T: for<'de> serde::Deserialize<'de>>(response: reqwest::Response) -> crate::Result<T> {
let bytes = response.bytes().await?;
match serde_json::from_slice(&bytes) {
Ok(t) => {
debug!("{}", String::from_utf8_lossy(&bytes));
Ok(t)
}
// If deserializing into the desired type fails try again to
// see if this is an error response.
Err(e) => {
error!("{}", String::from_utf8_lossy(&bytes));
if let Ok(error) = serde_json::from_slice(&bytes) {
return Err(crate::Error::Api(error));
}
Err(e.into())
}
}
}
+23 -25
View File
@@ -1,49 +1,47 @@
use std::{
fs::{File, OpenOptions},
io::{BufWriter, Read, Write},
path::Path,
};
use crate::{data::Data, Result};
use crate::data::ClientData;
use crate::Result;
use std::io::{Read, Write, BufWriter};
use std::path::Path;
use std::fs::{File, OpenOptions};
/// Attempts to deserialize a Data struct from a string
pub fn from_str(s: &str) -> Result<Data> {
pub fn from_str(s: &str) -> Result<ClientData> {
Ok(toml::from_str(s)?)
}
/// Attempts to deserialize a Data struct from a slice of bytes
pub fn from_slice(s: &[u8]) -> Result<Data> {
pub fn from_slice(s: &[u8]) -> Result<ClientData> {
Ok(toml::from_slice(s)?)
}
/// Attempts to deserialize a Data struct from something that implements
/// the std::io::Read trait
pub fn from_reader<R: Read>(mut r: R) -> Result<Data> {
pub fn from_reader<R: Read>(mut r: R) -> Result<ClientData> {
let mut buffer = Vec::new();
r.read_to_end(&mut buffer)?;
from_slice(&buffer)
}
/// Attempts to deserialize a Data struct from a file
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Data> {
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<ClientData> {
let path = path.as_ref();
let file = File::open(path)?;
Ok(from_reader(file)?)
}
/// Attempts to serialize a Data struct to a String
pub fn to_string(data: &Data) -> Result<String> {
pub fn to_string(data: &ClientData) -> Result<String> {
Ok(toml::to_string_pretty(data)?)
}
/// Attempts to serialize a Data struct to a Vec of bytes
pub fn to_vec(data: &Data) -> Result<Vec<u8>> {
pub fn to_vec(data: &ClientData) -> Result<Vec<u8>> {
Ok(toml::to_vec(data)?)
}
/// Attempts to serialize a Data struct to something that implements the
/// std::io::Write trait
pub fn to_writer<W: Write>(data: &Data, writer: W) -> Result<()> {
pub fn to_writer<W: Write>(data: &ClientData, writer: W) -> Result<()> {
let mut buf_writer = BufWriter::new(writer);
let vec = to_vec(data)?;
buf_writer.write_all(&vec)?;
@@ -55,7 +53,7 @@ pub fn to_writer<W: Write>(data: &Data, writer: W) -> Result<()> {
/// When opening the file, this will set the `.write(true)` and
/// `.truncate(true)` options, use the next method for more
/// fine-grained control
pub fn to_file<P: AsRef<Path>>(data: &Data, path: P) -> Result<()> {
pub fn to_file<P: AsRef<Path>>(data: &ClientData, path: P) -> Result<()> {
let mut options = OpenOptions::new();
options.create(true).write(true).truncate(true);
to_file_with_options(data, path, options)?;
@@ -63,7 +61,7 @@ pub fn to_file<P: AsRef<Path>>(data: &Data, path: P) -> Result<()> {
}
/// Attempts to serialize a Data struct to a file
pub fn to_file_with_options<P: AsRef<Path>>(data: &Data, path: P, options: OpenOptions) -> Result<()> {
pub fn to_file_with_options<P: AsRef<Path>>(data: &ClientData, path: P, options: OpenOptions) -> Result<()> {
let path = path.as_ref();
let file = options.open(path)?;
to_writer(data, file)?;
@@ -91,7 +89,7 @@ mod tests {
let desered = from_str(DOC).expect("Couldn't deserialize Data");
assert_eq!(
desered,
Data {
ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -106,7 +104,7 @@ mod tests {
let desered = from_slice(&doc).expect("Couldn't deserialize Data");
assert_eq!(
desered,
Data {
ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -122,7 +120,7 @@ mod tests {
let desered = from_reader(doc).expect("Couldn't deserialize Data");
assert_eq!(
desered,
Data {
ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -138,7 +136,7 @@ mod tests {
let desered = from_file(datafile.path()).expect("Couldn't deserialize Data");
assert_eq!(
desered,
Data {
ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -149,7 +147,7 @@ mod tests {
}
#[test]
fn test_to_string() {
let data = Data {
let data = ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -162,7 +160,7 @@ mod tests {
}
#[test]
fn test_to_vec() {
let data = Data {
let data = ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -175,7 +173,7 @@ mod tests {
}
#[test]
fn test_to_writer() {
let data = Data {
let data = ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -190,7 +188,7 @@ mod tests {
}
#[test]
fn test_to_file() {
let data = Data {
let data = ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),
@@ -205,7 +203,7 @@ mod tests {
}
#[test]
fn test_to_file_with_options() {
let data = Data {
let data = ClientData {
base: "https://example.com".into(),
client_id: "adbc01234".into(),
client_secret: "0987dcba".into(),