Rename entity Error to ErrorObject

This commit is contained in:
Alex Kotov 2022-08-03 10:55:48 +04:00
parent a91533e2e0
commit 3f3954d071
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
7 changed files with 44 additions and 45 deletions

View File

@ -6,7 +6,7 @@ pub struct DocumentBuilder {
meta: Option<MetaOrAttrsBuilder>,
links: Option<LinksBuilder>,
data: Option<DataBuilder>,
errors: Option<Vec<ErrorBuilder>>,
errors: Option<Vec<ErrorObjectBuilder>>,
}
impl Builder<'_> for DocumentBuilder {
@ -18,8 +18,8 @@ impl Builder<'_> for DocumentBuilder {
Some(errors) => {
let mut new_errors = Vec::new();
for error in errors {
new_errors.push(error.finish()?);
for error_object in errors {
new_errors.push(error_object.finish()?);
}
Some(new_errors)
@ -77,11 +77,11 @@ impl DocumentBuilder {
}
}
pub fn errors<E: Into<ErrorBuilder>>(self, errors: Vec<E>) -> Self {
pub fn errors<E: Into<ErrorObjectBuilder>>(self, errors: Vec<E>) -> Self {
let mut new_errors = Vec::new();
for error in errors {
new_errors.push(error.into());
for error_object in errors {
new_errors.push(error_object.into());
}
Self {
@ -112,9 +112,9 @@ impl DocumentBuilder {
}
}
pub fn error<E: Into<ErrorBuilder>>(self, error: E) -> Self {
pub fn error<E: Into<ErrorObjectBuilder>>(self, error_object: E) -> Self {
let mut errors = self.errors.unwrap_or_default();
errors.push(error.into());
errors.push(error_object.into());
Self {
errors: Some(errors),
@ -130,8 +130,8 @@ impl From<Document> for DocumentBuilder {
Some(errors) => {
let mut new_errors = Vec::new();
for error in errors {
new_errors.push(error.into());
for error_object in errors {
new_errors.push(error_object.into());
}
Some(new_errors)
@ -183,7 +183,7 @@ mod tests {
.link("qwe", LinkBuilder::new("http://qwe.com")),
)
.data(DataBuilder::Single(ResourceBuilder::new("qwerties")))
.errors(vec![ErrorBuilder::default().id("789")])
.errors(vec![ErrorObjectBuilder::default().id("789")])
.unwrap(),
Document {
jsonapi: Some(JsonApi {
@ -215,7 +215,7 @@ mod tests {
.link("self", LinkBuilder::new("http://self.com"))
.link("qwe", LinkBuilder::new("http://qwe.com"))
.data(DataBuilder::Single(ResourceBuilder::new("qwerties")))
.error(ErrorBuilder::default().id("789"))
.error(ErrorObjectBuilder::default().id("789"))
.unwrap(),
Document {
jsonapi: Some(JsonApi {
@ -408,7 +408,7 @@ mod tests {
fn with_errors() {
assert_eq!(
DocumentBuilder::default()
.errors(vec![ErrorBuilder::default().id("789")])
.errors(vec![ErrorObjectBuilder::default().id("789")])
.unwrap(),
Document {
jsonapi: None,

View File

@ -1,7 +1,7 @@
use super::*;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ErrorBuilder {
pub struct ErrorObjectBuilder {
id: Option<String>,
links: Option<LinksBuilder>,
status: Option<HttpStatus>,
@ -12,8 +12,8 @@ pub struct ErrorBuilder {
meta: Option<MetaOrAttrsBuilder>,
}
impl Builder<'_> for ErrorBuilder {
type Entity = Error;
impl Builder<'_> for ErrorObjectBuilder {
type Entity = ErrorObject;
fn finish(self) -> Result<Self::Entity, BuildErrors> {
Ok(Self::Entity {
@ -38,7 +38,7 @@ impl Builder<'_> for ErrorBuilder {
}
}
impl ErrorBuilder {
impl ErrorObjectBuilder {
pub fn id<I: ToString>(self, id: I) -> Self {
Self {
id: Some(id.to_string()),
@ -136,17 +136,17 @@ impl ErrorBuilder {
}
}
impl From<Error> for ErrorBuilder {
fn from(error: Error) -> Self {
impl From<ErrorObject> for ErrorObjectBuilder {
fn from(error_object: ErrorObject) -> Self {
Self {
id: error.id,
links: error.links.map(|links| links.into()),
status: error.status,
code: error.code,
title: error.title,
detail: error.detail,
source: error.source.map(|source| source.into()),
meta: error.meta.map(|meta| meta.into()),
id: error_object.id,
links: error_object.links.map(|links| links.into()),
status: error_object.status,
code: error_object.code,
title: error_object.title,
detail: error_object.detail,
source: error_object.source.map(|source| source.into()),
meta: error_object.meta.map(|meta| meta.into()),
}
}
}
@ -159,8 +159,8 @@ mod tests {
#[test]
fn empty() {
assert_eq!(
ErrorBuilder::default().unwrap(),
Error {
ErrorObjectBuilder::default().unwrap(),
ErrorObject {
id: None,
links: None,
status: None,
@ -176,7 +176,7 @@ mod tests {
#[test]
fn full() {
assert_eq!(
ErrorBuilder::default()
ErrorObjectBuilder::default()
.id("123")
.links(
LinksBuilder::default()
@ -198,7 +198,7 @@ mod tests {
.item("bar", "qwe"),
)
.unwrap(),
Error {
ErrorObject {
id: Some("123".into()),
links: Some(fixtures::simple_links()),
status: Some(HttpStatus::OK),
@ -217,7 +217,7 @@ mod tests {
#[test]
fn full_delegators() {
assert_eq!(
ErrorBuilder::default()
ErrorObjectBuilder::default()
.id("123")
.link("self", LinkBuilder::new("http://self.com"))
.link("qwe", LinkBuilder::new("http://qwe.com"))
@ -230,7 +230,7 @@ mod tests {
.meta1("foo", 123)
.meta1("bar", "qwe")
.unwrap(),
Error {
ErrorObject {
id: Some("123".into()),
links: Some(fixtures::simple_links()),
status: Some(HttpStatus::OK),

View File

@ -1,6 +1,6 @@
mod data;
mod document;
mod error;
mod error_object;
mod error_source;
mod jsonapi;
mod link;
@ -12,7 +12,7 @@ mod resource;
pub use data::DataBuilder;
pub use document::DocumentBuilder;
pub use error::ErrorBuilder;
pub use error_object::ErrorObjectBuilder;
pub use error_source::ErrorSourceBuilder;
pub use jsonapi::JsonApiBuilder;
pub use link::LinkBuilder;

View File

@ -1,9 +1,9 @@
use super::*;
impl Entity<'_> for Error {}
impl Entity<'_> for ErrorObject {}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Error {
pub struct ErrorObject {
pub id: Option<String>,
pub links: Option<Links>,
pub status: Option<HttpStatus>,

View File

@ -2,4 +2,4 @@ use super::*;
impl Entity<'_> for Errors {}
pub type Errors = Vec<Error>;
pub type Errors = Vec<ErrorObject>;

View File

@ -1,6 +1,6 @@
mod data;
mod document;
mod error;
mod error_object;
mod error_source;
mod errors;
mod http_status;
@ -16,7 +16,7 @@ mod version;
pub use data::Data;
pub use document::Document;
pub use error::Error;
pub use error_object::ErrorObject;
pub use error_source::ErrorSource;
pub use errors::Errors;
pub use http_status::HttpStatus;

View File

@ -1,5 +1,4 @@
use super::*;
use crate::entities::Error;
use std::collections::HashMap;
@ -114,8 +113,8 @@ pub fn simple_errors() -> Errors {
vec![simple_error()]
}
pub fn simple_error() -> Error {
Error {
pub fn simple_error() -> ErrorObject {
ErrorObject {
id: Some("789".into()),
links: None,
status: None,
@ -141,8 +140,8 @@ pub fn full_errors_value() -> Value {
json!([full_error_value()])
}
pub fn full_error() -> Error {
Error {
pub fn full_error() -> ErrorObject {
ErrorObject {
id: Some("789".into()),
links: Some(different_links()),
status: Some(HttpStatus::OK),