Merge pull request #72 from IcanDivideBy0/more_conversion_goodies

Prefer TryFrom to TryInto for ID
This commit is contained in:
Samuel Hurel 2020-05-10 16:44:13 +02:00 committed by GitHub
commit 8a5809e7df
2 changed files with 17 additions and 11 deletions

View File

@ -1,7 +1,7 @@
use crate::{InputValueError, InputValueResult, Result, ScalarType, Value}; use crate::{InputValueError, InputValueResult, Result, ScalarType, Value};
use async_graphql_derive::Scalar; use async_graphql_derive::Scalar;
use bson::oid::{self, ObjectId}; use bson::oid::{self, ObjectId};
use std::convert::TryInto; use std::convert::TryFrom;
use std::num::ParseIntError; use std::num::ParseIntError;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use uuid::Uuid; use uuid::Uuid;
@ -41,27 +41,27 @@ impl Into<String> for ID {
} }
} }
impl TryInto<usize> for ID { impl TryFrom<ID> for usize {
type Error = ParseIntError; type Error = ParseIntError;
fn try_into(self) -> std::result::Result<usize, Self::Error> { fn try_from(id: ID) -> std::result::Result<Self, Self::Error> {
self.0.parse() id.0.parse()
} }
} }
impl TryInto<Uuid> for ID { impl TryFrom<ID> for Uuid {
type Error = uuid::Error; type Error = uuid::Error;
fn try_into(self) -> std::result::Result<Uuid, Self::Error> { fn try_from(id: ID) -> std::result::Result<Self, Self::Error> {
Uuid::parse_str(&self.0) Uuid::parse_str(&id.0)
} }
} }
impl TryInto<ObjectId> for ID { impl TryFrom<ID> for ObjectId {
type Error = oid::Error; type Error = oid::Error;
fn try_into(self) -> std::result::Result<ObjectId, oid::Error> { fn try_from(id: ID) -> std::result::Result<Self, oid::Error> {
ObjectId::with_string(&self.0) ObjectId::with_string(&id.0)
} }
} }

View File

@ -1,4 +1,4 @@
use crate::{InputValueError, InputValueResult, Result, ScalarType, Value}; use crate::{InputValueError, InputValueResult, Result, ScalarType, Value, ID};
use async_graphql_derive::Scalar; use async_graphql_derive::Scalar;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
@ -32,6 +32,12 @@ where
} }
} }
impl From<ID> for Cursor {
fn from(id: ID) -> Self {
Cursor(id.into())
}
}
#[Scalar(internal)] #[Scalar(internal)]
impl ScalarType for Cursor { impl ScalarType for Cursor {
fn type_name() -> &'static str { fn type_name() -> &'static str {