Skip to content

Commit

Permalink
Improve APIs and comments
Browse files Browse the repository at this point in the history
- Add `Serialize` and `Deserialize` to all vidyut-prakriya arguments.

- Add `Ord` and `PartialOrd` to various types.

- Improve `vidyut-prakriya` documentation by formatting Sanskrit terms
  more consistently.

- Add new helper methods to `vidyut-lipi` to simplify some work in
  vidyut-py.

- Add new helper methods to `Vrtta` and move `ganas()` method to `Pada`.

- Tweak a test in `vidyut-chandas` for better robustness.

- Generally, remove return type of `&Vec<T>` in favor of `&[T]`.
  • Loading branch information
akprasad committed Nov 17, 2024
1 parent 2b5a980 commit 6022952
Show file tree
Hide file tree
Showing 38 changed files with 577 additions and 414 deletions.
16 changes: 9 additions & 7 deletions vidyut-chandas/src/chandas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Match {
}

/// The aksharas in this query.
pub fn aksharas(&self) -> &Vec<Vec<Akshara>> {
pub fn aksharas(&self) -> &[Vec<Akshara>] {
&self.aksharas
}
}
Expand All @@ -57,17 +57,17 @@ pub struct Matches {

impl Matches {
/// The padya matches for the query.
pub fn padyas(&self) -> &Vec<Padya> {
pub fn padyas(&self) -> &[Padya] {
&self.padyas
}

/// The match type for this query.
pub fn match_types(&self) -> &Vec<MatchType> {
pub fn match_types(&self) -> &[MatchType] {
&self.match_types
}

/// The aksharas in this query.
pub fn aksharas(&self) -> &Vec<Vec<Akshara>> {
pub fn aksharas(&self) -> &[Vec<Akshara>] {
&self.aksharas
}
}
Expand Down Expand Up @@ -147,12 +147,12 @@ impl Chandas {
}

/// The vrttas available to this classifier.
pub fn vrttas(&self) -> &Vec<Vrtta> {
pub fn vrttas(&self) -> &[Vrtta] {
&self.vrttas
}

/// The jatis available to this classifier.
pub fn jatis(&self) -> &Vec<Jati> {
pub fn jatis(&self) -> &[Jati] {
&self.jatis
}

Expand Down Expand Up @@ -267,7 +267,9 @@ mod tests {
fn new_chandas() -> Chandas {
Chandas::new(vec![
"vasantatilakA\tvrtta\tGGLGLLLGLLGLGG".try_into().unwrap(),
"mandAkrAntA\tvrtta\tGGGGLLLLLGGLGGLGG".try_into().unwrap(),
"mandAkrAntA\tvrtta\tGGGG|LLLLLG|GLGGLGG"
.try_into()
.unwrap(),
"puzpitAgrA\tvrtta\tLLLLLLGLGLGG/LLLLGLLGLGLGG"
.try_into()
.unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion vidyut-chandas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ mod wasm;

pub use akshara::{Akshara, Weight};
pub use chandas::{Chandas, Match, Matches};
pub use padya::{Jati, MatchType, Vrtta};
pub use padya::{Jati, MatchType, Pada, PatternWeight, Vrtta};
108 changes: 60 additions & 48 deletions vidyut-chandas/src/padya.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::akshara::{Akshara, Weight};
use crate::error::{ChandasError, Result};

/// Models the weights that a vrtta can accept.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum PatternWeight {
/// A heavy syllable.
G,
Expand Down Expand Up @@ -74,10 +74,10 @@ impl Gana {
}
}

/// Models a *pāda*, which is one of the four "feet" or "legs" of a verse.
/// Models a *pāda*, which is one of the four "feet" or "legs" of a vrtta.
/// A *pāda* defines a specific pattern of light and heavy syllables and
/// might also define one or more *yati*s (caesuras).
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Pada {
weights: Vec<PatternWeight>,
yati: Vec<usize>,
Expand All @@ -87,10 +87,49 @@ impl Pada {
fn new(weights: Vec<PatternWeight>, yati: Vec<usize>) -> Self {
Pada { weights, yati }
}

/// Returns the weights that this pada uses.
pub fn weights(&self) -> &[PatternWeight] {
&self.weights
}

/// Returns the caesurae (yati) that occur in this pada.
pub fn yati(&self) -> &[usize] {
&self.yati
}

/// Returns the ganas that define this pada.
pub fn ganas(&self) -> Vec<Gana> {
use Gana::*;
use PatternWeight::*;

let mut ganas = Vec::new();
for chunk in self.weights.chunks(3) {
match chunk {
[L, G, G] => ganas.push(Ya),
[G, G, G] => ganas.push(Ma),
[G, G, L] => ganas.push(Ta),
[G, L, G] => ganas.push(Ra),
[L, G, L] => ganas.push(Ja),
[G, L, L] => ganas.push(Bha),
[L, L, L] => ganas.push(Na),
[L, L, G] => ganas.push(Sa),
_ => {
for a in chunk {
match a {
L => ganas.push(La),
Any | G => ganas.push(Ga),
}
}
}
}
}
ganas
}
}

/// Models a *vṛtta*, which defines a specific pattern of light and heavy syllables.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Vrtta {
name: String,
padas: Vec<Pada>,
Expand All @@ -112,8 +151,8 @@ impl Vrtta {
&self.name
}

#[allow(unused)]
pub(crate) fn padas(&self) -> &Vec<Pada> {
/// Returns the padas that constitute this vrtta.
pub fn padas(&self) -> &[Pada] {
&self.padas
}

Expand Down Expand Up @@ -178,42 +217,6 @@ impl Vrtta {
MatchType::None
}
}

#[allow(unused)]
pub(crate) fn ganas(&self) -> Vec<Vec<Gana>> {
use Gana::*;
use PatternWeight::*;

let mut result = Vec::new();

for pada in &self.padas {
let mut ganas = Vec::new();

for chunk in pada.weights.chunks(3) {
match chunk {
[L, G, G] => ganas.push(Ya),
[G, G, G] => ganas.push(Ma),
[G, G, L] => ganas.push(Ta),
[G, L, G] => ganas.push(Ra),
[L, G, L] => ganas.push(Ja),
[G, L, L] => ganas.push(Bha),
[L, L, L] => ganas.push(Na),
[L, L, G] => ganas.push(Sa),
_ => {
for a in chunk {
match a {
L => ganas.push(La),
Any | G => ganas.push(Ga),
}
}
}
}
}
result.push(ganas);
}

result
}
}

impl TryFrom<&str> for Pada {
Expand Down Expand Up @@ -254,7 +257,7 @@ impl TryFrom<&str> for Vrtta {
}
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) enum JatiKind {
/// A default jati.
Basic,
Expand All @@ -265,7 +268,7 @@ pub(crate) enum JatiKind {
}

/// Models a *jāti*, which defines a specific pattern of *mātrā*s (morae).
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Jati {
/// The name of this jati.
name: String,
Expand Down Expand Up @@ -300,7 +303,7 @@ impl Jati {
}

/// The matras that define this meter. The returned `Vec` has length 4.
pub fn matras(&self) -> &Vec<i32> {
pub fn matras(&self) -> &[i32] {
&self.matras
}

Expand Down Expand Up @@ -416,14 +419,23 @@ mod tests {
use Gana::*;

let vasantatilaka: Vrtta = "vasantatilakA\tvrtta\tGGLGLLLGLLGLGG".try_into().unwrap();
assert_eq!(vasantatilaka.ganas()[0], vec![Ta, Bha, Ja, Ja, Ga, Ga]);
assert_eq!(
vasantatilaka.padas()[0].ganas(),
vec![Ta, Bha, Ja, Ja, Ga, Ga]
);

let mandakranta: Vrtta = "mandAkrAntA\tvrtta\tGGGGLLLLLGGLGGLGG".try_into().unwrap();
assert_eq!(mandakranta.ganas()[0], vec![Ma, Bha, Na, Ta, Ta, Ga, Ga]);
assert_eq!(
mandakranta.padas()[0].ganas(),
vec![Ma, Bha, Na, Ta, Ta, Ga, Ga]
);

let shardula: Vrtta = "SArdUlavikrIqita\tvrtta\tGGGLLGLGLLLGGGLGGLG"
.try_into()
.unwrap();
assert_eq!(shardula.ganas()[0], vec![Ma, Sa, Ja, Sa, Ta, Ta, Ga]);
assert_eq!(
shardula.padas()[0].ganas(),
vec![Ma, Sa, Ja, Sa, Ta, Ta, Ga]
);
}
}
4 changes: 2 additions & 2 deletions vidyut-kosha/src/morph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ impl Dhatu {
impl From<vp::Dhatu> for Dhatu {
fn from(vp: vp::Dhatu) -> Self {
Dhatu {
prefixes: vp.prefixes().clone(),
sanadi: vp.sanadi().clone(),
prefixes: vp.prefixes().to_vec(),
sanadi: vp.sanadi().to_vec(),
text: match vp.upadesha() {
Some(s) => s.to_string(),
None => String::new(),
Expand Down
1 change: 0 additions & 1 deletion vidyut-lipi/src/reshape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,6 @@ pub fn reshape_after(output: String, to: Scheme) -> String {
m.finish()
}
Scheme::GunjalaGondi => {
println!("Before: {output}");
const GUNJALA_GONDI_VIRAMA: char = '\u{11d97}';
while m.not_empty() {
if m.match_2(|x, y| x == GUNJALA_GONDI_VIRAMA && !is_gunjala_gondi_consonant(y)) {
Expand Down
Loading

0 comments on commit 6022952

Please sign in to comment.