reactive_graph/client/instances/relations/args/
search.rs

1use crate::client::instances::properties::args::parse_property;
2use crate::client::types::components::args::parse_component_ty;
3use crate::client::types::relations::args::parse_relation_ty;
4use clap::Args;
5use reactive_graph_client::PropertyInstanceDefinitions;
6use reactive_graph_client::client::instances::relations::variables::search::variables::SearchRelationInstancesVariables;
7use reactive_graph_graph::ComponentTypeId;
8use reactive_graph_graph::ComponentTypeIds;
9use reactive_graph_graph::NamespacedTypeGetter;
10use reactive_graph_graph::NamespacedTypeIdContainer;
11use reactive_graph_graph::PropertyInstances;
12use reactive_graph_graph::RelationTypeId;
13use serde_json::Value;
14use uuid::Uuid;
15
16/// CLI argument for searching relation instances.
17#[derive(Args, Debug, Clone)]
18pub(crate) struct SearchRelationInstancesArgs {
19    /// The id of the outbound entity instance.
20    #[clap(long)]
21    pub outbound_id: Option<Uuid>,
22
23    /// The fully qualified namespace of the relation type.
24    #[clap(name = "relation_type", value_parser = parse_relation_ty)]
25    pub relation_ty: Option<RelationTypeId>,
26
27    /// The id of the inbound entity instance.
28    #[clap(short, long)]
29    pub inbound_id: Option<Uuid>,
30
31    /// The properties to search for.
32    #[clap(short, long, value_parser = parse_property)]
33    pub properties: Option<Vec<(String, Value)>>,
34
35    /// The components to search for.
36    #[clap(short, long, value_parser = parse_component_ty)]
37    pub components: Option<Vec<ComponentTypeId>>,
38}
39
40impl SearchRelationInstancesArgs {
41    fn properties(&self) -> Option<PropertyInstances> {
42        match &self.properties {
43            None => None,
44            Some(properties) => Some(properties.iter().map(|(name, value)| (name.clone(), value.clone())).collect()),
45        }
46    }
47
48    fn components(&self) -> Option<ComponentTypeIds> {
49        match &self.components {
50            None => None,
51            Some(components) => Some(ComponentTypeIds::from_iter(components.iter().map(|ty| ty.clone()))),
52        }
53        // self.components
54        //     .clone()
55        //     .map(|components| ComponentTypeIds::from_iter(components.iter().map(|ty| ty.clone())))
56    }
57}
58
59impl From<&SearchRelationInstancesArgs> for SearchRelationInstancesVariables {
60    fn from(args: &SearchRelationInstancesArgs) -> Self {
61        SearchRelationInstancesVariables {
62            outbound_id: args.outbound_id.map(From::from),
63            _type: args.relation_ty.clone().map(|relation_ty| relation_ty.namespace().to_string()),
64            inbound_id: args.inbound_id.map(From::from),
65            properties: args.properties().map(|properties| PropertyInstanceDefinitions::from(properties).0),
66            components: args.components().map(|components| components.into_fully_qualified_namespaces()),
67        }
68    }
69}