import.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. use crate::entities::parser::empty_str::NotEmptyStr;
  2. use crate::entities::ViewLayoutPB;
  3. use crate::share::{ImportParams, ImportType};
  4. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  5. use flowy_error::FlowyError;
  6. #[derive(Clone, Debug, ProtoBuf_Enum)]
  7. pub enum ImportTypePB {
  8. HistoryDocument = 0,
  9. HistoryDatabase = 1,
  10. RawDatabase = 2,
  11. CSV = 3,
  12. }
  13. impl From<ImportTypePB> for ImportType {
  14. fn from(pb: ImportTypePB) -> Self {
  15. match pb {
  16. ImportTypePB::HistoryDocument => ImportType::HistoryDocument,
  17. ImportTypePB::HistoryDatabase => ImportType::HistoryDatabase,
  18. ImportTypePB::RawDatabase => ImportType::RawDatabase,
  19. ImportTypePB::CSV => ImportType::CSV,
  20. }
  21. }
  22. }
  23. impl Default for ImportTypePB {
  24. fn default() -> Self {
  25. Self::HistoryDocument
  26. }
  27. }
  28. #[derive(Clone, Debug, ProtoBuf, Default)]
  29. pub struct ImportPB {
  30. #[pb(index = 1)]
  31. pub parent_view_id: String,
  32. #[pb(index = 2)]
  33. pub name: String,
  34. #[pb(index = 3, one_of)]
  35. pub data: Option<Vec<u8>>,
  36. #[pb(index = 4, one_of)]
  37. pub file_path: Option<String>,
  38. #[pb(index = 5)]
  39. pub view_layout: ViewLayoutPB,
  40. #[pb(index = 6)]
  41. pub import_type: ImportTypePB,
  42. }
  43. impl TryInto<ImportParams> for ImportPB {
  44. type Error = FlowyError;
  45. fn try_into(self) -> Result<ImportParams, Self::Error> {
  46. let parent_view_id = NotEmptyStr::parse(self.parent_view_id)
  47. .map_err(|_| FlowyError::invalid_view_id())?
  48. .0;
  49. let name = if self.name.is_empty() {
  50. "Untitled".to_string()
  51. } else {
  52. self.name
  53. };
  54. let file_path = match self.file_path {
  55. None => None,
  56. Some(file_path) => Some(
  57. NotEmptyStr::parse(file_path)
  58. .map_err(|_| FlowyError::invalid_data().context("The import file path is empty"))?
  59. .0,
  60. ),
  61. };
  62. Ok(ImportParams {
  63. parent_view_id,
  64. name,
  65. data: self.data,
  66. file_path,
  67. view_layout: self.view_layout.into(),
  68. import_type: self.import_type.into(),
  69. })
  70. }
  71. }