#Linux disk CLI tool
32 messages · Page 1 of 1 (latest)
the signature of the ``read_mounts()function looks like thispub fn read_mounts(options: &ReadOptions) -> Result<Vec<Mount>, Error>So this means it assigns a vector todev` right?
How do I access the name field of the struct and assign it to a variable? My thought was to use
let dev = lfs_core::BlockDevice::name;but that's not working.
To strictly answer the question, to "access and assign it to a variable", you start with a value of that type and dolet dev = variable.name;(in this case that'll movenameout ofvariable. If you'd rather not move it, you can do a reference,let dev = &*variable.name;)
Do you already have a value of BlockDevice, or are you trying to make a new value of it?
If your goal was to make a new one, then you can just (or call a method or whatever which returns this type)
let dev = BlockDevice {
name: "something".to_owned(),
dm_name: None,
// or DeviceId::new(0, 0)
id: DeviceId { major: 0, minor: 0 },
parent: None
};
(Obviously I've just stubbed out those for None. You may want to actually fill it, e.g. with Some(whatever))
I figure one of those 2 answers will probably cover your question 😅
yup it's enough for me to play around with at the moment! thank you!
I'm going to do a little refresher on accessing struct fields in the book
didn't get backto this as soon as I had hoped for. But I think I want to try and play around with this Mount struct
A mount point
my thought process is I should be able to assign the struct to a variable let dev = lfs_core::Mount and then obtain the info field by doing dev.info
is my understanding correct?
I think the challenge I'm having is working with a crate's struct instead of creating the struct myself as I've done from the book and other exercises
looks like this read_mounts() function acheives this kind of behavior?
Read all the mount points and load basic information on them
I should be able to assign the struct to a variable
let dev = lfs_core::Mount
Well, that depends on their api at the end of the day.
Sometimes there's an associated new() fn on the struct, sometimes you make it directly like you said, and sometimes there's other api which returns it.
and then obtain the
infofield by doingdev.info
Once you have a value of this type, that's right yeah (if it's apubfield; if not, then they might have some kind of accessor function to get the value)
Yeah, looks like this crate would likely fall under, and sometimes there's other api which returns it
ah I see now. Cool I'll look into it more tonight, Thanks!
update on this, I switched to just using std::Process::Command to invoke lsblk with flags.
I think the problem I was having with lfs_core was I assumed the struct was created already so I was trying to access fields that didn't exist. Also the structs I wanted to use didn't have any constructors and I couldn't figure out how to make a new one.
going to use clap next to expand the table to include columns based on arguments given to the tool.
also going to try and use lsblk -j for JSON formatting to make extensibility easier
you were accessing a method/constant named "name" that didnt exist
on a struct that wasnt instantiated
so yeah first step is get an actual struct
then use .name (access field/self methods) not ::name (access method/const)
yeah, I'm planning on using structs for the JSON stuff so i'll get some struct exercise with that
for json configs?
are they deeply nested, because if not, you can try toml
and if you want json i suggest serde and serde_json
for toml i suggest serde and toml
serde_json is what I was looking at. I was going to use JSON because lsblk has a flag for outputting disk information in JSON format
ok
just so yk with serde its really easy to make both toml and json with the same struct
so just 1 struct writes to both json and toml