reactive_graph/client/types/entities/
mod.rs

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