use pyo3::prelude::*;
use tk::Token;

#[pyclass(module = "tokenizers", name = "Token", frozen)]
#[derive(Clone)]
pub struct PyToken {
    token: Token,
}
impl From<Token> for PyToken {
    fn from(token: Token) -> Self {
        Self { token }
    }
}
impl From<PyToken> for Token {
    fn from(token: PyToken) -> Self {
        token.token
    }
}

#[pymethods]
impl PyToken {
    /// Create a token from id, string value and byte offsets
    #[new]
    #[pyo3(
        signature = (id, value, offsets),
        text_signature = "(self, id, value, offsets)"
    )]
    fn new(id: u32, value: String, offsets: (usize, usize)) -> PyToken {
        Token::new(id, value, offsets).into()
    }

    #[getter]
    fn get_id(&self) -> u32 {
        self.token.id
    }

    #[getter]
    fn get_value(&self) -> &str {
        &self.token.value
    }

    #[getter]
    fn get_offsets(&self) -> (usize, usize) {
        self.token.offsets
    }

    fn as_tuple(&self) -> (u32, &str, (usize, usize)) {
        (self.token.id, &self.token.value, self.token.offsets)
    }
}
