Skip to content

Commit

Permalink
Merge pull request #18 from spmiller/windows-support
Browse files Browse the repository at this point in the history
Initial Windows support
  • Loading branch information
rwestphal authored Oct 8, 2024
2 parents a730692 + 77427af commit beb4434
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ bitflags = "2.5"
num-traits = "0.2"
num-derive = "0.4"

[target.'cfg(windows)'.dependencies]
libc = "0.2"

[dev-dependencies]
criterion = "0.3.3"

Expand Down
5 changes: 2 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use bitflags::bitflags;
use std::collections::HashMap;
use std::ffi::CString;
use std::os::raw::{c_char, c_void};
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::slice;
use std::sync::Once;
Expand Down Expand Up @@ -118,7 +117,7 @@ impl Context {
search_dir: P,
) -> Result<()> {
let search_dir =
CString::new(search_dir.as_ref().as_os_str().as_bytes()).unwrap();
CString::new(search_dir.as_ref().to_str().unwrap()).unwrap();
let ret =
unsafe { ffi::ly_ctx_set_searchdir(self.raw, search_dir.as_ptr()) };
if ret != ffi::LY_ERR::LY_SUCCESS {
Expand All @@ -137,7 +136,7 @@ impl Context {
search_dir: P,
) -> Result<()> {
let search_dir =
CString::new(search_dir.as_ref().as_os_str().as_bytes()).unwrap();
CString::new(search_dir.as_ref().to_str().unwrap()).unwrap();
let ret = unsafe {
ffi::ly_ctx_unset_searchdir(self.raw, search_dir.as_ptr())
};
Expand Down
65 changes: 61 additions & 4 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
//! YANG instance data.

use bitflags::bitflags;
use core::ffi::{c_char, c_void};
use std::ffi::CStr;
use std::ffi::CString;
use std::os::raw::{c_char, c_void};
use std::os::unix::io::AsRawFd;
use std::slice;

use crate::context::Context;
Expand Down Expand Up @@ -273,7 +272,8 @@ pub trait Data<'a> {
}

/// Print data tree in the specified format.
fn print_file<F: AsRawFd>(
#[cfg(not(target_os = "windows"))]
fn print_file<F: std::os::unix::io::AsRawFd>(
&self,
fd: F,
format: DataFormat,
Expand All @@ -293,6 +293,29 @@ pub trait Data<'a> {

Ok(())
}
/// Print data tree in the specified format.
#[cfg(target_os = "windows")]
fn print_file(
&self,
file: impl std::os::windows::io::AsRawHandle,
format: DataFormat,
options: DataPrinterFlags,
) -> Result<()> {
use libc::open_osfhandle;

let raw_handle = file.as_raw_handle();

let fd = unsafe { open_osfhandle(raw_handle as isize, 0) };

let ret = unsafe {
ffi::lyd_print_fd(fd, self.raw(), format as u32, options.bits())
};
if ret != ffi::LY_ERR::LY_SUCCESS {
return Err(Error::new(self.context()));
}

Ok(())
}

/// Print data tree in the specified format to a `String`.
///
Expand Down Expand Up @@ -381,7 +404,8 @@ impl<'a> DataTree<'a> {
}

/// Parse (and validate) input data as a YANG data tree.
pub fn parse_file<F: AsRawFd>(
#[cfg(not(target_os = "windows"))]
pub fn parse_file<F: std::os::unix::io::AsRawFd>(
context: &'a Context,
fd: F,
format: DataFormat,
Expand All @@ -407,6 +431,39 @@ impl<'a> DataTree<'a> {

Ok(unsafe { DataTree::from_raw(context, rnode) })
}
#[cfg(target_os = "windows")]
pub fn parse_file(
context: &'a Context,
file: impl std::os::windows::io::AsRawHandle,
format: DataFormat,
parser_options: DataParserFlags,
validation_options: DataValidationFlags,
) -> Result<DataTree<'a>> {
use libc::open_osfhandle;

let raw_handle = file.as_raw_handle();

let fd = unsafe { open_osfhandle(raw_handle as isize, 0) };

let mut rnode = std::ptr::null_mut();
let rnode_ptr = &mut rnode;

let ret = unsafe {
ffi::lyd_parse_data_fd(
context.raw,
fd,
format as u32,
parser_options.bits(),
validation_options.bits(),
rnode_ptr,
)
};
if ret != ffi::LY_ERR::LY_SUCCESS {
return Err(Error::new(context));
}

Ok(unsafe { DataTree::from_raw(context, rnode) })
}

/// Parse (and validate) input data as a YANG data tree.
pub fn parse_string(
Expand Down
26 changes: 24 additions & 2 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use num_traits::FromPrimitive;
use std::ffi::CString;
use std::mem;
use std::os::raw::{c_char, c_void};
use std::os::unix::io::AsRawFd;
use std::slice;

use crate::context::Context;
Expand Down Expand Up @@ -250,7 +249,8 @@ impl<'a> SchemaModule<'a> {
}

/// Print schema tree in the specified format into a file descriptor.
pub fn print_file<F: AsRawFd>(
#[cfg(not(target_os = "windows"))]
pub fn print_file<F: std::os::unix::io::AsRawFd>(
&self,
fd: F,
format: SchemaOutputFormat,
Expand All @@ -270,6 +270,28 @@ impl<'a> SchemaModule<'a> {

Ok(())
}
#[cfg(target_os = "windows")]
pub fn print_file(
&self,
file: impl std::os::windows::io::AsRawHandle,
format: SchemaOutputFormat,
options: SchemaPrinterFlags,
) -> Result<()> {
use libc::open_osfhandle;

let raw_handle = file.as_raw_handle();

let fd = unsafe { open_osfhandle(raw_handle as isize, 0) };

let ret = unsafe {
ffi::lys_print_fd(fd, self.raw, format as u32, options.bits())
};
if ret != ffi::LY_ERR::LY_SUCCESS {
return Err(Error::new(self.context));
}

Ok(())
}

/// Print schema tree in the specified format into a string.
pub fn print_string(
Expand Down

0 comments on commit beb4434

Please sign in to comment.