@@ -5,6 +5,7 @@ use crate::fields::{
55} ;
66use crate :: rows:: { new_cell_builder, Cell } ;
77
8+ use crate :: error:: DatabaseError ;
89use crate :: template:: entity:: CELL_DATA ;
910use crate :: template:: util:: TypeOptionCellData ;
1011use collab:: util:: AnyMapExt ;
@@ -13,6 +14,7 @@ use serde_json::{json, Value};
1314use serde_repr:: Serialize_repr ;
1415use std:: fmt:: { Display , Formatter } ;
1516use std:: path:: Path ;
17+ use std:: str:: FromStr ;
1618use std:: sync:: Arc ;
1719use yrs:: Any ;
1820
@@ -43,13 +45,10 @@ impl TypeOptionCellReader for MediaTypeOption {
4345 }
4446
4547 fn convert_raw_cell_data ( & self , text : & str ) -> String {
46- let data = MediaCellData :: from ( text. to_string ( ) ) ;
47- data
48- . files
49- . into_iter ( )
50- . map ( |file| file. name )
51- . collect :: < Vec < _ > > ( )
52- . join ( ", " )
48+ match serde_json:: from_str :: < MediaCellData > ( text) {
49+ Ok ( value) => value. to_string ( ) ,
50+ Err ( _) => "" . to_string ( ) ,
51+ }
5352 }
5453}
5554
@@ -131,26 +130,11 @@ impl From<&Cell> for MediaCellData {
131130impl From < MediaCellData > for Cell {
132131 fn from ( value : MediaCellData ) -> Self {
133132 let mut cell = new_cell_builder ( FieldType :: Media ) ;
134- cell. insert ( CELL_DATA . into ( ) , value. to_string ( ) . into ( ) ) ;
133+ cell. insert ( CELL_DATA . into ( ) , value. into ( ) ) ;
135134 cell
136135 }
137136}
138137
139- impl From < String > for MediaCellData {
140- fn from ( s : String ) -> Self {
141- if s. is_empty ( ) {
142- return MediaCellData { files : vec ! [ ] } ;
143- }
144-
145- let files = s
146- . split ( ", " )
147- . map ( |file : & str | serde_json:: from_str :: < MediaFile > ( file) . unwrap_or_default ( ) )
148- . collect :: < Vec < _ > > ( ) ;
149-
150- MediaCellData { files }
151- }
152- }
153-
154138impl ToString for MediaCellData {
155139 fn to_string ( & self ) -> String {
156140 self
@@ -162,6 +146,22 @@ impl ToString for MediaCellData {
162146 }
163147}
164148
149+ impl FromStr for MediaCellData {
150+ type Err = DatabaseError ;
151+
152+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
153+ if s. is_empty ( ) {
154+ return Ok ( MediaCellData { files : vec ! [ ] } ) ;
155+ }
156+ let files = s
157+ . split ( ", " )
158+ . map ( |file : & str | serde_json:: from_str :: < MediaFile > ( file) . unwrap_or_default ( ) )
159+ . collect :: < Vec < _ > > ( ) ;
160+
161+ Ok ( MediaCellData { files } )
162+ }
163+ }
164+
165165#[ derive( Clone , Debug , Default , PartialEq , Eq , Serialize , Deserialize ) ]
166166pub struct MediaFile {
167167 pub id : String ,
@@ -366,6 +366,44 @@ impl<'de> Deserialize<'de> for MediaUploadType {
366366mod tests {
367367 use super :: * ;
368368 use serde_json:: json;
369+ #[ test]
370+ fn test_is_cell_empty ( ) {
371+ let empty_media_cell_data = MediaCellData { files : vec ! [ ] } ;
372+ assert ! ( empty_media_cell_data. is_cell_empty( ) ) ;
373+
374+ let non_empty_media_cell_data = MediaCellData {
375+ files : vec ! [ MediaFile :: new(
376+ "file1.jpg" . to_string( ) ,
377+ "http://example.com/file1.jpg" . to_string( ) ,
378+ MediaUploadType :: Local ,
379+ MediaFileType :: Image ,
380+ ) ] ,
381+ } ;
382+ assert ! ( !non_empty_media_cell_data. is_cell_empty( ) ) ;
383+ }
384+
385+ #[ test]
386+ fn test_media_file_rename ( ) {
387+ let original = MediaFile :: new (
388+ "original_name.jpg" . to_string ( ) ,
389+ "http://example.com/file.jpg" . to_string ( ) ,
390+ MediaUploadType :: Local ,
391+ MediaFileType :: Image ,
392+ ) ;
393+
394+ let renamed = original. rename ( "new_name.jpg" . to_string ( ) ) ;
395+ assert_eq ! ( renamed. name, "new_name.jpg" ) ;
396+ assert_eq ! ( renamed. url, original. url) ;
397+ assert_eq ! ( renamed. upload_type, original. upload_type) ;
398+ assert_eq ! ( renamed. file_type, original. file_type) ;
399+ }
400+
401+ #[ test]
402+ fn test_invalid_json_deserialization ( ) {
403+ let invalid_json = json ! ( "InvalidType" ) ;
404+ assert ! ( serde_json:: from_value:: <MediaUploadType >( invalid_json. clone( ) ) . is_err( ) ) ;
405+ assert ! ( serde_json:: from_value:: <MediaFileType >( invalid_json) . is_err( ) ) ;
406+ }
369407
370408 #[ test]
371409 fn test_media_cell_data_to_string ( ) {
@@ -485,4 +523,90 @@ mod tests {
485523 MediaFileType :: Text
486524 ) ;
487525 }
526+
527+ #[ test]
528+ fn test_convert_raw_cell_data ( ) {
529+ let media_type_option = MediaTypeOption :: default ( ) ;
530+
531+ // Test with valid JSON data
532+ let valid_data =
533+ r#"{"files":[{"id":"1","name":"file1","url":"url1","upload_type":0,"file_type":1}]}"# ;
534+ assert_eq ! (
535+ media_type_option. convert_raw_cell_data( valid_data) ,
536+ "file1" . to_string( )
537+ ) ;
538+
539+ // Test with invalid JSON data
540+ let invalid_data = "invalid_json" ;
541+ assert_eq ! (
542+ media_type_option. convert_raw_cell_data( invalid_data) ,
543+ "" . to_string( )
544+ ) ;
545+
546+ // Test with empty string
547+ let empty_data = "" ;
548+ assert_eq ! (
549+ media_type_option. convert_raw_cell_data( empty_data) ,
550+ "" . to_string( )
551+ ) ;
552+
553+ // Test with valid JSON but missing "files" field
554+ let missing_field_data = r#"{"other_field":[{"id":"1"}]}"# ;
555+ assert_eq ! (
556+ media_type_option. convert_raw_cell_data( missing_field_data) ,
557+ "" . to_string( )
558+ ) ;
559+
560+ // Test with valid JSON but incorrect structure
561+ let incorrect_structure_data = r#"{"files":"not_an_array"}"# ;
562+ assert_eq ! (
563+ media_type_option. convert_raw_cell_data( incorrect_structure_data) ,
564+ "" . to_string( )
565+ ) ;
566+ }
567+
568+ #[ test]
569+ fn test_numeric_cell_conversion ( ) {
570+ let mut cell = new_cell_builder ( FieldType :: Media ) ;
571+ cell. insert ( CELL_DATA . into ( ) , "123.45" . to_string ( ) . into ( ) ) ;
572+
573+ let media_type_option = MediaTypeOption :: default ( ) ;
574+ let numeric_value = media_type_option. numeric_cell ( & cell) ;
575+
576+ assert_eq ! ( numeric_value, Some ( 123.45 ) ) ;
577+ }
578+
579+ #[ test]
580+ fn test_media_cell_data_to_and_from_cell ( ) {
581+ // Create MediaCellData with sample MediaFile entries
582+ let media_file_1 = MediaFile :: new (
583+ "file1.jpg" . to_string ( ) ,
584+ "http://example.com/file1.jpg" . to_string ( ) ,
585+ MediaUploadType :: Local ,
586+ MediaFileType :: Image ,
587+ ) ;
588+ let media_file_2 = MediaFile :: new (
589+ "file2.png" . to_string ( ) ,
590+ "http://example.com/file2.png" . to_string ( ) ,
591+ MediaUploadType :: Cloud ,
592+ MediaFileType :: Image ,
593+ ) ;
594+
595+ let media_cell_data = MediaCellData {
596+ files : vec ! [ media_file_1. clone( ) , media_file_2. clone( ) ] ,
597+ } ;
598+
599+ // Convert MediaCellData to a Cell
600+ let cell: Cell = media_cell_data. clone ( ) . into ( ) ;
601+
602+ // Assert the Cell has the correct field type and content
603+ let cell_data = MediaCellData :: from ( & cell) ;
604+ cell_data
605+ . files
606+ . iter ( )
607+ . zip ( media_cell_data. files . iter ( ) )
608+ . for_each ( |( a, b) | {
609+ assert_eq ! ( a, b) ;
610+ } ) ;
611+ }
488612}
0 commit comments