Merge pull request #71 from IcanDivideBy0/cursor_from_display

Simplify ID and Cursor conversion
This commit is contained in:
Sunli 2020-05-10 22:09:09 +08:00 committed by GitHub
commit 94663e1a10
7 changed files with 15 additions and 89 deletions

View File

@ -33,7 +33,7 @@ pub trait Type {
/// Returns a `GlobalID` that is unique among all types.
fn global_id(id: ID) -> ID {
base64::encode(format!("{}:{}", Self::type_name(), id)).into()
base64::encode(format!("{}:{}", Self::type_name(), *id)).into()
}
/// Parse `GlobalID`.

View File

@ -122,7 +122,7 @@ pub use error::{
pub use parser::{Pos, Positioned, Value};
pub use query::{IntoQueryBuilder, IntoQueryBuilderOpts, QueryBuilder, QueryResponse};
pub use registry::CacheControl;
pub use scalars::{Any, Json, ToGraphQLID, ID};
pub use scalars::{Any, Json, ID};
pub use schema::Schema;
pub use subscription::{
SimpleBroker, SubscriptionStream, SubscriptionStreams, SubscriptionTransport,
@ -130,7 +130,7 @@ pub use subscription::{
};
pub use types::{
Connection, Cursor, DataSource, EmptyEdgeFields, EmptyMutation, EmptySubscription, PageInfo,
QueryOperation, ToGraphQLCursor, Upload,
QueryOperation, Upload,
};
pub use validation::ValidationMode;

View File

@ -12,12 +12,6 @@ use uuid::Uuid;
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub struct ID(String);
impl std::fmt::Display for ID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Deref for ID {
type Target = String;
@ -32,9 +26,12 @@ impl DerefMut for ID {
}
}
impl From<String> for ID {
fn from(value: String) -> Self {
ID(value)
impl<T> From<T> for ID
where
T: std::fmt::Display,
{
fn from(value: T) -> Self {
ID(value.to_string())
}
}
@ -44,18 +41,6 @@ impl Into<String> for ID {
}
}
impl<'a> From<&'a str> for ID {
fn from(value: &'a str) -> Self {
ID(value.to_string())
}
}
impl From<usize> for ID {
fn from(value: usize) -> Self {
ID(value.to_string())
}
}
impl TryInto<usize> for ID {
type Error = ParseIntError;
@ -64,12 +49,6 @@ impl TryInto<usize> for ID {
}
}
impl From<Uuid> for ID {
fn from(uuid: Uuid) -> ID {
ID(uuid.to_string())
}
}
impl TryInto<Uuid> for ID {
type Error = uuid::Error;
@ -78,12 +57,6 @@ impl TryInto<Uuid> for ID {
}
}
impl From<ObjectId> for ID {
fn from(object_id: ObjectId) -> ID {
ID(object_id.to_hex())
}
}
impl TryInto<ObjectId> for ID {
type Error = oid::Error;
@ -98,21 +71,6 @@ impl PartialEq<&str> for ID {
}
}
/// Convert any type that implements Display to the ID type
pub trait ToGraphQLID {
#[allow(missing_docs)]
fn to_graphql_id(&self) -> ID;
}
impl<T> ToGraphQLID for T
where
T: std::fmt::Display,
{
fn to_graphql_id(&self) -> ID {
ID(self.to_string())
}
}
#[Scalar(internal)]
impl ScalarType for ID {
fn type_name() -> &'static str {

View File

@ -15,7 +15,7 @@ mod bson;
mod uuid;
pub use any::Any;
pub use id::{ToGraphQLID, ID};
pub use id::ID;
pub use json::Json;
#[cfg(test)]

View File

@ -9,12 +9,6 @@ use std::ops::{Deref, DerefMut};
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub struct Cursor(String);
impl std::fmt::Display for Cursor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Deref for Cursor {
type Target = String;
@ -29,36 +23,12 @@ impl DerefMut for Cursor {
}
}
impl From<String> for Cursor {
fn from(value: String) -> Self {
Cursor(value)
}
}
impl<'a> From<&'a str> for Cursor {
fn from(value: &'a str) -> Self {
Cursor(value.to_string())
}
}
impl From<usize> for Cursor {
fn from(value: usize) -> Self {
Cursor(value.to_string())
}
}
/// Convert any type that implements Display to the Cursor type
pub trait ToGraphQLCursor {
#[allow(missing_docs)]
fn to_graphql_cursor(&self) -> Cursor;
}
impl<T> ToGraphQLCursor for T
impl<T> From<T> for Cursor
where
T: std::fmt::Display,
{
fn to_graphql_cursor(&self) -> Cursor {
Cursor(self.to_string())
fn from(value: T) -> Self {
Cursor(value.to_string())
}
}

View File

@ -7,7 +7,7 @@ mod slice;
use crate::{Context, FieldResult, ObjectType};
pub use connection_type::Connection;
pub use cursor::{Cursor, ToGraphQLCursor};
pub use cursor::Cursor;
pub use page_info::PageInfo;
/// Connection query operation

View File

@ -7,9 +7,7 @@ mod optional;
mod query_root;
mod upload;
pub use connection::{
Connection, Cursor, DataSource, EmptyEdgeFields, PageInfo, QueryOperation, ToGraphQLCursor,
};
pub use connection::{Connection, Cursor, DataSource, EmptyEdgeFields, PageInfo, QueryOperation};
pub use empty_mutation::EmptyMutation;
pub use empty_subscription::EmptySubscription;
pub use query_root::QueryRoot;