1use std::collections::BTreeMap;
4
5use crate::ids::{ProfessionId, UserName};
6use crate::wire::profession::{
7 WireProfessionActivity, WireProfessionContribution, WireProfessionDetail,
8 WireProfessionDisplay, WireProfessionLevel, WireProfessionLevelHistory, WireProfessionProgress,
9 WireProfessionRanking, WireProfessionRankingEntry, WireProfessionRarity, WireProfessionRecord,
10 WireProfessionRecordsCursor, WireProfessionRecordsPage, WireProfessionReward,
11 WireProfessionSettings, WireProfessionSummary, WirePublicProfessionProfile,
12};
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum ProfessionPrivacyPreset {
16 AllPublic,
17 LevelOnly,
18 PrimaryOnly,
19 SelfOnly,
20 FullyHidden,
21 Custom,
22}
23
24impl ProfessionPrivacyPreset {
25 pub fn as_str(&self) -> &'static str {
26 match self {
27 Self::AllPublic => "ALL_PUBLIC",
28 Self::LevelOnly => "LEVEL_ONLY",
29 Self::PrimaryOnly => "PRIMARY_ONLY",
30 Self::SelfOnly => "SELF_ONLY",
31 Self::FullyHidden => "FULLY_HIDDEN",
32 Self::Custom => "CUSTOM",
33 }
34 }
35}
36
37impl From<&str> for ProfessionPrivacyPreset {
38 fn from(value: &str) -> Self {
39 match value.trim() {
40 "ALL_PUBLIC" => Self::AllPublic,
41 "PRIMARY_ONLY" => Self::PrimaryOnly,
42 "SELF_ONLY" => Self::SelfOnly,
43 "FULLY_HIDDEN" => Self::FullyHidden,
44 "CUSTOM" => Self::Custom,
45 _ => Self::LevelOnly,
46 }
47 }
48}
49
50#[derive(Clone, Copy, Debug, PartialEq, Eq)]
51pub enum ProfessionProfilePosition {
52 HomeProfile,
53 UserCard,
54 ProfessionPage,
55 Ranking,
56 CompactMobile,
57}
58
59impl ProfessionProfilePosition {
60 pub fn as_str(&self) -> &'static str {
61 match self {
62 Self::HomeProfile => "homeProfile",
63 Self::UserCard => "userCard",
64 Self::ProfessionPage => "professionPage",
65 Self::Ranking => "ranking",
66 Self::CompactMobile => "compactMobile",
67 }
68 }
69}
70
71impl From<&str> for ProfessionProfilePosition {
72 fn from(value: &str) -> Self {
73 match value.trim() {
74 "userCard" => Self::UserCard,
75 "professionPage" => Self::ProfessionPage,
76 "ranking" => Self::Ranking,
77 "compactMobile" => Self::CompactMobile,
78 _ => Self::HomeProfile,
79 }
80 }
81}
82
83#[derive(Clone, Debug)]
84#[non_exhaustive]
85pub struct ProfessionSettings {
86 pub primary_profession_id: ProfessionId,
87 pub progress: Vec<ProfessionProgress>,
88 pub available_professions: Vec<ProfessionSummary>,
89 pub growth_guides: BTreeMap<String, Vec<String>>,
90 pub privacy_preset: ProfessionPrivacyPreset,
91 pub module_visibility: ProfessionModuleVisibility,
92}
93
94impl From<WireProfessionSettings> for ProfessionSettings {
95 fn from(w: WireProfessionSettings) -> Self {
96 let module_visibility = ProfessionModuleVisibility::from_json(&w.module_visibility_json);
97
98 Self {
99 primary_profession_id: w.primary_profession_id.into(),
100 progress: w.progress.into_iter().map(Into::into).collect(),
101 available_professions: w
102 .available_professions
103 .into_iter()
104 .map(Into::into)
105 .collect(),
106 growth_guides: w.growth_guides,
107 privacy_preset: ProfessionPrivacyPreset::from(w.privacy_preset.as_str()),
108 module_visibility,
109 }
110 }
111}
112
113#[derive(Clone, Debug)]
114#[non_exhaustive]
115pub struct ProfessionProgress {
116 pub profession_id: ProfessionId,
117 pub display_name: String,
118 pub profession_code: String,
119 pub short_name: String,
120 pub description: String,
121 pub level_code: String,
122 pub level_name: String,
123 pub level_description: String,
124 pub required_experience: i64,
125 pub total_experience: i64,
126}
127
128impl From<WireProfessionProgress> for ProfessionProgress {
129 fn from(w: WireProfessionProgress) -> Self {
130 Self {
131 profession_id: w.profession_id.into(),
132 display_name: w.display_name,
133 profession_code: w.profession_code,
134 short_name: w.short_name,
135 description: w.description,
136 level_code: w.level_code,
137 level_name: w.level_name,
138 level_description: w.level_description,
139 required_experience: w.required_experience,
140 total_experience: w.total_experience,
141 }
142 }
143}
144
145#[derive(Clone, Debug)]
146#[non_exhaustive]
147pub struct ProfessionSummary {
148 pub profession_id: ProfessionId,
149 pub display_name: String,
150 pub profession_code: String,
151 pub short_name: String,
152 pub code: String,
153 pub description: String,
154}
155
156impl From<WireProfessionSummary> for ProfessionSummary {
157 fn from(w: WireProfessionSummary) -> Self {
158 Self {
159 profession_id: w.profession_id.into(),
160 display_name: w.display_name,
161 profession_code: w.profession_code,
162 short_name: w.short_name,
163 code: w.code,
164 description: w.description,
165 }
166 }
167}
168
169#[derive(Clone, Debug, Default)]
170#[non_exhaustive]
171pub struct ProfessionModuleVisibility {
172 pub primary_profession: String,
173 pub all_professions: String,
174 pub experience: String,
175 pub rarity: String,
176 pub major_contributions: String,
177 pub contribution_stats: String,
178 pub activity_feed: String,
179 pub level_history: String,
180 pub rewards: String,
181 pub ranking: String,
182}
183
184impl ProfessionModuleVisibility {
185 pub fn all(level: impl Into<String>) -> Self {
186 let level = level.into();
187 Self {
188 primary_profession: level.clone(),
189 all_professions: level.clone(),
190 experience: level.clone(),
191 rarity: level.clone(),
192 major_contributions: level.clone(),
193 contribution_stats: level.clone(),
194 activity_feed: level.clone(),
195 level_history: level.clone(),
196 rewards: level.clone(),
197 ranking: level,
198 }
199 }
200
201 pub fn from_json(json: &str) -> Self {
202 serde_json::from_str::<std::collections::HashMap<String, String>>(json)
203 .map(|map| {
204 let get = |k: &str| map.get(k).cloned().unwrap_or_default();
205 Self {
206 primary_profession: get("primaryProfession"),
207 all_professions: get("allProfessions"),
208 experience: get("experience"),
209 rarity: get("rarity"),
210 major_contributions: get("majorContributions"),
211 contribution_stats: get("contributionStats"),
212 activity_feed: get("activityFeed"),
213 level_history: get("levelHistory"),
214 rewards: get("rewards"),
215 ranking: get("ranking"),
216 }
217 })
218 .unwrap_or_default()
219 }
220}
221
222#[derive(Clone, Debug)]
223#[non_exhaustive]
224pub struct ProfessionDetail {
225 pub profession: ProfessionDisplay,
226 pub levels: Vec<ProfessionLevel>,
227 pub rarity: ProfessionRarity,
228 pub contributions: Vec<ProfessionContribution>,
229}
230
231impl From<WireProfessionDetail> for ProfessionDetail {
232 fn from(w: WireProfessionDetail) -> Self {
233 Self {
234 profession: w.profession.into(),
235 levels: w.levels.into_iter().map(Into::into).collect(),
236 rarity: w.rarity.into(),
237 contributions: w.contributions.into_iter().map(Into::into).collect(),
238 }
239 }
240}
241
242#[derive(Clone, Debug, Default)]
243#[non_exhaustive]
244pub struct ProfessionDisplay {
245 pub profession_id: ProfessionId,
246 pub display_name: String,
247 pub level_code: String,
248 pub level_name: String,
249 pub total_experience: i64,
250}
251
252impl From<WireProfessionDisplay> for ProfessionDisplay {
253 fn from(w: WireProfessionDisplay) -> Self {
254 Self {
255 profession_id: w.profession_id.into(),
256 display_name: w.display_name,
257 level_code: w.level_code,
258 level_name: w.level_name,
259 total_experience: w.total_experience,
260 }
261 }
262}
263
264#[derive(Clone, Debug)]
265#[non_exhaustive]
266pub struct ProfessionLevel {
267 pub code: String,
268 pub display_name: String,
269 pub required_experience: i64,
270}
271
272impl From<WireProfessionLevel> for ProfessionLevel {
273 fn from(w: WireProfessionLevel) -> Self {
274 Self {
275 code: w.code,
276 display_name: w.display_name,
277 required_experience: w.required_experience,
278 }
279 }
280}
281
282#[derive(Clone, Debug, Default)]
283#[non_exhaustive]
284pub struct ProfessionRarity {
285 pub rank: u32,
286 pub surpassed_percent: f64,
287 pub total_users: u32,
288}
289
290impl From<WireProfessionRarity> for ProfessionRarity {
291 fn from(w: WireProfessionRarity) -> Self {
292 Self {
293 rank: w.rank,
294 surpassed_percent: w.surpassed_percent,
295 total_users: w.total_users,
296 }
297 }
298}
299
300#[derive(Clone, Debug)]
301#[non_exhaustive]
302pub struct ProfessionContribution {
303 pub profession_id: ProfessionId,
304 pub action_code: String,
305 pub source_label: String,
306 pub event_count: u32,
307 pub experience_sum: i64,
308 pub last_occurred_at: i64,
309}
310
311impl From<WireProfessionContribution> for ProfessionContribution {
312 fn from(w: WireProfessionContribution) -> Self {
313 Self {
314 profession_id: w.profession_id.into(),
315 action_code: w.action_code,
316 source_label: w.source_label,
317 event_count: w.event_count,
318 experience_sum: w.experience_sum,
319 last_occurred_at: w.last_occurred_at,
320 }
321 }
322}
323
324#[derive(Clone, Debug)]
325#[non_exhaustive]
326pub struct ProfessionRecordsPage {
327 pub records: Vec<ProfessionRecord>,
328 pub next_cursor: Option<ProfessionRecordsCursor>,
329}
330
331impl From<WireProfessionRecordsPage> for ProfessionRecordsPage {
332 fn from(w: WireProfessionRecordsPage) -> Self {
333 Self {
334 records: w.records.into_iter().map(Into::into).collect(),
335 next_cursor: w.next_cursor.map(Into::into),
336 }
337 }
338}
339
340#[derive(Clone, Debug)]
341#[non_exhaustive]
342pub struct ProfessionRecord {
343 pub effect_id: String,
344 pub action_code: String,
345 pub source_label: String,
346 pub experience_delta: i64,
347 pub before_experience: i64,
348 pub after_experience: i64,
349 pub occurred_at: i64,
350}
351
352impl From<WireProfessionRecord> for ProfessionRecord {
353 fn from(w: WireProfessionRecord) -> Self {
354 Self {
355 effect_id: w.effect_id,
356 action_code: w.action_code,
357 source_label: w.source_label,
358 experience_delta: w.experience_delta,
359 before_experience: w.before_experience,
360 after_experience: w.after_experience,
361 occurred_at: w.occurred_at,
362 }
363 }
364}
365
366#[derive(Clone, Debug)]
367#[non_exhaustive]
368pub struct ProfessionRecordsCursor {
369 pub before_occurred_at: i64,
370 pub before_effect_id: String,
371}
372
373impl From<WireProfessionRecordsCursor> for ProfessionRecordsCursor {
374 fn from(w: WireProfessionRecordsCursor) -> Self {
375 Self {
376 before_occurred_at: w.before_occurred_at,
377 before_effect_id: w.before_effect_id,
378 }
379 }
380}
381
382#[derive(Clone, Debug)]
383#[non_exhaustive]
384pub struct PublicProfessionProfile {
385 pub primary_profession: Option<ProfessionDisplay>,
386 pub professions: Vec<ProfessionDisplay>,
387 pub major_contributions: Vec<ProfessionContribution>,
388 pub contribution_stats: Vec<ProfessionContribution>,
389 pub activity_feed: Vec<ProfessionActivity>,
390 pub level_history: Vec<ProfessionLevelHistory>,
391 pub rewards: Vec<ProfessionReward>,
392 pub ranking: Vec<ProfessionRankingEntry>,
393}
394
395impl From<WirePublicProfessionProfile> for PublicProfessionProfile {
396 fn from(w: WirePublicProfessionProfile) -> Self {
397 Self {
398 primary_profession: w.primary_profession.map(Into::into),
399 professions: w.professions.into_iter().map(Into::into).collect(),
400 major_contributions: w.major_contributions.into_iter().map(Into::into).collect(),
401 contribution_stats: w.contribution_stats.into_iter().map(Into::into).collect(),
402 activity_feed: w.activity_feed.into_iter().map(Into::into).collect(),
403 level_history: w.level_history.into_iter().map(Into::into).collect(),
404 rewards: w.rewards.into_iter().map(Into::into).collect(),
405 ranking: w.ranking.into_iter().map(Into::into).collect(),
406 }
407 }
408}
409
410#[derive(Clone, Debug)]
411#[non_exhaustive]
412pub struct ProfessionActivity {
413 pub profession_id: ProfessionId,
414 pub action_code: String,
415 pub source_label: String,
416 pub experience_delta: i64,
417 pub occurred_at: i64,
418}
419
420impl From<WireProfessionActivity> for ProfessionActivity {
421 fn from(w: WireProfessionActivity) -> Self {
422 Self {
423 profession_id: w.profession_id.into(),
424 action_code: w.action_code,
425 source_label: w.source_label,
426 experience_delta: w.experience_delta,
427 occurred_at: w.occurred_at,
428 }
429 }
430}
431
432#[derive(Clone, Debug)]
433#[non_exhaustive]
434pub struct ProfessionLevelHistory {
435 pub profession_id: ProfessionId,
436 pub before_level_code: String,
437 pub after_level_code: String,
438 pub occurred_at: i64,
439}
440
441impl From<WireProfessionLevelHistory> for ProfessionLevelHistory {
442 fn from(w: WireProfessionLevelHistory) -> Self {
443 Self {
444 profession_id: w.profession_id.into(),
445 before_level_code: w.before_level_code,
446 after_level_code: w.after_level_code,
447 occurred_at: w.occurred_at,
448 }
449 }
450}
451
452#[derive(Clone, Debug)]
453#[non_exhaustive]
454pub struct ProfessionReward {
455 pub reward_type: String,
456 pub label: String,
457 pub granted_at: i64,
458}
459
460impl From<WireProfessionReward> for ProfessionReward {
461 fn from(w: WireProfessionReward) -> Self {
462 Self {
463 reward_type: w.reward_type,
464 label: w.label,
465 granted_at: w.granted_at,
466 }
467 }
468}
469
470#[derive(Clone, Debug)]
471#[non_exhaustive]
472pub struct ProfessionRanking {
473 pub selected_profession: ProfessionSummary,
474 pub professions: Vec<ProfessionSummary>,
475 pub entries: Vec<ProfessionRankingEntry>,
476}
477
478impl From<WireProfessionRanking> for ProfessionRanking {
479 fn from(w: WireProfessionRanking) -> Self {
480 Self {
481 selected_profession: w.selected_profession.into(),
482 professions: w.professions.into_iter().map(Into::into).collect(),
483 entries: w.entries.into_iter().map(Into::into).collect(),
484 }
485 }
486}
487
488#[derive(Clone, Debug)]
489#[non_exhaustive]
490pub struct ProfessionRankingEntry {
491 pub rank: u32,
492 pub user_name: UserName,
493 pub user_nickname: String,
494 pub user_avatar_url48: String,
495 pub profession_id: ProfessionId,
496 pub profession_name: String,
497 pub level_name: String,
498 pub primary_color: String,
499 pub icon_url: String,
500 pub total_experience: Option<i64>,
501 pub surpassed_percent: f64,
502}
503
504impl From<WireProfessionRankingEntry> for ProfessionRankingEntry {
505 fn from(w: WireProfessionRankingEntry) -> Self {
506 Self {
507 rank: w.rank,
508 user_name: w.user_name.into(),
509 user_nickname: w.user_nickname,
510 user_avatar_url48: w.user_avatar_url48,
511 profession_id: w.profession_id.into(),
512 profession_name: w.profession_name,
513 level_name: w.level_name,
514 primary_color: w.primary_color,
515 icon_url: w.icon_url,
516 total_experience: w.total_experience,
517 surpassed_percent: w.surpassed_percent,
518 }
519 }
520}