File size: 4,204 Bytes
55e29e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""ProcessingResult value object for audio processing pipeline results."""

from dataclasses import dataclass
from typing import Optional
from .text_content import TextContent
from .audio_content import AudioContent


@dataclass(frozen=True)
class ProcessingResult:
    """Value object representing the result of audio processing pipeline."""
    
    success: bool
    original_text: Optional[TextContent]
    translated_text: Optional[TextContent]
    audio_output: Optional[AudioContent]
    error_message: Optional[str]
    processing_time: float
    
    def __post_init__(self):
        """Validate processing result after initialization."""
        self._validate()
    
    def _validate(self):
        """Validate processing result properties."""
        if not isinstance(self.success, bool):
            raise TypeError("Success must be a boolean")
        
        if self.original_text is not None and not isinstance(self.original_text, TextContent):
            raise TypeError("Original text must be a TextContent instance or None")
        
        if self.translated_text is not None and not isinstance(self.translated_text, TextContent):
            raise TypeError("Translated text must be a TextContent instance or None")
        
        if self.audio_output is not None and not isinstance(self.audio_output, AudioContent):
            raise TypeError("Audio output must be an AudioContent instance or None")
        
        if self.error_message is not None and not isinstance(self.error_message, str):
            raise TypeError("Error message must be a string or None")
        
        if not isinstance(self.processing_time, (int, float)):
            raise TypeError("Processing time must be a number")
        
        if self.processing_time < 0:
            raise ValueError("Processing time cannot be negative")
        
        # Business rule validations
        if self.success:
            if self.error_message is not None:
                raise ValueError("Successful result cannot have an error message")
            if self.original_text is None:
                raise ValueError("Successful result must have original text")
        else:
            if self.error_message is None or not self.error_message.strip():
                raise ValueError("Failed result must have a non-empty error message")
    
    @property
    def has_translation(self) -> bool:
        """Check if the result includes translated text."""
        return self.translated_text is not None
    
    @property
    def has_audio_output(self) -> bool:
        """Check if the result includes audio output."""
        return self.audio_output is not None
    
    @property
    def is_complete_pipeline(self) -> bool:
        """Check if the result represents a complete pipeline execution."""
        return (self.success and 
                self.original_text is not None and 
                self.translated_text is not None and 
                self.audio_output is not None)
    
    @classmethod
    def success_result(
        cls,
        original_text: TextContent,
        translated_text: Optional[TextContent] = None,
        audio_output: Optional[AudioContent] = None,
        processing_time: float = 0.0
    ) -> 'ProcessingResult':
        """Create a successful processing result."""
        return cls(
            success=True,
            original_text=original_text,
            translated_text=translated_text,
            audio_output=audio_output,
            error_message=None,
            processing_time=processing_time
        )
    
    @classmethod
    def failure_result(
        cls,
        error_message: str,
        processing_time: float = 0.0,
        original_text: Optional[TextContent] = None,
        translated_text: Optional[TextContent] = None,
        audio_output: Optional[AudioContent] = None
    ) -> 'ProcessingResult':
        """Create a failed processing result."""
        return cls(
            success=False,
            original_text=original_text,
            translated_text=translated_text,
            audio_output=audio_output,
            error_message=error_message,
            processing_time=processing_time
        )