reactive_graph/client/types/relations/
mod.rs

1use 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::NamespacedTypeContainer;
14use std::sync::Arc;
15
16pub(crate) mod args;
17pub(crate) mod commands;
18pub(crate) mod output_format;
19
20pub(crate) async fn relation_types(client: &Arc<ReactiveGraphClient>, relation_type_args: RelationTypesArgs) -> CommandResult {
21    let output_format_wrapper: RelationTypesOutputFormatWrapper = relation_type_args.output_format.clone().into();
22    let Some(command) = relation_type_args.commands else {
23        return Err(CommandError::MissingSubCommand);
24    };
25    match command {
26        RelationTypesCommands::List => match client.types().relations().get_all_relation_types().await {
27            Ok(Some(relation_types)) => output_format_wrapper.collection(relation_types),
28            Ok(None) => Err(NoContent("No relation types found".to_string())),
29            Err(e) => Err(e.into()),
30        },
31        RelationTypesCommands::Get(args) => match client.types().relations().get_relation_type_by_type(args.clone()).await {
32            Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
33            Ok(None) => Err(args.not_found()),
34            Err(e) => Err(e.into()),
35        },
36        RelationTypesCommands::ListProperties(args) => match client.types().relations().get_relation_type_by_type(args.clone()).await {
37            Ok(Some(relation_type)) => {
38                let output_format_wrapper: PropertyTypesOutputFormatWrapper = relation_type_args.output_format.into();
39                output_format_wrapper.collection(relation_type.properties.to_vec())
40            }
41            Ok(None) => Err(args.not_found()),
42            Err(e) => Err(e.into()),
43        },
44        RelationTypesCommands::ListExtensions(args) => match client.types().relations().get_relation_type_by_type(args.clone()).await {
45            Ok(Some(relation_type)) => {
46                let output_format_wrapper: ExtensionsOutputFormatWrapper = relation_type_args.output_format.into();
47                output_format_wrapper.collection(relation_type.extensions.to_vec())
48            }
49            Ok(None) => Err(args.not_found()),
50            Err(e) => Err(e.into()),
51        },
52        RelationTypesCommands::ListComponents(args) => match client.types().relations().get_relation_type_components(args.clone()).await {
53            Ok(Some(components)) => {
54                let output_format_wrapper: ComponentsOutputFormatWrapper = relation_type_args.output_format.into();
55                output_format_wrapper.collection(components.to_vec())
56            }
57            Ok(None) => Err(args.not_found()),
58            Err(e) => Err(e.into()),
59        },
60        RelationTypesCommands::Create(args) => match client.types().relations().create_relation_type_with_variables((&args).into()).await {
61            Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
62            Ok(None) => Err(NotCreated("Relation type wasn't created".to_string())),
63            Err(e) => Err(e.into()),
64        },
65        RelationTypesCommands::Delete(args) => match client.types().relations().delete_relation_type_with_variables((&args).into()).await {
66            Ok(Some(true)) => Ok(format!("Relation type {}__{} deleted", args.namespace, args.name).into()),
67            Ok(Some(false)) => Ok(format!("Relation type {}__{} not deleted", args.namespace, args.name).into()),
68            Ok(None) => Err(args.not_found()),
69            Err(e) => Err(e.into()),
70        },
71        RelationTypesCommands::AddProperty(args) => match client.types().relations().add_property_with_variables((&args).into()).await {
72            Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
73            Ok(None) => Err(NotCreated("Property wasn't created".to_string())),
74            Err(e) => Err(e.into()),
75        },
76        RelationTypesCommands::RemoveProperty(args) => match client.types().relations().remove_property_with_variables((&args).into()).await {
77            Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
78            Ok(None) => Err(NotFound(format!("Relation type {}__{} not found", args.ty.namespace, args.ty.name))),
79            Err(e) => Err(e.into()),
80        },
81        RelationTypesCommands::AddExtension(args) => match client.types().relations().add_extension_with_variables((&args).into()).await {
82            Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
83            Ok(None) => Err(NotCreated("Extension wasn't created".to_string())),
84            Err(e) => Err(e.into()),
85        },
86        RelationTypesCommands::RemoveExtension(args) => match client.types().relations().remove_extension_with_variables((&args).into()).await {
87            Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
88            Ok(None) => Err(args.relation_ty.not_found()),
89            Err(e) => Err(e.into()),
90        },
91        RelationTypesCommands::AddComponent(args) => match client.types().relations().add_component((&args).into()).await {
92            Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
93            Ok(None) => Err(NotCreated("Component wasn't added to relation type".to_string())),
94            Err(e) => Err(e.into()),
95        },
96        RelationTypesCommands::RemoveComponent(args) => match client.types().relations().remove_component((&args).into()).await {
97            Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
98            Ok(None) => Err(args.ty.not_found()),
99            Err(e) => Err(e.into()),
100        },
101        RelationTypesCommands::UpdateDescription(args) => match client.types().relations().update_description_with_variables((&args).into()).await {
102            Ok(Some(relation_type)) => output_format_wrapper.single(relation_type),
103            Ok(None) => Err(args.ty.not_found()),
104            Err(e) => Err(e.into()),
105        },
106        RelationTypesCommands::JsonSchema => match client.json_schema().types().relations().await {
107            Ok(Some(json_schema)) => Ok(json_schema.into()),
108            Ok(None) => Err(NotFound("JSON Schema not available".to_string())),
109            Err(e) => Err(e.into()),
110        },
111    }
112}