Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add eq and hash to marks
  • Loading branch information
andreaponti5 committed Apr 8, 2025
commit d3c6f6132dbf035bbff88afeac517e7d1103e557
44 changes: 43 additions & 1 deletion parse_document_model/marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,34 @@ class Color(BaseModel):
g: int
b: int

def __eq__(self, other: Any) -> bool:
return (isinstance(other, Color) and
self.id == other.id and
self.r == other.r and
self.g == other.g and
self.b == other.b)

def __hash__(self) -> int:
return hash((self.id, self.r, self.g, self.b))


class Font(BaseModel):
id: str
name: str
size: int

def __eq__(self, other: Any) -> bool:
return (isinstance(other, Font) and
self.id == other.id and
self.size == other.size and
self.name == other.name)

def __hash__(self) -> int:
return hash((self.id, self.size, self.name))


class Mark(BaseModel):
category: Literal['bold', 'italic', 'textStyle', 'link']
category: Literal['bold', 'italic', 'superscripted', 'serifed', 'monospaced', 'textStyle', 'link']

@model_validator(mode='before')
def check_details(self: Any) -> Any:
Expand All @@ -37,11 +56,34 @@ def check_details(self: Any) -> Any:
raise ValueError('textStyle should not be provided when type is link')
return self

def __eq__(self, other):
return isinstance(other, Mark) and self.category == other.category

def __hash__(self) -> int:
return hash(self.category)


class TextStyleMark(Mark):
color: Optional[Color] = None
font: Optional[Font] = None

def __eq__(self, other):
return (isinstance(other, TextStyleMark) and
self.category == other.category and
self.color == other.color and
self.font == other.font)

def __hash__(self) -> int:
return hash((self.category, self.color, self.font))


class UrlMark(Mark):
url: str

def __eq__(self, other):
return (isinstance(other, UrlMark) and
self.category == other.category and
self.url == other.url)

def __hash__(self) -> int:
return hash((self.category, self.url))