Coverage for src / lexigram / contracts / domain / services.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-08-19 05:41 +0800

1"""Domain service and policy contracts.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING, Any, Generic, Protocol, TypeVar, runtime_checkable 

6 

7if TYPE_CHECKING: 

8 from lexigram.contracts.core.result import Result 

9 

10T = TypeVar("T") 

11T_co = TypeVar("T_co", covariant=True) 

12 

13 

14@runtime_checkable 

15class DomainServiceProtocol(Protocol): 

16 """Protocol for domain services. 

17 

18 Domain services encapsulate business operations that don't naturally 

19 belong to a single entity or value object. They should be stateless. 

20 """ 

21 

22 

23class PolicyViolationProtocol(Protocol): 

24 """Describes why a policy was violated.""" 

25 

26 code: str 

27 message: str 

28 details: dict[str, Any] 

29 

30 

31@runtime_checkable 

32class PolicyProtocol(Protocol, Generic[T]): 

33 """Protocol for domain policies. 

34 

35 A policy evaluates a domain object or operation and returns 

36 a Result indicating whether the policy is satisfied. 

37 """ 

38 

39 async def evaluate(self, subject: T) -> Result[T, PolicyViolationProtocol]: 

40 """Evaluate the policy against the subject.""" 

41 ... 

42 

43 

44__all__ = [ 

45 "DomainServiceProtocol", 

46 "PolicyProtocol", 

47 "PolicyViolationProtocol", 

48 "T", 

49 "T_co", 

50]