use core::ops::Deref;
use super::{Node, NodeBase};
#[derive(Clone)]
pub struct Chosen<'a> {
node: NodeBase<'a>,
}
impl<'a> Chosen<'a> {
pub(crate) fn new(node: NodeBase<'a>) -> Self {
Self { node }
}
pub fn bootargs(&self) -> Option<&'a str> {
self.node.find_property_str("bootargs")
}
pub fn stdout_path(&self) -> Option<&'a str> {
self.node.find_property_str("stdout-path")
}
pub fn stdout(&self) -> Option<Node<'a>> {
let path = split_path_options(self.stdout_path()?);
self.node._fdt.find_by_path(path)
}
pub fn stdin_path(&self) -> Option<&'a str> {
self.node.find_property_str("stdin-path")
}
}
impl<'a> Deref for Chosen<'a> {
type Target = NodeBase<'a>;
fn deref(&self) -> &Self::Target {
&self.node
}
}
impl core::fmt::Debug for Chosen<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Chosen")
.field("bootargs", &self.bootargs())
.field("stdout_path", &self.stdout_path())
.finish()
}
}
fn split_path_options(path: &str) -> &str {
path.split_once(':').map_or(path, |(path, _)| path)
}
#[cfg(test)]
mod tests {
use super::split_path_options;
#[test]
fn split_path_options_keeps_plain_path() {
assert_eq!(split_path_options("/pl011@9000000"), "/pl011@9000000");
assert_eq!(split_path_options("serial0"), "serial0");
}
#[test]
fn split_path_options_removes_serial_options() {
assert_eq!(
split_path_options("/pl011@9000000:115200n8"),
"/pl011@9000000"
);
assert_eq!(split_path_options("serial0:115200n8"), "serial0");
}
}