-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c4067f
commit b818c90
Showing
29 changed files
with
507 additions
and
160 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "ryo3-size" | ||
description = "python + size crate (https://crates.io/crates/size)" | ||
version.workspace = true | ||
authors.workspace = true | ||
documentation.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
pyo3 = { workspace = true, features = ["experimental-inspect"] } | ||
size.workspace = true | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# `ryo3-size` | ||
|
||
python + `size` crate. | ||
|
||
`size`: | ||
- [crates.io](https://crates.io/crates/size) | ||
- [docs.rs](https://docs.rs/size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use crate::types::{Base, Style}; | ||
use pyo3::prelude::*; | ||
|
||
#[pyfunction] | ||
pub fn parse_size(input: &str) -> PyResult<i64> { | ||
size::Size::from_str(input) | ||
.map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("{e}"))) | ||
.map(|s| s.bytes()) | ||
} | ||
|
||
/// Format a size of bytes into a human-readable string. | ||
#[must_use] | ||
#[pyfunction] | ||
#[pyo3(signature = (n, *, base = None, style = None))] | ||
pub fn fmt_size(n: i64, base: Option<Base>, style: Option<Style>) -> String { | ||
let formatter = size::fmt::SizeFormatter::new() | ||
.with_base(base.unwrap_or_default().0) | ||
.with_style(style.unwrap_or_default().0); | ||
formatter.format(n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![doc = include_str!("../README.md")] | ||
|
||
mod fns; | ||
mod size_formatter; | ||
mod types; | ||
|
||
pub use fns::*; | ||
use pyo3::prelude::*; | ||
use pyo3::types::PyModule; | ||
|
||
pub fn pymod_add(m: &Bound<'_, PyModule>) -> PyResult<()> { | ||
m.add_class::<size_formatter::PySizeFormatter>()?; | ||
m.add_function(wrap_pyfunction!(parse_size, m)?)?; | ||
m.add_function(wrap_pyfunction!(fmt_size, m)?)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::types::{Base, Style}; | ||
use pyo3::prelude::*; | ||
|
||
#[pyclass(name = "SizeFormatter", module = "ry")] | ||
pub struct PySizeFormatter { | ||
formatter: size::fmt::SizeFormatter, | ||
base: Base, | ||
style: Style, | ||
} | ||
|
||
#[pymethods] | ||
impl PySizeFormatter { | ||
#[new] | ||
#[pyo3(signature = (base = None, style = None))] | ||
fn py_new(base: Option<Base>, style: Option<Style>) -> Self { | ||
// fn py_new(base: Option<u8>, style: Option<&str>) -> Self { | ||
let base = base.unwrap_or_default(); | ||
let style = style.unwrap_or_default(); | ||
|
||
let formatter = size::fmt::SizeFormatter::new() | ||
.with_base(base.0) | ||
.with_style(style.0); | ||
PySizeFormatter { | ||
formatter, | ||
base, | ||
style, | ||
} | ||
} | ||
|
||
fn format(&self, n: i64) -> String { | ||
self.formatter.format(n) | ||
} | ||
|
||
fn __repr__(&self) -> String { | ||
format!("SizeFormatter(base: {}, style: {})", self.base, self.style) | ||
} | ||
|
||
fn __call__(&self, n: i64) -> String { | ||
self.formatter.format(n) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use pyo3::prelude::*; | ||
use pyo3::types::{PyInt, PyString}; | ||
use std::fmt::Display; | ||
|
||
pub struct Base(pub size::fmt::Base); | ||
|
||
impl Default for Base { | ||
fn default() -> Self { | ||
Base(size::fmt::Base::Base10) | ||
} | ||
} | ||
|
||
impl Display for Base { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self.0 { | ||
size::fmt::Base::Base2 => write!(f, "2"), | ||
size::fmt::Base::Base10 => write!(f, "10"), | ||
_ => write!(f, "unknown"), | ||
} | ||
} | ||
} | ||
|
||
const BASE_ERR_MSG: &str = "base must be be int(2)/int(10)"; | ||
impl FromPyObject<'_> for Base { | ||
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> { | ||
// if is int... | ||
if ob.is_none() { | ||
Ok(Base::default()) | ||
} else if let Ok(i) = ob.downcast::<PyInt>() { | ||
let base = i.extract::<u8>()?; | ||
match base { | ||
2 => Ok(Base(size::fmt::Base::Base2)), | ||
10 => Ok(Base(size::fmt::Base::Base10)), | ||
_ => Err(pyo3::exceptions::PyValueError::new_err(format!( | ||
"{BASE_ERR_MSG} ~ given: {base}" | ||
))), | ||
} | ||
} else { | ||
Err(pyo3::exceptions::PyTypeError::new_err(BASE_ERR_MSG)) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Style(pub size::fmt::Style); | ||
|
||
impl Default for Style { | ||
fn default() -> Self { | ||
Style(size::fmt::Style::Default) | ||
} | ||
} | ||
|
||
const STYLE_ERR_MSG: &str = | ||
"style must be None/'default'/'abbreviated'/'abbreviated_lowercase'/'full'/'full_lowercase'"; | ||
|
||
impl FromPyObject<'_> for Style { | ||
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> { | ||
if ob.is_none() { | ||
Ok(Style::default()) | ||
} else if let Ok(s) = ob.downcast::<PyString>() { | ||
let s_ref = s.to_str()?; | ||
match s_ref.to_ascii_lowercase().as_str() { | ||
"default" => Ok(Style(size::fmt::Style::Default)), | ||
"abbreviated" => Ok(Style(size::fmt::Style::Abbreviated)), | ||
"abbreviated_lowercase" | "abbreviated-lowercase" => { | ||
Ok(Style(size::fmt::Style::AbbreviatedLowercase)) | ||
} | ||
"full" => Ok(Style(size::fmt::Style::Full)), | ||
"full_lowercase" | "full-lowercase" => Ok(Style(size::fmt::Style::FullLowercase)), | ||
_ => Err(pyo3::exceptions::PyValueError::new_err(format!( | ||
"{STYLE_ERR_MSG} ~ given: {s_ref}" | ||
))), | ||
} | ||
} else { | ||
Err(pyo3::exceptions::PyTypeError::new_err(STYLE_ERR_MSG)) | ||
} | ||
} | ||
} | ||
|
||
impl Display for Style { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self.0 { | ||
size::fmt::Style::Default => write!(f, "default"), | ||
size::fmt::Style::Abbreviated => write!(f, "abbreviated"), | ||
size::fmt::Style::AbbreviatedLowercase => write!(f, "abbreviated_lowercase"), | ||
size::fmt::Style::Full => write!(f, "full"), | ||
size::fmt::Style::FullLowercase => write!(f, "full_lowercase"), | ||
_ => write!(f, "unknown"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.