reactive_graph/client/types/entities/
mod.rs

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