Skip to content

Commit 5f03725

Browse files
committed
feat: Expose dhatu code in vidyut.kosha.DhatuEntry
Fixes #164 Add code field to DhatuMeta and expose it through Python API Following the same pattern as #163 (artha field implementation)
1 parent 3541b2d commit 5f03725

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

bindings-python/src/kosha/entries.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ pub struct PyDhatuEntry {
3737
/// - `vidi~` --> `vind`
3838
pub(crate) clean_text: String,
3939

40+
/// The Dhatupatha code for this dhatu.
41+
///
42+
/// Examples:
43+
///
44+
/// - `BU` --> `01.0001`
45+
/// - `eD` --> `01.0002`
46+
pub(crate) code: Option<String>,
47+
4048
/// The meaning of this dhatu's *mūla* as an SLP1 string.
4149
///
4250
/// We have meaning strings only for the ~2000 *mūla* dhatus from the Dhatupatha. Any roots
@@ -59,11 +67,12 @@ pub struct PyDhatuEntry {
5967
impl PyDhatuEntry {
6068
/// Create a new `DhatuEntry`.
6169
#[new]
62-
#[pyo3(signature = (dhatu, clean_text, *, artha_sa = None, artha_en = None, artha_hi = None,
70+
#[pyo3(signature = (dhatu, clean_text, *, code = None, artha_sa = None, artha_en = None, artha_hi = None,
6371
karmatva = None, ittva = None, pada = None))]
6472
fn new(
6573
dhatu: PyDhatu,
6674
clean_text: String,
75+
code: Option<String>,
6776
artha_sa: Option<String>,
6877
artha_en: Option<String>,
6978
artha_hi: Option<String>,
@@ -74,6 +83,7 @@ impl PyDhatuEntry {
7483
Self {
7584
dhatu,
7685
clean_text,
86+
code,
7787
artha_sa,
7888
artha_en,
7989
artha_hi,
@@ -85,9 +95,10 @@ impl PyDhatuEntry {
8595

8696
fn __repr__(&self) -> String {
8797
format!(
88-
"DhatuEntry(dhatu={}, clean_text={}, artha_sa={})",
98+
"DhatuEntry(dhatu={}, clean_text={}, code={}, artha_sa={})",
8999
self.dhatu.__repr__(),
90100
py_repr_string(&self.clean_text),
101+
py_repr_option_string(&self.code),
91102
py_repr_option_string(&self.artha_sa),
92103
)
93104
}
@@ -103,6 +114,7 @@ impl<'a> From<&DhatuEntry<'a>> for PyDhatuEntry {
103114
Self {
104115
dhatu: val.dhatu().into(),
105116
clean_text: val.clean_text().to_string(),
117+
code: val.code().map(|x| x.to_string()),
106118
artha_sa: val.artha_sa().map(|x| x.to_string()),
107119
artha_en: val.artha_en().map(|x| x.to_string()),
108120
artha_hi: val.artha_hi().map(|x| x.to_string()),

vidyut-kosha/src/entries.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub struct DhatuEntry<'a> {
2424
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
2525
pub struct DhatuMeta {
2626
pub(crate) clean_text: String,
27+
pub(crate) code: Option<String>,
2728
pub(crate) artha_sa: Option<String>,
2829
pub(crate) artha_hi: Option<String>,
2930
pub(crate) artha_en: Option<String>,
@@ -36,6 +37,7 @@ pub struct DhatuMeta {
3637
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
3738
pub struct DhatuMetaBuilder {
3839
clean_text: Option<String>,
40+
code: Option<String>,
3941
artha_sa: Option<String>,
4042
artha_hi: Option<String>,
4143
artha_en: Option<String>,
@@ -122,6 +124,18 @@ impl<'a> DhatuEntry<'a> {
122124
self.meta.map_or("", |x| &x.clean_text)
123125
}
124126

127+
/// Returns the Dhatupatha code for this dhatu.
128+
///
129+
/// Examples:
130+
///
131+
/// - `BU` --> `01.0001`
132+
/// - `eD` --> `01.0002`
133+
///
134+
/// Data is sourced from <https://ashtadhyayi.com>.
135+
pub fn code(&self) -> Option<&str> {
136+
self.meta.and_then(|x| x.code.as_deref())
137+
}
138+
125139
/// Returns the Sanskrit meaning of this dhatu's *mūla* as an SLP1 string. All of these
126140
/// meaning strings come directly from the Dhatupatha.
127141
///
@@ -192,6 +206,12 @@ impl DhatuMetaBuilder {
192206
self
193207
}
194208

209+
/// (Optional) Sets `code`.
210+
pub fn code(mut self, code: String) -> Self {
211+
self.code = Some(code);
212+
self
213+
}
214+
195215
/// (Optional) Sets `artha_sa`.
196216
pub fn artha_sa(mut self, artha: String) -> Self {
197217
self.artha_sa = Some(artha);
@@ -235,6 +255,7 @@ impl DhatuMetaBuilder {
235255
Some(x) => x,
236256
_ => return Err(Error::UnsupportedType),
237257
},
258+
code: self.code,
238259
artha_sa: self.artha_sa,
239260
artha_en: self.artha_en,
240261
artha_hi: self.artha_hi,

0 commit comments

Comments
 (0)