fkl_parser/mir/strategy/
bounded_context.rs

1use std::fmt::{Display, Formatter};
2
3use serde::Deserialize;
4use serde::Serialize;
5
6use crate::mir::tactic::aggregate::Aggregate;
7
8// # Bounded Context
9// A description of a boundary (typically a subsystem, or the work of a particular team) within
10// which a particular model is defined and applicable.
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
12pub struct BoundedContext {
13  pub name: String,
14  pub aggregates: Vec<Aggregate>,
15}
16
17impl BoundedContext {
18  pub fn new(name: &str) -> Self {
19    BoundedContext { name: name.to_string(), aggregates: vec![] }
20  }
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
24#[serde(rename_all = "camelCase")]
25pub struct ContextRelation {
26  pub source: String,
27  pub target: String,
28  pub connection_type: ConnectionDirection,
29  pub source_type: Vec<ContextRelationType>,
30  pub target_type: Vec<ContextRelationType>,
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
34pub enum ConnectionDirection {
35  Undirected,
36  // -->
37  PositiveDirected,
38  // <--
39  NegativeDirected,
40  // <->
41  BiDirected,
42}
43
44impl Default for ConnectionDirection {
45  fn default() -> Self {
46    ConnectionDirection::Undirected
47  }
48}
49
50impl ConnectionDirection {
51  pub fn from_connection(str: &str) -> Self {
52    match str {
53      "Undirected" | "-" | "--" => ConnectionDirection::Undirected,
54      "PositiveDirected" | "->" => ConnectionDirection::PositiveDirected,
55      "NegativeDirected" | "<-" => ConnectionDirection::NegativeDirected,
56      "BiDirected" | "<->" => ConnectionDirection::BiDirected,
57      _ => ConnectionDirection::Undirected,
58    }
59  }
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
63#[serde(rename_all = "camelCase")]
64pub enum ContextRelationType {
65  None,
66  // Symmetric relation
67  SharedKernel,
68  Partnership,
69  // Upstream Downstream
70  CustomerSupplier,
71  Conformist,
72  AntiCorruptionLayer,
73  OpenHostService,
74  PublishedLanguage,
75  SeparateWay,
76  // added in book "DDD Reference"
77  BigBallOfMud,
78}
79
80impl Default for ContextRelationType {
81  fn default() -> Self {
82    ContextRelationType::None
83  }
84}
85
86impl Display for ContextRelationType {
87  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
88    match self {
89      ContextRelationType::None => write!(f, "None"),
90      ContextRelationType::SharedKernel => write!(f, "SharedKernel"),
91      ContextRelationType::Partnership => write!(f, "Partnership"),
92      ContextRelationType::CustomerSupplier => write!(f, "CustomerSupplier"),
93      ContextRelationType::Conformist => write!(f, "Conformist"),
94      ContextRelationType::AntiCorruptionLayer => write!(f, "AntiCorruptionLayer"),
95      ContextRelationType::OpenHostService => write!(f, "OpenHostService"),
96      ContextRelationType::PublishedLanguage => write!(f, "PublishedLanguage"),
97      ContextRelationType::SeparateWay => write!(f, "SeparateWay"),
98      ContextRelationType::BigBallOfMud => write!(f, "BigBallOfMud"),
99    }
100  }
101}
102
103impl ContextRelationType {
104  pub fn list(types: &Vec<String>) -> Vec<ContextRelationType> {
105    types
106      .iter()
107      .map(|t| match t.to_lowercase().as_str() {
108        "sharedkernel" | "sk" => ContextRelationType::SharedKernel,
109        "partnership" | "p" => ContextRelationType::Partnership,
110        "customersupplier" | "cs" => ContextRelationType::CustomerSupplier,
111        "conformist" | "c" => ContextRelationType::Conformist,
112        "anticorruptionlayer" | "acl" => ContextRelationType::AntiCorruptionLayer,
113        "openhostservice" | "ohs" => ContextRelationType::OpenHostService,
114        "publishedlanguage" | "pl" => ContextRelationType::PublishedLanguage,
115        "separateway" | "sw" => ContextRelationType::SeparateWay,
116        "bigballofmud" | "bb" => ContextRelationType::BigBallOfMud,
117        _ => {
118          ContextRelationType::None
119        }
120      })
121      .collect()
122  }
123}