Skip to content

Commit

Permalink
feat: make Resolved{FileLine, FileSpan, Pos, Span} eq, hash and frozen
Browse files Browse the repository at this point in the history
  • Loading branch information
xen0n committed Jul 3, 2024
1 parent 7080ab0 commit 0f257bf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 56 deletions.
68 changes: 12 additions & 56 deletions src/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ impl<'py> FromPyObject<'py> for PyPos {
}
}

#[pyclass(module = "xingque", name = "ResolvedPos", frozen)]
#[pyclass(module = "xingque", name = "ResolvedPos", eq, hash, frozen)]
#[repr(transparent)]
#[derive(PartialEq, Eq, Hash, Clone)]
pub(crate) struct PyResolvedPos(ResolvedPos);

impl From<ResolvedPos> for PyResolvedPos {
Expand Down Expand Up @@ -86,15 +88,6 @@ impl PyResolvedPos {
Ok(me.repr(Some(Cow::Owned(class_name))))
}

fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
// TODO: handle Tuple[int, int]
if let Ok(other) = other.downcast::<PyResolvedPos>() {
self.0 == other.borrow().0
} else {
false
}
}

#[getter]
fn line(&self) -> usize {
self.0.line
Expand All @@ -106,7 +99,9 @@ impl PyResolvedPos {
}
}

#[pyclass(module = "xingque", name = "ResolvedSpan", frozen)]
#[pyclass(module = "xingque", name = "ResolvedSpan", eq, hash, frozen)]
#[repr(transparent)]
#[derive(PartialEq, Eq, Hash, Clone)]
pub(crate) struct PyResolvedSpan(ResolvedSpan);

impl From<ResolvedSpan> for PyResolvedSpan {
Expand Down Expand Up @@ -137,15 +132,6 @@ impl PyResolvedSpan {
))
}

fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
// TODO: handle Tuple[int, int]
if let Ok(other) = other.downcast::<PyResolvedSpan>() {
self.0 == other.borrow().0
} else {
false
}
}

#[getter]
fn begin(&self) -> PyResolvedPos {
self.0.begin.into()
Expand Down Expand Up @@ -366,7 +352,9 @@ impl PyFileSpan {
}
}

#[pyclass(module = "xingque", name = "ResolvedFileLine")]
#[pyclass(module = "xingque", name = "ResolvedFileLine", eq, hash, frozen)]
#[repr(transparent)]
#[derive(PartialEq, Eq, Hash, Clone)]
pub(crate) struct PyResolvedFileLine(ResolvedFileLine);

impl From<ResolvedFileLine> for PyResolvedFileLine {
Expand All @@ -391,35 +379,20 @@ impl PyResolvedFileLine {
))
}

fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
match other.downcast::<PyResolvedFileLine>() {
Ok(other) => self.0 == other.borrow().0,
Err(_) => false,
}
}

#[getter]
fn get_file(&self) -> &str {
&self.0.file
}

#[setter]
fn set_file(&mut self, x: String) {
self.0.file = x;
}

#[getter]
fn get_line(&self) -> usize {
self.0.line
}

#[setter]
fn set_line(&mut self, x: usize) {
self.0.line = x;
}
}

#[pyclass(module = "xingque", name = "ResolvedFileSpan")]
#[pyclass(module = "xingque", name = "ResolvedFileSpan", eq, hash, frozen)]
#[repr(transparent)]
#[derive(PartialEq, Eq, Hash, Clone)]
pub(crate) struct PyResolvedFileSpan(ResolvedFileSpan);

impl From<ResolvedFileSpan> for PyResolvedFileSpan {
Expand All @@ -444,33 +417,16 @@ impl PyResolvedFileSpan {
))
}

fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
match other.downcast::<PyResolvedFileSpan>() {
Ok(other) => self.0 == other.borrow().0,
Err(_) => false,
}
}

#[getter]
fn get_file(&self) -> &str {
&self.0.file
}

#[setter]
fn set_file(&mut self, x: String) {
self.0.file = x;
}

#[getter]
fn get_span(&self) -> PyResolvedSpan {
self.0.span.into()
}

#[setter]
fn set_span(&mut self, x: &PyResolvedSpan) {
self.0.span = x.0;
}

fn begin_file_line(&self) -> PyResolvedFileLine {
self.0.begin_file_line().into()
}
Expand Down
5 changes: 5 additions & 0 deletions xingque.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ class ResolvedFileLine:
line: int
def __init__(self, file: str, line: int) -> None: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...

class ResolvedFileSpan:
file: str
span: ResolvedSpan
def __init__(self, file: str, span: ResolvedSpan) -> None: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...
def begin_file_line(self) -> ResolvedFileLine: ...

class ResolvedPos:
def __init__(self, line: int, column: int) -> None: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...
@property
def line(self) -> int: ...
@property
Expand All @@ -67,6 +71,7 @@ class ResolvedPos:
class ResolvedSpan:
def __init__(self, begin: ResolvedPos, end: ResolvedPos) -> None: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...
@property
def begin(self) -> ResolvedPos: ...
@property
Expand Down

0 comments on commit 0f257bf

Please sign in to comment.