samling/
jsonschema.rs

1use crate::{
2    auth::{filters::UserFilters, sorting::UserSortOrder},
3    exports::GroupBy,
4    ApiErrorCode, ApiErrorResponse, CollectionFilters, CreateGroup, CreateUser, ExportField,
5    ExportFormat, Group, GroupSummary, I18nString, NestedStyleSortOrder, Result, StyleFilters,
6    UpdateGroup, UpdateOwnUser, UpdateUser,
7};
8use std::path::Path;
9
10use schemars::{schema_for, JsonSchema};
11use tokio::io::AsyncWriteExt;
12
13pub async fn generate_schema_file(output_path: &Path) -> Result<()> {
14    let schema = schema_for!(PublicJsonSchema);
15    let schema_data = serde_json::to_string_pretty(&schema)?;
16    tracing::info!("Writing schema to {output_path:?}...");
17    let mut file = tokio::fs::File::create(&output_path).await?;
18    file.write_all(schema_data.as_bytes()).await?;
19    Ok(())
20}
21
22// To find JsonSchema derived structs you can use ripgrep (rg):
23//     rg -A 3 JsonSchema | rg -r '$1' '.+struct (\w+) .+' | sort
24#[derive(JsonSchema)]
25#[allow(dead_code)]
26pub(crate) struct PublicJsonSchema {
27    i18n_string: I18nString,
28    auth: AuthJsonSchema,
29    errors: ErrorsSchema,
30    export: ExportSchema,
31    filters: FiltersSchema,
32    sort_by: SortByJsonSchema,
33    language: crate::Language,
34    environment: crate::Environment,
35    attribute: crate::Attribute,
36    attribute_type: crate::AttributeType,
37    attribute_type_summary: crate::AttributeTypeSummary,
38    category: crate::Category,
39    category_summary: crate::CategorySummary,
40    collection: crate::Collection,
41    collection_style_item: crate::CollectionItem,
42    collection_with_items: crate::CollectionWithItems,
43    collection_summary: crate::CollectionSummary,
44    update_collection: crate::UpdateCollection,
45    create_collection: crate::CreateCollection,
46    color: crate::Color,
47    color_summary: crate::ColorSummary,
48    image: crate::Image,
49    image_summary: crate::ImageSummary,
50    nested_attribute: crate::AttributeSummary,
51    nested_color: crate::NestedColor,
52    nested_price: crate::NestedPrice,
53    nested_size: crate::NestedSize,
54    nested_style: crate::NestedStyle,
55    nested_style_summary: crate::NestedStyleSummary,
56    organization: crate::Organization,
57    price: crate::Price,
58    price_list: crate::PriceList,
59    price_list_summary: crate::PriceListSummary,
60    size: crate::Size,
61    style: crate::Style,
62    style_summary: crate::StyleSummary,
63}
64
65#[derive(JsonSchema)]
66#[allow(dead_code)]
67pub(crate) struct ErrorsSchema {
68    response: ApiErrorResponse,
69    code: ApiErrorCode,
70}
71
72#[derive(JsonSchema)]
73#[allow(dead_code)]
74pub(crate) struct SortByJsonSchema {
75    user: UserSortOrder,
76    nested_style: NestedStyleSortOrder,
77}
78
79#[derive(JsonSchema)]
80#[allow(dead_code)]
81pub(crate) struct ExportSchema {
82    format: ExportFormat,
83    field: ExportField,
84    group_by: GroupBy,
85}
86
87#[derive(JsonSchema)]
88#[allow(dead_code)]
89pub(crate) struct FiltersSchema {
90    user: UserFilters,
91    style: StyleFilters,
92    collection: CollectionFilters,
93    item_filter_choices: crate::ItemFilterChoices,
94}
95
96#[derive(JsonSchema)]
97#[allow(dead_code)]
98pub(crate) struct AuthJsonSchema {
99    user: crate::User,
100    user_summary: crate::UserSummary,
101    authenticated_user: crate::AuthenticatedUser,
102    google_credentials: crate::GoogleCredentials,
103    microsoft_credentials: crate::MicrosoftCredentials,
104    group: Group,
105    group_summary: GroupSummary,
106    create_group: CreateGroup,
107    update_group: UpdateGroup,
108    create_user: CreateUser,
109    update_user: UpdateUser,
110    update_own_user: UpdateOwnUser,
111}