Skip to content

Commit

Permalink
add breach VOs
Browse files Browse the repository at this point in the history
  • Loading branch information
n1nj4t4nuk1 committed Dec 14, 2024
1 parent 3cb700c commit 5202a6b
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 0 deletions.
Empty file.
62 changes: 62 additions & 0 deletions libs/cti/src/breaches/domain/entities/breach_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::shared::domain::errors::DomainError;
use std::hash::{Hash, Hasher};

#[derive(Debug)]
pub struct BreachId {
value: String,
}

impl BreachId {
pub fn new(id: &String) -> Result<Self, DomainError> {
if id.contains(" ") {
return Err(DomainError::ValueObjectError {
value: "CVE id must not contain blank spaces".to_string(),
});
}
Ok(Self { value: id.clone() })
}

pub fn from_optional(id: &Option<String>) -> Result<Self, DomainError> {
if id.is_none() {
return Err(DomainError::ValueObjectError {
value: "CVE id must not be empty".to_string(),
});
}
let id = id.as_ref().unwrap();
Self::new(&id)
}

pub fn value(&self) -> String {
self.value.clone()
}

pub fn rvalue(&self) -> &String {
&self.value
}
}

impl Clone for BreachId {
fn clone(&self) -> Self {
Self::new(&self.value).unwrap()
}
}

impl PartialEq for BreachId {
fn eq(&self, other: &Self) -> bool {
self.value() == other.value()
}
}

impl Eq for BreachId {}

impl Hash for BreachId {
fn hash<H: Hasher>(&self, state: &mut H) {
self.value.hash(state);
}
}

impl std::fmt::Display for BreachId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.value)
}
}
53 changes: 53 additions & 0 deletions libs/cti/src/breaches/domain/entities/breach_product.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::shared::domain::errors::DomainError;
use std::hash::{Hash, Hasher};

#[derive(Debug)]
pub struct BreachProduct {
value: String,
}

impl BreachProduct {
pub fn new(value: &String) -> Result<Self, DomainError> {
Ok(Self {
value: value.clone(),
})
}

pub fn from_optional(value: &Option<String>) -> Result<Self, DomainError> {
if value.is_none() {
return Err(DomainError::ValueObjectError {
value: "CVE state must not be null".to_string(),
});
}
let value = value.as_ref().unwrap();
Self::new(&value)
}

pub fn value(&self) -> String {
self.value.clone()
}

pub fn ref_value(&self) -> &String {
&self.value
}
}

impl Clone for BreachProduct {
fn clone(&self) -> Self {
Self::new(&self.value).unwrap()
}
}

impl PartialEq for BreachProduct {
fn eq(&self, other: &Self) -> bool {
self.value() == other.value()
}
}

impl Eq for BreachProduct {}

impl Hash for BreachProduct {
fn hash<H: Hasher>(&self, state: &mut H) {
self.value.hash(state);
}
}
53 changes: 53 additions & 0 deletions libs/cti/src/breaches/domain/entities/breach_product_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::shared::domain::errors::DomainError;
use std::hash::{Hash, Hasher};

#[derive(Debug)]
pub struct BreachProductType {
value: String,
}

impl BreachProductType {
pub fn new(value: &String) -> Result<Self, DomainError> {
Ok(Self {
value: value.clone(),
})
}

pub fn from_optional(value: &Option<String>) -> Result<Self, DomainError> {
if value.is_none() {
return Err(DomainError::ValueObjectError {
value: "CVE state must not be null".to_string(),
});
}
let value = value.as_ref().unwrap();
Self::new(&value)
}

pub fn value(&self) -> String {
self.value.clone()
}

pub fn ref_value(&self) -> &String {
&self.value
}
}

impl Clone for BreachProductType {
fn clone(&self) -> Self {
Self::new(&self.value).unwrap()
}
}

impl PartialEq for BreachProductType {
fn eq(&self, other: &Self) -> bool {
self.value() == other.value()
}
}

impl Eq for BreachProductType {}

impl Hash for BreachProductType {
fn hash<H: Hasher>(&self, state: &mut H) {
self.value.hash(state);
}
}
53 changes: 53 additions & 0 deletions libs/cti/src/breaches/domain/entities/breach_product_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::shared::domain::errors::DomainError;
use std::hash::{Hash, Hasher};

#[derive(Debug)]
pub struct BreachProductVersion {
value: String,
}

impl BreachProductVersion {
pub fn new(value: &String) -> Result<Self, DomainError> {
Ok(Self {
value: value.clone(),
})
}

pub fn from_optional(value: &Option<String>) -> Result<Self, DomainError> {
if value.is_none() {
return Err(DomainError::ValueObjectError {
value: "CVE state must not be null".to_string(),
});
}
let value = value.as_ref().unwrap();
Self::new(&value)
}

pub fn value(&self) -> String {
self.value.clone()
}

pub fn ref_value(&self) -> &String {
&self.value
}
}

impl Clone for BreachProductVersion {
fn clone(&self) -> Self {
Self::new(&self.value).unwrap()
}
}

impl PartialEq for BreachProductVersion {
fn eq(&self, other: &Self) -> bool {
self.value() == other.value()
}
}

impl Eq for BreachProductVersion {}

impl Hash for BreachProductVersion {
fn hash<H: Hasher>(&self, state: &mut H) {
self.value.hash(state);
}
}
53 changes: 53 additions & 0 deletions libs/cti/src/breaches/domain/entities/breach_vendor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::shared::domain::errors::DomainError;
use std::hash::{Hash, Hasher};

#[derive(Debug)]
pub struct BreachVendor {
value: String,
}

impl BreachVendor {
pub fn new(value: &String) -> Result<Self, DomainError> {
Ok(Self {
value: value.clone(),
})
}

pub fn from_optional(value: &Option<String>) -> Result<Self, DomainError> {
if value.is_none() {
return Err(DomainError::ValueObjectError {
value: "CVE state must not be null".to_string(),
});
}
let value = value.as_ref().unwrap();
Self::new(&value)
}

pub fn value(&self) -> String {
self.value.clone()
}

pub fn ref_value(&self) -> &String {
&self.value
}
}

impl Clone for BreachVendor {
fn clone(&self) -> Self {
Self::new(&self.value).unwrap()
}
}

impl PartialEq for BreachVendor {
fn eq(&self, other: &Self) -> bool {
self.value() == other.value()
}
}

impl Eq for BreachVendor {}

impl Hash for BreachVendor {
fn hash<H: Hasher>(&self, state: &mut H) {
self.value.hash(state);
}
}
5 changes: 5 additions & 0 deletions libs/cti/src/breaches/domain/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod breach_id;
pub mod breach_product;
pub mod breach_product_type;
pub mod breach_product_version;
pub mod breach_vendor;
Empty file.
3 changes: 3 additions & 0 deletions libs/cti/src/breaches/domain/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod entities;
pub mod repositories;
pub mod events;
Empty file.

0 comments on commit 5202a6b

Please sign in to comment.