changepacks_python/
package.rs1use anyhow::Result;
2use async_trait::async_trait;
3use changepacks_core::{Language, Package, UpdateType};
4use changepacks_utils::next_version;
5use std::path::{Path, PathBuf};
6use tokio::fs::{read_to_string, write};
7use toml_edit::DocumentMut;
8
9#[derive(Debug)]
10pub struct PythonPackage {
11 name: String,
12 version: String,
13 path: PathBuf,
14 relative_path: PathBuf,
15 is_changed: bool,
16}
17
18impl PythonPackage {
19 pub fn new(name: String, version: String, path: PathBuf, relative_path: PathBuf) -> Self {
20 Self {
21 name,
22 version,
23 path,
24 relative_path,
25 is_changed: false,
26 }
27 }
28}
29
30#[async_trait]
31impl Package for PythonPackage {
32 fn name(&self) -> &str {
33 &self.name
34 }
35
36 fn version(&self) -> &str {
37 &self.version
38 }
39
40 fn path(&self) -> &Path {
41 &self.path
42 }
43
44 fn relative_path(&self) -> &Path {
45 &self.relative_path
46 }
47
48 async fn update_version(&self, update_type: UpdateType) -> Result<()> {
49 let next_version = next_version(&self.version, update_type)?;
50
51 let pyproject_toml = read_to_string(&self.path).await?;
52 let mut pyproject_toml: DocumentMut = pyproject_toml.parse::<DocumentMut>()?;
53 pyproject_toml["project"]["version"] = next_version.into();
54 write(&self.path, pyproject_toml.to_string()).await?;
55 Ok(())
56 }
57
58 fn language(&self) -> Language {
59 Language::Python
60 }
61
62 fn set_changed(&mut self, changed: bool) {
63 self.is_changed = changed;
64 }
65
66 fn is_changed(&self) -> bool {
67 self.is_changed
68 }
69}