Skip to content

Commit

Permalink
Update documentation. #6
Browse files Browse the repository at this point in the history
  • Loading branch information
gudaoxuri committed Mar 21, 2022
1 parent 311b29a commit 1692dce
Show file tree
Hide file tree
Showing 5 changed files with 586 additions and 15 deletions.
16 changes: 8 additions & 8 deletions src/basic/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ use crate::basic::error::TardisError;
use crate::basic::result::TardisResult;
use crate::TardisFuns;

/// Base64 handle / Base64处理
///
/// # Examples
/// ```rust
/// use tardis::TardisFuns;
/// let b64_str = TardisFuns::crypto.base64.encode("测试");
/// let str = TardisFuns::crypto.base64.decode(&b64_str).unwrap();
/// ```
pub struct TardisCrypto {
pub base64: TardisCryptoBase64,
pub aes: TardisCryptoAes,
Expand Down Expand Up @@ -40,14 +48,6 @@ pub struct TardisCryptoSm2PublicKey {
pub struct TardisCryptoDigest;
pub struct TardisCryptoKey;

/// Base64 handle / Base64处理
///
/// # Examples
/// ```rust
/// use tardis::TardisFuns;
/// let b64_str = TardisFuns::crypto.base64.encode("测试");
/// let str = TardisFuns::crypto.base64.decode(&b64_str).unwrap();
/// ```
impl TardisCryptoBase64 {
pub fn decode(&self, data: &str) -> TardisResult<String> {
match base64::decode(data) {
Expand Down
27 changes: 22 additions & 5 deletions src/basic/field.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
//! Field handle / 字段处理
//!
//! Provides some common regular, Id generation and other functions.
//!
//! 提供了一些常用的正则判断、Id生成等功能.
use std::fmt::{Display, Formatter};

use regex::Regex;
Expand All @@ -21,6 +16,28 @@ static BASE36: &str = "abcdefghijklmnopqrstuvwxyz0123456789";

pub static GENERAL_SPLIT: &str = "##";

/// Field handle / 字段处理
///
/// Provides some common regular, Id generation and other functions.
///
/// 提供了一些常用的正则判断、Id生成等功能.
/// # Examples
/// ```rust
/// use tardis::TardisFuns;
/// assert!(TardisFuns::field.is_phone("18657120202"));
/// assert_eq!(TardisFuns::field.incr_by_base62("abcd1").unwrap(), "abcd2");
/// assert!(TardisFuns::field.incr_by_base62("999").is_none());
/// assert_eq!(TardisFuns::field.incr_by_base36("abcd1").unwrap(), "abcd2");
/// assert!(TardisFuns::field.incr_by_base36("999").is_none());
/// assert!(TardisFuns::field.is_code_cs("Adw834_dfds"));
/// assert!(!TardisFuns::field.is_code_cs(" Adw834_dfds"));
/// assert!(!TardisFuns::field.is_code_cs("Adw834_d-fds"));
/// assert!(TardisFuns::field.is_code_ncs("adon2_43323tr"));
/// assert!(!TardisFuns::field.is_code_ncs("adon2_43323tr "));
/// assert!(!TardisFuns::field.is_code_ncs("Adw834_dfds"));
/// assert_eq!(TardisFuns::field.nanoid().len(), 21);
/// assert_eq!(TardisFuns::field.nanoid_len(4).len(), 4);
/// ```
pub struct TardisField;

impl TardisField {
Expand Down
35 changes: 34 additions & 1 deletion src/basic/json.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
//! Json handle / Json处理
use crate::basic::error::TardisError;
use crate::basic::result::TardisResult;
use crate::serde::de::DeserializeOwned;
use crate::serde::{Deserialize, Serialize};
use crate::serde_json::Value;

/// Json handle / Json处理
///
/// # Examples
/// ```rust
/// use tardis::TardisFuns;
/// let test_config = TestConfig {
/// project_name: "测试".to_string(),
/// level_num: 0,
/// db_proj: DatabaseConfig { url: "http://xxx".to_string() },
/// };
///
/// let json_str = TardisFuns::json.obj_to_string(&test_config)?;
/// assert_eq!(json_str, r#"{"project_name":"测试","level_num":0,"db_proj":{"url":"http://xxx"}}"#);
///
/// let json_obj = TardisFuns::json.str_to_obj::<TestConfig<DatabaseConfig>>(&json_str)?;
/// assert_eq!(json_obj.project_name, "测试");
/// assert_eq!(json_obj.level_num, 0);
/// assert_eq!(json_obj.db_proj.url, "http://xxx");
///
/// let json_value = TardisFuns::json.str_to_json(&json_str)?;
/// assert_eq!(json_value["project_name"], "测试");
/// assert_eq!(json_value["level_num"], 0);
/// assert_eq!(json_value["db_proj"]["url"], "http://xxx");
///
/// let json_value = TardisFuns::json.obj_to_json(&json_obj)?;
/// assert_eq!(json_value["project_name"], "测试");
/// assert_eq!(json_value["level_num"], 0);
/// assert_eq!(json_value["db_proj"]["url"], "http://xxx");
///
/// let json_obj = TardisFuns::json.json_to_obj::<TestConfig<DatabaseConfig>>(json_value)?;
/// assert_eq!(json_obj.project_name, "测试");
/// assert_eq!(json_obj.level_num, 0);
/// assert_eq!(json_obj.db_proj.url, "http://xxx");
/// ```
pub struct TardisJson;

impl TardisJson {
Expand Down
10 changes: 9 additions & 1 deletion src/basic/uri.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
//! Uri handle / Uri处理
use crate::basic::result::TardisResult;

/// Uri handle / Uri处理
///
/// # Examples
/// ```rust
/// use tardis::TardisFuns;
/// assert_eq!(TardisFuns::uri.format("http://idealwrold.group").unwrap(), "http://idealwrold.group");
/// assert_eq!(TardisFuns::uri.format("jdbc:h2:men:iam").unwrap(), "jdbc:h2:men:iam");
/// assert_eq!(TardisFuns::uri.format("api://a1.t1/e1?q2=2&q1=1&q3=3").unwrap(), "api://a1.t1/e1?q1=1&q2=2&q3=3");
/// ```
pub struct TardisUri;

impl TardisUri {
Expand Down
Loading

0 comments on commit 1692dce

Please sign in to comment.