reactive_graph/client/types/relations/
mod.rs1use crate::client::error::CommandError;
2use crate::client::error::CommandError::NoContent;
3use crate::client::error::CommandError::NotCreated;
4use crate::client::error::CommandError::NotFound;
5use crate::client::result::CommandResult;
6use crate::client::types::components::output_format::ComponentsOutputFormatWrapper;
7use crate::client::types::extension::output_format::ExtensionsOutputFormatWrapper;
8use crate::client::types::property_type::output_format::PropertyTypesOutputFormatWrapper;
9use crate::client::types::relations::args::RelationTypesArgs;
10use crate::client::types::relations::commands::RelationTypesCommands;
11use crate::client::types::relations::output_format::RelationTypesOutputFormatWrapper;
12use reactive_graph_client::ReactiveGraphClient;
13use reactive_graph_graph::InboundOutboundType;
14use reactive_graph_graph::MatchingInboundOutboundType;
15use reactive_graph_graph::NamespacedTypeContainer;
16use reactive_graph_graph::NamespacedTypeGetter;
17use reactive_graph_graph::RelationType;
18use std::sync::Arc;
19
20pub(crate) mod args;
21pub(crate) mod commands;
22pub(crate) mod output_format;
23
24pub(crate) async fn relation_types(client: &Arc<ReactiveGraphClient>, relation_type_args: RelationTypesArgs) -> CommandResult {
25 let output_format_wrapper: RelationTypesOutputFormatWrapper = relation_type_args.output_format.clone().into();
26 let Some(command) = relation_type_args.commands else {
27 return Err(CommandError::MissingSubCommand);
28 };
29 match command {
30 RelationTypesCommands::List => match client.types().relations().get_all_relation_types().await {
31 Ok(Some(relation_types)) => output_format_wrapper.collection(relation_types.to_vec()),
32 Ok(None) => Err(NoContent("No relation types found".to_string())),
33 Err(e) => Err(e.into()),
34 },
35 RelationTypesCommands::Get { relation_ty } => match client.types().relations().get_relation_type_by_type(&relation_ty).await {
36 Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
37 Ok(None) => Err(CommandError::NamespacedTypeNotFound(relation_ty.namespaced_type())),
38 Err(e) => Err(e.into()),
39 },
40 RelationTypesCommands::ListProperties { relation_ty } => match client.types().relations().get_relation_type_by_type(&relation_ty).await {
41 Ok(Some(relation_type)) => {
42 let output_format_wrapper: PropertyTypesOutputFormatWrapper = relation_type_args.output_format.into();
43 output_format_wrapper.collection(relation_type.properties.to_vec())
44 }
45 Ok(None) => Err(CommandError::NamespacedTypeNotFound(relation_ty.namespaced_type())),
46 Err(e) => Err(e.into()),
47 },
48 RelationTypesCommands::ListExtensions { relation_ty } => match client.types().relations().get_relation_type_by_type(&relation_ty).await {
49 Ok(Some(relation_type)) => {
50 let output_format_wrapper: ExtensionsOutputFormatWrapper = relation_type_args.output_format.into();
51 output_format_wrapper.collection(relation_type.extensions.to_vec())
52 }
53 Ok(None) => Err(CommandError::NamespacedTypeNotFound(relation_ty.namespaced_type())),
54 Err(e) => Err(e.into()),
55 },
56 RelationTypesCommands::ListComponents { relation_ty } => match client.types().relations().get_relation_type_components(&relation_ty).await {
57 Ok(Some(components)) => {
58 let output_format_wrapper: ComponentsOutputFormatWrapper = relation_type_args.output_format.into();
59 output_format_wrapper.collection(components.to_vec())
60 }
61 Ok(None) => Err(CommandError::NamespacedTypeNotFound(relation_ty.namespaced_type())),
62 Err(e) => Err(e.into()),
63 },
64 RelationTypesCommands::GetJsonSchema { relation_ty } => match client.types().relations().json_schema_for_relation_type_by_type(&relation_ty).await {
65 Ok(Some(json_schema)) => Ok(json_schema.into()),
66 Ok(None) => Err(CommandError::NamespacedTypeNotFound(relation_ty.namespaced_type())),
67 Err(e) => Err(e.into()),
68 },
69 RelationTypesCommands::Create(args) => match client
70 .types()
71 .relations()
72 .create_relation_type(
73 RelationType::builder()
74 .outbound_type(InboundOutboundType::EntityType(MatchingInboundOutboundType::NamespacedType(args.outbound_ty.clone())))
75 .ty(&args.relation_ty)
76 .inbound_type(InboundOutboundType::EntityType(MatchingInboundOutboundType::NamespacedType(args.inbound_ty.clone())))
77 .description(args.description.unwrap_or_default())
78 .build(),
79 )
80 .await
81 {
82 Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
83 Ok(None) => Err(NotCreated("Relation type wasn't created".to_string())),
84 Err(e) => Err(e.into()),
85 },
86 RelationTypesCommands::Delete { relation_ty } => match client.types().relations().delete_relation_type(&relation_ty).await {
87 Ok(Some(true)) => Ok(format!("Entity Type {} deleted", relation_ty).into()),
88 Ok(Some(false)) => Ok(format!("Entity Type {} not deleted", relation_ty).into()),
89 Ok(None) => Err(CommandError::NamespacedTypeNotFound(relation_ty.namespaced_type())),
90 Err(e) => Err(e.into()),
91 },
92 RelationTypesCommands::AddProperty(args) => match client.types().relations().add_property(&args.relation_ty, &args.property_type).await {
93 Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
94 Ok(None) => Err(NotCreated("Property wasn't created".to_string())),
95 Err(e) => Err(e.into()),
96 },
97 RelationTypesCommands::RemoveProperty(args) => match client.types().relations().remove_property(&args.relation_ty, args.property_name.clone()).await {
98 Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
99 Ok(None) => Err(CommandError::NamespacedTypeNotFound(args.relation_ty.namespaced_type())),
100 Err(e) => Err(e.into()),
101 },
102 RelationTypesCommands::AddExtension(args) => match client.types().relations().add_extension(&args.relation_ty, &args.extension).await {
103 Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
104 Ok(None) => Err(NotCreated("Extension wasn't created".to_string())),
105 Err(e) => Err(e.into()),
106 },
107 RelationTypesCommands::RemoveExtension(args) => match client.types().relations().remove_extension(&args).await {
108 Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
109 Ok(None) => Err(CommandError::NamespacedTypeNotFound(args.relation_ty.namespaced_type())),
110 Err(e) => Err(e.into()),
111 },
112 RelationTypesCommands::AddComponent(args) => match client.types().relations().add_component(&args).await {
113 Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
114 Ok(None) => Err(NotCreated("Component wasn't added to relation type".to_string())),
115 Err(e) => Err(e.into()),
116 },
117 RelationTypesCommands::RemoveComponent(args) => match client.types().relations().remove_component(&args).await {
118 Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
119 Ok(None) => Err(CommandError::NamespacedTypeNotFound(args.relation_ty.namespaced_type())),
120 Err(e) => Err(e.into()),
121 },
122 RelationTypesCommands::UpdateDescription(args) => match client.types().relations().update_description(&args.relation_ty, args.description).await {
123 Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
124 Ok(None) => Err(CommandError::NamespacedTypeNotFound(args.relation_ty.namespaced_type())),
125 Err(e) => Err(e.into()),
126 },
127 RelationTypesCommands::JsonSchema => match client.json_schema().types().relations().await {
128 Ok(Some(json_schema)) => Ok(json_schema.into()),
129 Ok(None) => Err(NotFound("JSON Schema not available".to_string())),
130 Err(e) => Err(e.into()),
131 },
132 }
133}