Add entity ErrorSource

This commit is contained in:
Alex Kotov 2022-08-02 20:12:37 +04:00
parent 3e24709ba4
commit b552a4f725
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
4 changed files with 123 additions and 0 deletions

View File

@ -0,0 +1,109 @@
use super::*;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ErrorSourceBuilder {
pointer: Option<String>,
parameter: Option<String>,
}
impl Builder<'_> for ErrorSourceBuilder {
type Entity = ErrorSource;
fn finish(self) -> Result<Self::Entity, ()> {
Ok(Self::Entity {
pointer: self.pointer,
parameter: self.parameter,
})
}
}
impl ErrorSourceBuilder {
pub fn pointer<P: ToString>(self, pointer: P) -> Self {
Self {
pointer: Some(pointer.to_string()),
..self
}
}
pub fn parameter<P: ToString>(self, parameter: P) -> Self {
Self {
parameter: Some(parameter.to_string()),
..self
}
}
}
impl From<ErrorSource> for ErrorSourceBuilder {
fn from(error_source: ErrorSource) -> Self {
Self {
pointer: error_source.pointer,
parameter: error_source.parameter,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty() {
assert_eq!(
ErrorSourceBuilder::default().unwrap(),
ErrorSource {
pointer: None,
parameter: None,
},
);
}
#[test]
fn full() {
assert_eq!(
ErrorSourceBuilder::default()
.pointer("/foo/0/bar/1")
.parameter("car")
.unwrap(),
ErrorSource {
pointer: Some("/foo/0/bar/1".into()),
parameter: Some("car".into()),
},
);
}
#[test]
fn with_pointer() {
assert_eq!(
ErrorSourceBuilder::default()
.pointer("/foo/0/bar/1")
.unwrap(),
ErrorSource {
pointer: Some("/foo/0/bar/1".into()),
parameter: None,
},
);
}
#[test]
fn with_parameter() {
assert_eq!(
ErrorSourceBuilder::default().parameter("car").unwrap(),
ErrorSource {
pointer: None,
parameter: Some("car".into()),
},
);
}
#[test]
fn implicit_from_entity() {
let error_source = ErrorSource {
pointer: Some("/foo/0/bar/1".into()),
parameter: Some("car".into()),
};
let builder: ErrorSourceBuilder = error_source.clone().into();
assert_eq!(builder.unwrap(), error_source);
}
}

View File

@ -1,5 +1,6 @@
mod data;
mod document;
mod error_source;
mod jsonapi;
mod link;
mod links;
@ -10,6 +11,7 @@ mod resource;
pub use data::DataBuilder;
pub use document::DocumentBuilder;
pub use error_source::ErrorSourceBuilder;
pub use jsonapi::JsonApiBuilder;
pub use link::LinkBuilder;
pub use links::LinksBuilder;

View File

@ -0,0 +1,10 @@
use super::*;
impl Entity<'_> for ErrorSource {}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ErrorSource {
// TODO: Add entity with validation
pub pointer: Option<String>,
pub parameter: Option<String>,
}

View File

@ -1,5 +1,6 @@
mod data;
mod document;
mod error_source;
mod http_status;
mod jsonapi;
mod link;
@ -13,6 +14,7 @@ mod version;
pub use data::Data;
pub use document::Document;
pub use error_source::ErrorSource;
pub use http_status::HttpStatus;
pub use jsonapi::JsonApi;
pub use link::Link;