Struct OsRelease

Source
pub struct OsRelease { /* private fields */ }
Expand description

The parsed contents of the os-release file.

This structure is a map of the fields in the os-release file.

For more information, see os-release(5).

§Notes

If you are using this crate to determine the OS or a specific version of it, use the Self::id() and Self::version_id() methods, possibly with Self::id_like() as fallback for Self::id(). When looking for an OS identification string for presentation to the user, use the Self::pretty_name() method.

Note that operating system vendors may choose not to provide version information, for example to accommodate for rolling releases. In this case, Self::version() and Self::version_id() may be None. Application should not rely on these fields to be set.

§Examples

Open the os-release file and print the OS name and version:

use etc_os_release::OsRelease;

let os_release = OsRelease::open()?;
println!("{}-{}", os_release.id(), os_release.version_id().unwrap_or_default());

Parse a string containing the contents of the os-release file:

use std::str::FromStr;

use etc_os_release::OsRelease;

let os_release = OsRelease::from_str(r#"
NAME=Fedora
VERSION="32 (Workstation Edition)"
ID=fedora
VERSION_ID=32
PRETTY_NAME="Fedora 32 (Workstation Edition)"
ANSI_COLOR="0;38;2;60;110;180"
LOGO=fedora-logo-icon
CPE_NAME="cpe:/o:fedoraproject:fedora:32"
HOME_URL="https://fedoraproject.org/"
DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/f32/system-administrators-guide/"
SUPPORT_URL="https://fedoraproject.org/wiki/Communicating_and_getting_help"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Fedora"
REDHAT_BUGZILLA_PRODUCT_VERSION=32
REDHAT_SUPPORT_PRODUCT="Fedora"
REDHAT_SUPPORT_PRODUCT_VERSION=32
PRIVACY_POLICY_URL="https://fedoraproject.org/wiki/Legal:PrivacyPolicy"
VARIANT="Workstation Edition"
VARIANT_ID=workstation
"#).unwrap();

assert_eq!(os_release.id(), "fedora");
assert_eq!(os_release.version_id(), Some("32"));

Implementations§

Source§

impl OsRelease

Methods to construct an OsRelease.

Source

pub fn open() -> Result<Self, Error>

Open the os-release file and parse it.

If /etc/os-release exists, it is opened. Otherwise, /usr/lib/os-release is opened. If neither file exists, an error is returned.

For simplicity, this function assumes that the file is well-formed.

Source

pub fn from_reader(reader: impl Read) -> Result<Self, Error>

Parse the os-release file from a reader.

For simplicity, this function assumes that the file is well-formed.

Source§

impl OsRelease

Methods to get any field in the os-release file.

Source

pub fn entries(&self) -> impl Iterator<Item = OsReleaseEntry<'_>>

Returns the iterator over the fields in the os-release file.

Source

pub fn get_value(&self, key: &str) -> Option<&str>

Returns the value of a field in the os-release file.

Source

pub fn get_value_as_list(&self, key: &str) -> Option<impl Iterator<Item = &str>>

Returns the value of a field in the os-release as a list of strings.

Source

pub fn get_value_as_url(&self, key: &str) -> Result<Option<Url>, ParseError>

Available on crate feature url only.

Returns the value of a field in the os-release as a URL.

Source

pub fn get_value_as_date( &self, key: &str, ) -> Result<Option<NaiveDate>, ParseError>

Available on crate feature date only.

Returns the value of a field in the os-release as a date.

Source§

impl OsRelease

Methods to get general information identifying the operating system.

For more information, see the General information identifying the operating system section of os-release(5).

Source

pub fn name(&self) -> &str

Returns the string identifying the operating system, without a version component.

This field is suitable for presentation to the user.

If not set in the os-release file, defaults to linux.

For more information, see the NAME= section of os-release(5)

Source

pub fn id(&self) -> &str

Returns the lower-case string identifying the operating system, excluding any version information.

This field is suitable for processing by scripts or usage in generated filenames.

If not set in the os-release file, defaults to linux.

For more information, see the ID= section of os-release(5)

Source

pub fn id_like(&self) -> Option<impl Iterator<Item = &str>>

Returns the list of operating system identifiers.

The list contains operating systems that are closely related to the local operating system in regards to packaging and programming interfaces. For example, the operating system that the local operating system is a derivative from.

For more information, see the ID_LIKE= section of os-release(5)

Source

pub fn pretty_name(&self) -> &str

Returns the pretty operating system name in a format suitable for presentation to the user.

If not set in the os-release file, defaults to Linux.

For more information, see the PRETTY_NAME= section of os-release(5)

Source

pub fn cpe_name(&self) -> Option<&str>

Returns the CPE name for the operating system in URI binding syntax.

The name follows the Common Platform Enumeration Specification as proposed by the NIST.

For more information, see the CPE_NAME= section of os-release(5)

Source

pub fn variant(&self) -> Option<&str>

Returns the string identifying a specific variant or edition of the operating system.

This field is suitable for presentation to the user.

For more information, see the VARIANT= section of os-release(5)

§Note

This field is for display purposes only. The Self::variant_id() field should be used for making programmatic decisions.

Source

pub fn variant_id(&self) -> Option<&str>

Returns the lower-case string identifying a specific variant or edition of the operating system.

For more information, see the VARIANT_ID= section of os-release(5)

Source§

impl OsRelease

Methods to get information about the version of the operating system.

For more information, see the Information about the version of the operating system section of os-release(5).

Source

pub fn version(&self) -> Option<&str>

Returns the string identifying the operating system version, excluding any OS name information.

This field possibly includes a release code name.

This field is suitable for presentation to the user.

For more information, see the VERSION= section of os-release(5)

Source

pub fn version_id(&self) -> Option<&str>

Returns the lower-case string identifying the operating system version, excluding any OS name information or release code name.

This field is suitable for processing by scripts or usage in generated filenames.

For more information, see the VERSION_ID= section of os-release(5)

Source

pub fn version_codename(&self) -> Option<&str>

Returns the string identifying the operating system release code name, excluding any OS name information or release code name.

This field is suitable for processing by scripts or usage in generated filenames.

For more information, see the VERSION_CODENAME= section of os-release(5)

Source

pub fn build_id(&self) -> Option<&str>

Returns the string uniquely identifying the system image originally used as the installation base.

For more information, see the BUILD_ID= section of os-release(5)

Source

pub fn image_id(&self) -> Option<&str>

Returns the lower-case string identifying a specific image of the operating system.

For more information, see the IMAGE_ID= section of os-release(5)

Source

pub fn image_version(&self) -> Option<&str>

Return the lower-case string identifying the OS image version.

For more information, see the IMAGE_VERSION= section of os-release(5)

Source§

impl OsRelease

Methods to get presentation information and links.

For more information, see the Presentation information and links section of os-release(5).

Source

pub fn home_url(&self) -> Result<Option<Url>, ParseError>

Available on crate feature url only.

Returns the URL of the homepage of the operating system, or alternatively some homepage of the specific version of the operating system.

For more information, see the HOME_URL= section of os-release(5)

Source

pub fn documentation_url(&self) -> Result<Option<Url>, ParseError>

Available on crate feature url only.

Returns the URL of the main documentation page of the operating system.

For more information, see the HOME_URL= section of os-release(5)

Source

pub fn support_url(&self) -> Result<Option<Url>, ParseError>

Available on crate feature url only.

Returns the URL of the main support page for the operating system.

For more information, see the HOME_URL= section of os-release(5)

Source

pub fn bug_report_url(&self) -> Result<Option<Url>, ParseError>

Available on crate feature url only.

Returns the main bug reporting page for the operating system.

For more information, see the HOME_URL= section of os-release(5)

Source

pub fn privacy_policy_url(&self) -> Result<Option<Url>, ParseError>

Available on crate feature url only.

Returns the main privacy policy page for the operating system.

For more information, see the HOME_URL= section of os-release(5)

Source

pub fn support_end(&self) -> Result<Option<NaiveDate>, ParseError>

Available on crate feature date only.

Returns the date at which support for this version of the OS ends.

For more information, see the SUPPORT_END= section of os-release(5)

Returns the logo string, specifying the name of an icon as defined by freedesktop.org Icon Theme Specification.

For more information, see the LOGO= section of os-release(5)

Source

pub fn ansi_color(&self) -> Option<&str>

Returns the suggested presentation color when showing the OS name on the console.

For more information, see the ANSI_COLOR= section of os-release(5)

Source

pub fn vendor_name(&self) -> Option<&str>

Returns the name of the OS vendor.

For more information, see the VENDOR_NAME= section of os-release(5)

Source

pub fn vendor_url(&self) -> Result<Option<Url>, ParseError>

Available on crate feature url only.

Returns the homepage of the OS vendor.

For more information, see the VENDOR_URL= section of os-release(5)

Source§

impl OsRelease

Methods to get distribution-level defaults and metadata.

For more information, see the Distribution-level defaults and metadata section of os-release(5).

Source

pub fn default_hostname(&self) -> Option<&str>

Returns the string specifying the hostname if hostname(5) is not present and no other configuration source specifies the hostname.

For more information, see the DEFAULT_HOSTNAME= section of os-release(5)

Source

pub fn architecture(&self) -> Option<&str>

Returns the string that specifies which CPU architecture the userspace binaries require.

For more information, see the ARCHITECTURE= section of os-release(5)

Source

pub fn sysext_level(&self) -> Option<&str>

Returns the lower-case string identifying the operating system extensions support level, to indicate which extension images are supported.

For more information, see the SYSEXT_LEVEL= section of os-release(5)

Source

pub fn confext_level(&self) -> Option<&str>

Returns the lower-case string identifying the operating system confext support level, to indicate which confext images are supported.

For more information, see the CONFEXT_LEVEL= section of os-release(5)

Source

pub fn sysext_scope(&self) -> Option<impl Iterator<Item = &str>>

Returns the list of one or more of the strings "system", "initrd" and "portable".

For more information, see the SYSEXT_SCOPE= section of os-release(5)

Source

pub fn confext_scope(&self) -> Option<impl Iterator<Item = &str>>

Returns the list of one or more of the strings "system", "initrd" and "portable".

For more information, see the CONFEXT_SCOPE= section of os-release(5)

Source

pub fn portable_prefixes(&self) -> Option<impl Iterator<Item = &str>>

Returns the list of one or more valid prefix match strings for the Portable Services Documentation logic.

For more information, see the PORTABLE_PREFIXES= section of os-release(5)

Trait Implementations§

Source§

impl Clone for OsRelease

Source§

fn clone(&self) -> OsRelease

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OsRelease

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> FromIterator<&'a str> for OsRelease

Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = &'a str>,

Creates a value from an iterator. Read more
Source§

impl<'a> FromIterator<OsReleaseEntry<'a>> for OsRelease

Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = OsReleaseEntry<'a>>,

Creates a value from an iterator. Read more
Source§

impl FromIterator<String> for OsRelease

Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = String>,

Creates a value from an iterator. Read more
Source§

impl FromStr for OsRelease

Source§

type Err = Infallible

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,