cgd 0.1.0-alpha.2

Official command-line interface for Coragrid source-linked private-market data infrastructure
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use std::fmt;
use std::str::FromStr;

use serde::de::Error as _;
use serde::{Deserialize, Deserializer, Serialize};

const COMPANY_PREFIX: &str = "co_";
const DOCUMENT_PREFIX: &str = "doc_";
const TOKEN_LENGTH: usize = 26;
const CROCKFORD_LOWER: &str = "0123456789abcdefghjkmnpqrstvwxyz";

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct CompanyId(String);

impl CompanyId {
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for CompanyId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.0)
    }
}

impl FromStr for CompanyId {
    type Err = &'static str;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let token = value
            .strip_prefix(COMPANY_PREFIX)
            .ok_or("company IDs must begin with co_")?;
        if token.len() != TOKEN_LENGTH {
            return Err("company IDs must contain a 26-character token");
        }
        if !matches!(token.as_bytes().first(), Some(b'0'..=b'7')) {
            return Err("company ID token must encode exactly 128 bits");
        }
        if !token
            .chars()
            .all(|character| CROCKFORD_LOWER.contains(character))
        {
            return Err("company IDs use canonical lowercase Crockford Base32");
        }
        Ok(Self(value.to_owned()))
    }
}

impl<'de> Deserialize<'de> for CompanyId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::from_str(&value).map_err(D::Error::custom)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct DocumentId(String);

impl DocumentId {
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for DocumentId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.0)
    }
}

impl FromStr for DocumentId {
    type Err = &'static str;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let token = value
            .strip_prefix(DOCUMENT_PREFIX)
            .ok_or("document IDs must begin with doc_")?;
        if token.len() != TOKEN_LENGTH {
            return Err("document IDs must contain a 26-character token");
        }
        if !matches!(token.as_bytes().first(), Some(b'0'..=b'7')) {
            return Err("document ID token must encode exactly 128 bits");
        }
        if !token
            .chars()
            .all(|character| CROCKFORD_LOWER.contains(character))
        {
            return Err("document IDs use canonical lowercase Crockford Base32");
        }
        Ok(Self(value.to_owned()))
    }
}

impl<'de> Deserialize<'de> for DocumentId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::from_str(&value).map_err(D::Error::custom)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Envelope<T> {
    pub data: T,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CallerResource {
    pub object: String,
    pub credential_id: String,
    pub organization_id: String,
    pub principal_type: String,
    #[serde(default)]
    pub scopes: Vec<String>,
    pub limits: CallerLimits,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CallerLimits {
    pub max_search_results: u16,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CompanyLifecycle {
    pub status: String,
    #[serde(default)]
    pub canonical_id: Option<CompanyId>,
    #[serde(default)]
    pub successor_ids: Vec<CompanyId>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CompanyIdentifier {
    pub scheme: String,
    pub value: String,
    #[serde(default)]
    pub country_scope: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CompanyResource {
    pub id: CompanyId,
    pub object: String,
    pub lifecycle: CompanyLifecycle,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub summary: Option<String>,
    #[serde(default)]
    pub country_code: Option<String>,
    #[serde(default)]
    pub company_status: Option<String>,
    #[serde(default)]
    pub company_type: Option<String>,
    #[serde(default)]
    pub industry: Option<String>,
    #[serde(default)]
    pub business_model: Option<String>,
    #[serde(default)]
    pub product_category: Option<String>,
    #[serde(default)]
    pub operating_geography: Option<String>,
    #[serde(default)]
    pub as_of_date: Option<String>,
    #[serde(default)]
    pub identifiers: Vec<CompanyIdentifier>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct OfficerSource {
    pub system: String,
    #[serde(default)]
    pub url: Option<String>,
    #[serde(default)]
    pub title: Option<String>,
    #[serde(default)]
    pub published_at: Option<String>,
    #[serde(default)]
    pub observed_at: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CompanyOfficerResource {
    pub object: String,
    pub name: String,
    #[serde(default)]
    pub roles: Vec<String>,
    #[serde(default)]
    pub appointed_on: Option<String>,
    #[serde(default)]
    pub resigned_on: Option<String>,
    pub source: OfficerSource,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CompanyOfficerPage {
    pub object: String,
    pub requested_company_id: CompanyId,
    pub company_id: CompanyId,
    pub data: Vec<CompanyOfficerResource>,
    pub has_more: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DocumentLifecycle {
    pub status: String,
    #[serde(default)]
    pub canonical_id: Option<DocumentId>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DocumentSource {
    pub system: String,
    pub record_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DocumentAccessCapabilities {
    pub metadata: bool,
    pub bytes: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DocumentResource {
    pub id: DocumentId,
    pub object: String,
    pub lifecycle: DocumentLifecycle,
    pub company_id: CompanyId,
    pub source: DocumentSource,
    pub document_type: String,
    #[serde(default)]
    pub document_category: Option<String>,
    #[serde(default)]
    pub title: Option<String>,
    #[serde(default)]
    pub file_name: Option<String>,
    #[serde(default)]
    pub content_type: Option<String>,
    #[serde(default)]
    pub file_extension: Option<String>,
    #[serde(default)]
    pub sha256: Option<String>,
    #[serde(default)]
    pub byte_size: Option<u64>,
    #[serde(default)]
    pub filed_at: Option<String>,
    #[serde(default)]
    pub published_date: Option<String>,
    #[serde(default)]
    pub published_at: Option<String>,
    pub collected_at: String,
    pub availability: String,
    pub access: DocumentAccessCapabilities,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ExternalIdentifierSelector {
    pub scheme: String,
    pub value: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ResolveCompanyRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub domain: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub jurisdiction: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub identifiers: Vec<ExternalIdentifierSelector>,
    pub limit: u16,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ResolutionCandidate {
    pub id: CompanyId,
    #[serde(default)]
    pub match_reasons: Vec<String>,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub jurisdiction: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ResolutionResource {
    pub object: String,
    pub status: String,
    #[serde(default)]
    pub result: Option<ResolutionCandidate>,
    #[serde(default)]
    pub candidates: Vec<ResolutionCandidate>,
    #[serde(default)]
    pub requested_id: Option<CompanyId>,
    #[serde(default)]
    pub canonical_id: Option<CompanyId>,
    #[serde(default)]
    pub successor_ids: Vec<CompanyId>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct CompanyFilters {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub countries: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub company_status: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub company_types: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub financial: Option<FinancialRange>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum FinancialField {
    Revenue,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ReportingScope {
    Company,
    Group,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct FinancialRange {
    pub field: FinancialField,
    pub gte: Option<i64>,
    pub lte: Option<i64>,
    pub currency: String,
    pub fiscal_year: i32,
    pub reporting_scope: ReportingScope,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum CompanyInclude {
    Profile,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SearchCompaniesRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query: Option<String>,
    pub filters: CompanyFilters,
    pub include: Vec<CompanyInclude>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
    pub limit: u16,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CompanySearchItem {
    #[serde(flatten)]
    pub company: CompanyResource,
    #[serde(default)]
    pub relevance_score: Option<f32>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CompanyPage {
    pub data: Vec<CompanySearchItem>,
    #[serde(default)]
    pub next_cursor: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Problem {
    pub r#type: String,
    pub title: String,
    pub status: u16,
    pub detail: String,
    pub instance: String,
    pub request_id: String,
    pub code: String,
    pub retryable: bool,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn company_id_accepts_only_the_public_canonical_grammar() {
        let valid = "co_014d2pf2dbsqqg28t5cy4tqkff";
        assert_eq!(CompanyId::from_str(valid).unwrap().as_str(), valid);

        for invalid in [
            "018f6d3d-11a3-7c22-9c8f-3e64e1e7fa83",
            "co_814d2pf2dbsqqg28t5cy4tqkff",
            "co_014D2pf2dbsqqg28t5cy4tqkff",
            "co_014d2pf2dbsqqg28t5cy4tqkfi",
            "fact_014d2pf2dbsqqg28t5cy4tqkff",
        ] {
            assert!(CompanyId::from_str(invalid).is_err(), "accepted {invalid}");
        }
    }

    #[test]
    fn additive_fields_and_unknown_lifecycle_values_remain_compatible() {
        let json = r#"{
            "id":"co_014d2pf2dbsqqg28t5cy4tqkff",
            "object":"company",
            "lifecycle":{"status":"future_status","future_link":"co_x"},
            "name":"Example",
            "future_field":{"nested":true}
        }"#;
        let company: CompanyResource = serde_json::from_str(json).unwrap();
        assert_eq!(company.lifecycle.status, "future_status");
        assert_eq!(company.name.as_deref(), Some("Example"));
    }

    #[test]
    fn standalone_document_response_matches_the_owned_client_contract() {
        let envelope: Envelope<DocumentResource> =
            serde_json::from_str(crate::client::TEST_DOCUMENT_METADATA).unwrap();
        assert_eq!(envelope.data.id.as_str(), "doc_014d2pf2dbsqqg28t5cy4tqkff");
        assert!(envelope.data.access.metadata);
        assert!(!envelope.data.access.bytes);
    }
}