entities.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. use crate::entities::RowPB;
  2. #[derive(Clone, PartialEq, Eq)]
  3. pub struct Group {
  4. pub id: String,
  5. pub field_id: String,
  6. pub name: String,
  7. pub(crate) rows: Vec<RowPB>,
  8. /// [content] is used to determine which group the cell belongs to.
  9. pub content: String,
  10. }
  11. impl Group {
  12. pub fn new(id: String, field_id: String, name: String, content: String) -> Self {
  13. Self {
  14. id,
  15. field_id,
  16. name,
  17. rows: vec![],
  18. content,
  19. }
  20. }
  21. pub fn contains_row(&self, row_id: &str) -> bool {
  22. self.rows.iter().any(|row| row.id == row_id)
  23. }
  24. pub fn remove_row(&mut self, row_id: &str) {
  25. match self.rows.iter().position(|row| row.id == row_id) {
  26. None => {}
  27. Some(pos) => {
  28. self.rows.remove(pos);
  29. }
  30. }
  31. }
  32. pub fn add_row(&mut self, row_pb: RowPB) {
  33. match self.rows.iter().find(|row| row.id == row_pb.id) {
  34. None => {
  35. self.rows.push(row_pb);
  36. }
  37. Some(_) => {}
  38. }
  39. }
  40. pub fn insert_row(&mut self, index: usize, row_pb: RowPB) {
  41. if index < self.rows.len() {
  42. self.rows.insert(index, row_pb);
  43. } else {
  44. tracing::error!("Insert row index:{} beyond the bounds:{},", index, self.rows.len());
  45. }
  46. }
  47. pub fn index_of_row(&self, row_id: &str) -> Option<usize> {
  48. self.rows.iter().position(|row| row.id == row_id)
  49. }
  50. pub fn number_of_row(&self) -> usize {
  51. self.rows.len()
  52. }
  53. }