use std::rc::Rc;
use reqwest::blocking::Client;
use serde::Serialize;
use crate::types::{Account, TelegraphResult};
use crate::error::TelegraphError;
#[derive(Default)]
pub struct NoShortName;
#[derive(Serialize)]
pub struct ShortName(String);
#[derive(Default, Serialize)]
pub struct CreateAccount<N> {
#[serde(skip)]
client: Rc<Client>,
#[serde(skip)]
method_name: Rc<String>,
short_name: N,
#[serde(skip_serializing_if = "Option::is_none")]
author_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
author_url: Option<String>
}
impl CreateAccount<NoShortName> {
pub(crate) fn new(client: Rc<Client>, method_name: Rc<String>) -> CreateAccount<NoShortName> {
Self { client, method_name, ..Self::default() }
}
}
impl<N> CreateAccount<N> {
pub fn short_name(self, short_name: &str) -> CreateAccount<ShortName> {
CreateAccount {
client: self.client,
method_name: self.method_name,
short_name: ShortName(short_name.into()),
author_name: self.author_name,
author_url: self.author_url
}
}
pub fn author_name(mut self, author_name: &str) -> Self {
self.author_name = Some(author_name.into());
self
}
pub fn author_url(mut self, author_url: &str) -> Self {
self.author_url = Some(author_url.into());
self
}
}
impl CreateAccount<ShortName> {
pub fn send(self) -> Result<Account, TelegraphError> {
let req = self.client.post(self.method_name.as_str()).form(&self).send()?;
let json: TelegraphResult<Account> = req.json()?;
if !json.ok {
Err(json.error.unwrap())
} else {
Ok(json.result.unwrap())
}
}
}