from_serialized.rs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #![allow(clippy::type_complexity)]
  2. use super::new_validation;
  3. use node_runtime::{
  4. forum::{Category, CategoryId, Post, Thread},
  5. AccountId, BlockNumber, ForumConfig, Moment, PostId, ThreadId,
  6. };
  7. use serde::Deserialize;
  8. use serde_json::Result;
  9. #[derive(Deserialize)]
  10. struct ForumData {
  11. categories: Vec<(CategoryId, Category<BlockNumber, Moment, AccountId>)>,
  12. posts: Vec<(
  13. PostId,
  14. Post<BlockNumber, Moment, AccountId, ThreadId, PostId>,
  15. )>,
  16. threads: Vec<(ThreadId, Thread<BlockNumber, Moment, AccountId, ThreadId>)>,
  17. }
  18. fn parse_forum_json() -> Result<ForumData> {
  19. let data = include_str!("../../res/forum_data_empty.json");
  20. serde_json::from_str(data)
  21. }
  22. pub fn create(forum_sudo: AccountId) -> ForumConfig {
  23. let forum_data = parse_forum_json().expect("failed loading forum data");
  24. let next_category_id: CategoryId = forum_data
  25. .categories
  26. .last()
  27. .map_or(1, |category| category.0 + 1);
  28. let next_thread_id: ThreadId = forum_data.threads.last().map_or(1, |thread| thread.0 + 1);
  29. let next_post_id: PostId = forum_data.posts.last().map_or(1, |post| post.0 + 1);
  30. ForumConfig {
  31. category_by_id: forum_data.categories,
  32. thread_by_id: forum_data.threads,
  33. post_by_id: forum_data.posts,
  34. next_category_id,
  35. next_thread_id,
  36. next_post_id,
  37. category_title_constraint: new_validation(10, 90),
  38. category_description_constraint: new_validation(10, 490),
  39. thread_title_constraint: new_validation(10, 90),
  40. post_text_constraint: new_validation(10, 990),
  41. thread_moderation_rationale_constraint: new_validation(10, 290),
  42. post_moderation_rationale_constraint: new_validation(10, 290),
  43. forum_sudo,
  44. }
  45. }