Getters and Setters for python API

⚓ Rust    📅 2025-08-31    👤 surdeus    👁️ 2      

surdeus

Hello, I am having a hard time figuring out if I am going in a good direction for this project and I will do my best to keep this question concise and to the point.

I am currently making a CLI program using Clap and one of its functionalities for example is to parse files into json and other formats. In the future I am trying to make this into an API for python scripting and have read many posts and the pyO3 documentation on the matter of using 'getters and setters', but for some reason I cant wrap my head around this topic.

Currently my program structure goes along the lines of this:

pub struct ParseArgs {
    file: Option<PathBuf>,
}
impl ParseArgs {
    pub fn new(
        file: Option<PathBuf>,
    ) -> Self {
        Self {
            file,
        }
    }

    pub fn file(&self) -> Option<&PathBuf> {
        self.file.as_ref()
    }

Then, in my CLI core logic I match my commands to this structure and send it off to perform whatever function it needs to go to.

So my question is as follows:: Is this a good approach to this problem and data type, and in the near future when I start building in my API is it a good setup to use the #[getter] and #[setter] functionality in pyO3? Or am I over complicating it and can just use #[pyo3(get, set)] implementation of this? Or am I going in the wrong direction completely and should just make the struct fields public and rely on rusts strict typing?

Thank you to anyone who helps me with this, it is much appreciated! This is my first big rust project so I am little lost on structuring functionality.

1 post - 1 participant

Read full topic

🏷️ Rust_feed