@@ -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
@@ -144,6 +146,22 @@ impl ToString for MediaCellData {
144146 }
145147}
146148
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+
147165#[ derive( Clone , Debug , Default , PartialEq , Eq , Serialize , Deserialize ) ]
148166pub struct MediaFile {
149167 pub id : String ,
@@ -348,6 +366,44 @@ impl<'de> Deserialize<'de> for MediaUploadType {
348366mod tests {
349367 use super :: * ;
350368 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+ }
351407
352408 #[ test]
353409 fn test_media_cell_data_to_string ( ) {
@@ -467,4 +523,56 @@ mod tests {
467523 MediaFileType :: Text
468524 ) ;
469525 }
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+ }
470578}
0 commit comments