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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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