#![warn(missing_docs)]
pub mod common;
pub mod tmf;
use common::tmf_error::TMFError;
#[cfg(feature = "tmf620")]
use tmf::tmf620::TMF620;
#[cfg(feature = "tmf622")]
use tmf::tmf622::TMF622;
#[cfg(feature = "tmf629")]
use tmf::tmf629::TMF629;
#[cfg(feature = "tmf632")]
use tmf::tmf632::TMF632;
#[cfg(feature = "tmf633")]
use tmf::tmf633::TMF633;
#[cfg(feature = "tmf637")]
use tmf::tmf637::TMF637;
#[cfg(feature = "tmf638")]
use tmf::tmf638::TMF638;
#[cfg(feature = "tmf639")]
use tmf::tmf639::TMF639;
#[cfg(feature = "tmf645")]
use tmf::tmf645::TMF645;
#[cfg(feature = "tmf648")]
use tmf::tmf648::TMF648;
#[cfg(feature = "tmf663")]
use tmf::tmf663::TMF663;
#[cfg(feature = "tmf674")]
use tmf::tmf674::TMF674;
use tmflib::{HasId, Uri};
#[cfg(feature = "insecure")]
const INSECURE: bool = true;
#[cfg(not(feature = "insecure"))]
const INSECURE: bool = false;
pub const DEFAULT_PORT: u16 = 8001;
#[derive(Clone, Debug, Default)]
pub struct Config {
pub host: Uri,
pub port: u16,
pub insecure: bool,
}
impl Config {
pub fn new(host: impl Into<String>, port: u16) -> Config {
Config {
host: Uri::from(host.into()),
port,
insecure: INSECURE,
}
}
}
#[derive(Clone, Default, Debug)]
pub struct QueryOptions {
pub fields: Option<String>,
pub limit: Option<u16>,
pub offset: Option<u16>,
pub name: Option<String>,
}
impl QueryOptions {
pub fn fields(mut self, fields: String) -> QueryOptions {
self.fields = Some(fields);
self
}
pub fn limit(mut self, limit: u16) -> QueryOptions {
self.limit = Some(limit);
self
}
pub fn offset(mut self, offset: u16) -> QueryOptions {
self.offset = Some(offset);
self
}
pub fn name(mut self, name: impl Into<String>) -> QueryOptions {
self.name = Some(name.into());
self
}
}
impl From<QueryOptions> for String {
fn from(val: QueryOptions) -> Self {
let limit = match val.limit {
Some(l) => format!("limit={l}"),
None => String::default(),
};
let offset = match val.offset {
Some(o) => format!("offset={o}"),
None => String::default(),
};
let name = match val.name {
Some(n) => format!("name={n}"),
None => String::default(),
};
format!("{limit}&{offset}&{name}")
}
}
#[cfg(feature = "blocking")]
pub trait BlockingOperations {
type TMF: HasId;
fn get(&self, id: impl Into<String>) -> Result<Vec<Self::TMF>, TMFError>;
fn list(&self, filter: Option<QueryOptions>) -> Result<Vec<Self::TMF>, TMFError>;
fn create(&self, item: Self::TMF) -> Result<Self::TMF, TMFError>;
fn update(&self, id: impl Into<String>, patch: Self::TMF) -> Result<Self::TMF, TMFError>;
fn delete(&self, id: impl Into<String>) -> Result<Self::TMF, TMFError>;
}
#[cfg(not(feature = "blocking"))]
pub trait AsyncOperations {
type TMF: HasId;
fn get(
&self,
id: impl Into<String>,
) -> impl std::future::Future<Output = Result<Vec<Self::TMF>, TMFError>>;
fn list(
&self,
filter: Option<QueryOptions>,
) -> impl std::future::Future<Output = Result<Vec<Self::TMF>, TMFError>>;
fn create(
&self,
item: Self::TMF,
) -> impl std::future::Future<Output = Result<Self::TMF, TMFError>>;
fn update(
&self,
id: impl Into<String>,
patch: Self::TMF,
) -> impl std::future::Future<Output = Result<Self::TMF, TMFError>>;
fn delete(
&self,
id: impl Into<String>,
) -> impl std::future::Future<Output = Result<Self::TMF, TMFError>>;
}
#[allow(clippy::new_ret_no_self)]
pub trait HasNew<T: Clone> {
fn new(config: Config) -> T;
}
pub struct TMFClient {
config: Config,
#[cfg(feature = "tmf620")]
tmf620: Option<TMF620>,
#[cfg(feature = "tmf622")]
tmf622: Option<TMF622>,
#[cfg(feature = "tmf629")]
tmf629: Option<TMF629>,
#[cfg(feature = "tmf632")]
tmf632: Option<TMF632>,
#[cfg(feature = "tmf633")]
tmf633: Option<TMF633>,
#[cfg(feature = "tmf637")]
tmf637: Option<TMF637>,
#[cfg(feature = "tmf638")]
tmf638: Option<TMF638>,
#[cfg(feature = "tmf639")]
tmf639: Option<TMF639>,
#[cfg(feature = "tmf645")]
tmf645: Option<TMF645>,
#[cfg(feature = "tmf648")]
tmf648: Option<TMF648>,
#[cfg(feature = "tmf663")]
tmf663: Option<TMF663>,
#[cfg(feature = "tmf674")]
tmf674: Option<TMF674>,
}
fn instantiate<T: Clone + HasNew<T>>(api: &mut Option<T>, config: Config) -> T {
match api {
Some(instance) => instance.clone(),
None => {
let new_api = T::new(config);
api.replace(new_api.clone());
new_api
}
}
}
impl TMFClient {
pub fn new(host: impl Into<String>, port: Option<u16>) -> TMFClient {
TMFClient {
config: Config::new(host, port.unwrap_or(DEFAULT_PORT)),
#[cfg(feature = "tmf620")]
tmf620: None,
#[cfg(feature = "tmf622")]
tmf622: None,
#[cfg(feature = "tmf629")]
tmf629: None,
#[cfg(feature = "tmf632")]
tmf632: None,
#[cfg(feature = "tmf633")]
tmf633: None,
#[cfg(feature = "tmf637")]
tmf637: None,
#[cfg(feature = "tmf638")]
tmf638: None,
#[cfg(feature = "tmf639")]
tmf639: None,
#[cfg(feature = "tmf645")]
tmf645: None,
#[cfg(feature = "tmf648")]
tmf648: None,
#[cfg(feature = "tmf663")]
tmf663: None,
#[cfg(feature = "tmf674")]
tmf674: None,
}
}
#[cfg(feature = "tmf620")]
pub fn tmf620(&mut self) -> TMF620 {
let config = self.config.clone();
let instance: TMF620 = instantiate(&mut self.tmf620, config);
self.tmf620.replace(instance.clone());
instance
}
#[cfg(feature = "tmf622")]
pub fn tmf622(&mut self) -> TMF622 {
instantiate(&mut self.tmf622, self.config.clone())
}
#[cfg(feature = "tmf629")]
pub fn tmf629(&mut self) -> TMF629 {
instantiate(&mut self.tmf629, self.config.clone())
}
#[cfg(feature = "tmf632")]
pub fn tmf632(&mut self) -> TMF632 {
instantiate(&mut self.tmf632, self.config.clone())
}
#[cfg(feature = "tmf633")]
pub fn tmf633(&mut self) -> TMF633 {
instantiate(&mut self.tmf633, self.config.clone())
}
#[cfg(feature = "tmf637")]
pub fn tmf637(&mut self) -> TMF637 {
instantiate(&mut self.tmf637, self.config.clone())
}
#[cfg(feature = "tmf638")]
pub fn tmf638(&mut self) -> TMF638 {
instantiate(&mut self.tmf638, self.config.clone())
}
#[cfg(feature = "tmf639")]
pub fn tmf639(&mut self) -> TMF639 {
instantiate(&mut self.tmf639, self.config.clone())
}
#[cfg(feature = "tmf645")]
pub fn tmf645(&mut self) -> TMF645 {
instantiate(&mut self.tmf645, self.config.clone())
}
#[cfg(feature = "tmf648")]
pub fn tmf648(&mut self) -> TMF648 {
instantiate(&mut self.tmf648, self.config.clone())
}
#[cfg(feature = "tmf663")]
pub fn tmf663(&mut self) -> TMF663 {
instantiate(&mut self.tmf663, self.config.clone())
}
#[cfg(feature = "tmf674")]
pub fn tmf674(&mut self) -> TMF674 {
instantiate(&mut self.tmf674, self.config.clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filter_limit() {
let filter = QueryOptions::default().limit(111);
assert_eq!(filter.limit, Some(111));
}
#[test]
fn test_filter_offset() {
let filter = QueryOptions::default().offset(222);
assert_eq!(filter.offset, Some(222));
}
}