hubspot_rust_sdk/objects/
types.rs

1use std::{collections::HashMap, fmt::Display};
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct HubSpotObject {
7    pub id: String,
8    pub properties: Value,
9    pub associations: Option<HashMap<String, ObjectAssociations>>
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
13pub struct ObjectAssociations {
14    pub results: Vec<ObjectAssociation>
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
18pub struct ObjectAssociation {
19    pub id: String,
20    #[serde(rename = "type")]
21    _type: String,
22}
23
24impl HubSpotObject {
25    pub fn from_value(value: Value) -> Result<HubSpotObject, String> {
26        match serde_json::from_value(value) {
27            Ok(obj) => Ok(obj),
28            Err(e) => Err(format!("Failed to parse object: {}", e))
29        }
30    }
31}
32
33#[derive(Debug, Clone, PartialEq)]
34pub enum HubSpotObjectType {
35    Contact,
36    Company,
37    Deal,
38    CustomObject {
39        singular: String,
40        plural: String,
41    }
42}
43
44impl Display for HubSpotObjectType {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        match self {
47            HubSpotObjectType::Contact => write!(f, "contacts"),
48            HubSpotObjectType::Company => write!(f, "companies"),
49            HubSpotObjectType::Deal => write!(f, "deals"),
50            HubSpotObjectType::CustomObject { plural, .. } => write!(f, "{}", plural),
51        }
52    }
53}
54
55impl HubSpotObjectType {
56    pub fn to_string_singular(&self) -> String {
57        match self {
58            HubSpotObjectType::Contact => "contact".to_string(),
59            HubSpotObjectType::Company => "company".to_string(),
60            HubSpotObjectType::Deal => "deal".to_string(),
61            HubSpotObjectType::CustomObject { singular, .. } => singular.to_string(),
62        }
63    }
64}