2

I'm creating a Rust app that needs to save and load files with its own custom file format, similar to Photoshop and Illustrator save/load to .psd and .ai files, respectively.

  1. How do I go about "defining" my own file format so that the OS recognizes it and will use my app to open this file format (.mtp is the filetype's extension name)?

  2. How do I develop a means of parsing this file format in my app?

In reality, my app is saving to a JSON style structure via Serde. I want this file extension to distinguish the file type as something unique to my app so that users don't load random .json files into the app.

struct MyApp{
   name: String,
   content: String,
}

impl MyApp{
    save_file(&self, path: String)->std::io::Result<()>{
        let mut file = std::fs:File::create(path)?;
        Ok(())
    }
    load_file(&self, path: String)->std::io::Result<()>{
        let mut file = std::fs:File::open(path)?;
        //do something with the file
        Ok(())
    }
}

fn main(){
    //using my own custom .mtp format
    let app = MyApp{
        name: "CoolApp".to_string(),
        content: "some information".to_string(),
    }
    app.save_fle("output/my_path.mtp");
    app.load_file("output/my_path.mtp");
}
Matze
  • 5,100
  • 6
  • 46
  • 69
ANimator120
  • 2,556
  • 1
  • 20
  • 52
  • I'm going to take the linux approach that there is no such thing as as a file format. Some files just happen to end with `.psd` or `.ai` and others don't have a file extension at all. The real question is how you want to parse the file. There is no wrong answer. – Locke Mar 05 '21 at 20:11
  • For Windows, you can follow the steps outlined by that documentation https://learn.microsoft.com/en-us/windows/win32/shell/how-to-register-a-file-type-for-a-new-application – Matze Mar 05 '21 at 20:11
  • As an alternative to JSON, you could also consider an XML-based format instead. This would give you the option to validate the actual document´s content against a schema (in order to address the problem where users try to load random, or non-compatible files). – Matze Mar 05 '21 at 20:15
  • For Linux, it depends on the desktop environment of the distribution; I think this question already has some useful answers https://stackoverflow.com/questions/30931/register-file-extensions-mime-types-in-linux – Matze Mar 05 '21 at 20:18
  • @Matze so associating the file types is an OS-dependent process performed at app installation with shell scripts, not something that's done from the rust binary? – ANimator120 Mar 05 '21 at 20:27
  • The registration of a mime-type and the file content structures that describe the format is actually two different things. In the case of JSON, you need to model the data the way you need it (`Serde` does the rest). For any other kind of binary file format, it may help to write a parser for a well-known file-type (such as BMP, or even PSD) and learn from it. – Matze Mar 05 '21 at 20:27
  • @ANimator120 Yes, the registration of the mime-type is usually OS-specific. The registration is usually done by the setup, but it could also be done by the app itself. You need to find out what works best for your app, and if you want to have platform-specific functionality in your code. – Matze Mar 05 '21 at 20:33

0 Answers0