Prefer TryFrom to TryInto for ID
This commit is contained in:
parent
e3542a1360
commit
0344b0b996
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user